4 * Contains \Drupal\bootstrap\Plugin\Preprocess\MenuLocalAction.
7 namespace Drupal\bootstrap\Plugin\Preprocess;
9 use Drupal\bootstrap\Annotation\BootstrapPreprocess;
10 use Drupal\bootstrap\Bootstrap;
11 use Drupal\bootstrap\Utility\Element;
12 use Drupal\bootstrap\Utility\Variables;
13 use Drupal\Component\Render\FormattableMarkup;
16 * Pre-processes variables for the "menu_local_action" theme hook.
18 * @ingroup plugins_preprocess
20 * @BootstrapPreprocess("menu_local_action")
22 class MenuLocalAction extends PreprocessBase implements PreprocessInterface {
27 public function preprocessElement(Element $element, Variables $variables) {
28 $link = $element->getProperty('link');
29 $link += ['localized_options' => []];
30 $link['localized_options']['set_active_class'] = TRUE;
32 $icon = Bootstrap::glyphiconFromString($link['title']);
33 $options = isset($link['localized_options']) ? $link['localized_options'] : [];
35 if (isset($link['url'])) {
36 // Turn link into a mini-button and colorize based on title.
37 $class = Bootstrap::cssClassFromString($link['title'], 'default');
38 if (!isset($options['attributes']['class'])) {
39 $options['attributes']['class'] = [];
41 $string = is_string($options['attributes']['class']);
43 $options['attributes']['class'] = explode(' ', $options['attributes']['class']);
45 $options['attributes']['class'][] = 'btn';
46 $options['attributes']['class'][] = 'btn-xs';
47 $options['attributes']['class'][] = 'btn-' . $class;
49 $options['attributes']['class'] = implode(' ', $options['attributes']['class']);
52 $variables['link'] = [
54 '#title' => $icon ? new FormattableMarkup(Element::create($icon)->renderPlain() . '@text', ['@text' => $link['title']]) : $link['title'],
55 '#options' => $options,
56 '#url' => $link['url'],
60 $variables['link'] = [
62 '#title' => $link['title'],
63 '#options' => $options,
64 '#url' => $link['url'],