class MenuLocalAction

Pre-processes variables for the "menu_local_action" theme hook.

Plugin annotation

@BootstrapPreprocess("menu_local_action")

Hierarchy

Expanded class hierarchy of MenuLocalAction

Related topics

Namespace

Drupal\bootstrap\Plugin\Preprocess
Source
class MenuLocalAction extends PreprocessBase implements PreprocessInterface {

  /**
   * {@inheritdoc}
   */
  public function preprocessElement(Element $element, Variables $variables) {
    $link = $element->getProperty('link');
    $link += ['localized_options' => []];
    $link['localized_options']['set_active_class'] = TRUE;

    $icon = Bootstrap::glyphiconFromString($link['title']);
    $options = isset($link['localized_options']) ? $link['localized_options'] : [];

    if (isset($link['url'])) {
      // Turn link into a mini-button and colorize based on title.
      $class = Bootstrap::cssClassFromString($link['title'], 'default');
      if (!isset($options['attributes']['class'])) {
        $options['attributes']['class'] = [];
      }
      $string = is_string($options['attributes']['class']);
      if ($string) {
        $options['attributes']['class'] = explode(' ', $options['attributes']['class']);
      }
      $options['attributes']['class'][] = 'btn';
      $options['attributes']['class'][] = 'btn-xs';
      $options['attributes']['class'][] = 'btn-' . $class;
      if ($string) {
        $options['attributes']['class'] = implode(' ', $options['attributes']['class']);
      }

      $variables['link'] = [
        '#type' => 'link',
        '#title' => $icon ? new FormattableMarkup(Element::create($icon)->renderPlain() . '@text', ['@text' => $link['title']]) : $link['title'],
        '#options' => $options,
        '#url' => $link['url'],
      ];
    }
    else {
      $variables['link'] = [
        '#type' => 'link',
        '#title' => $link['title'],
        '#options' => $options,
        '#url' => $link['url'],
      ];
    }
  }

}

Members

Contains filters are case sensitive
Name Modifiers Type Description
MenuLocalAction::preprocessElement public function Preprocess the variables array if an element is present. Overrides PreprocessBase::preprocessElement
PluginBase::$theme protected property The currently set theme object.
PluginBase::__construct public function
PreprocessBase::$hook protected property The theme hook invoked.
PreprocessBase::$info protected property The theme hook info array from the theme registry.
PreprocessBase::$variables protected property The Variables object.
PreprocessBase::preprocess public function Preprocess theme hook variables. Overrides PreprocessInterface::preprocess
PreprocessBase::preprocessAttributes protected function Ensures all attributes have been converted to an Attribute object.
PreprocessBase::preprocessDescription protected function Converts any set description variable into a traversable array.
PreprocessBase::preprocessVariables protected function Preprocess the variables array.