3 namespace Drupal\bootstrap\Plugin\Preprocess;
5 use Drupal\bootstrap\Bootstrap;
6 use Drupal\bootstrap\Utility\Element;
7 use Drupal\bootstrap\Utility\Variables;
8 use Drupal\Component\Render\FormattableMarkup;
11 * Pre-processes variables for the "menu_local_action" theme hook.
13 * @ingroup plugins_preprocess
15 * @BootstrapPreprocess("menu_local_action")
17 class MenuLocalAction extends PreprocessBase implements PreprocessInterface {
22 public function preprocessElement(Element $element, Variables $variables) {
23 $link = $element->getProperty('link');
24 $link += ['localized_options' => []];
25 $link['localized_options']['set_active_class'] = TRUE;
27 $icon = Bootstrap::glyphiconFromString($link['title']);
28 $options = isset($link['localized_options']) ? $link['localized_options'] : [];
30 if (isset($link['url'])) {
31 // Turn link into a mini-button and colorize based on title.
32 $class = Bootstrap::cssClassFromString($link['title'], 'default');
33 if (!isset($options['attributes']['class'])) {
34 $options['attributes']['class'] = [];
36 $string = is_string($options['attributes']['class']);
38 $options['attributes']['class'] = explode(' ', $options['attributes']['class']);
40 $options['attributes']['class'][] = 'btn';
41 $options['attributes']['class'][] = 'btn-xs';
42 $options['attributes']['class'][] = 'btn-' . $class;
44 $options['attributes']['class'] = implode(' ', $options['attributes']['class']);
47 $variables['link'] = [
49 '#title' => $icon ? new FormattableMarkup(Element::create($icon)->renderPlain() . '@text', ['@text' => $link['title']]) : $link['title'],
50 '#options' => $options,
51 '#url' => $link['url'],
55 $variables['link'] = [
57 '#title' => $link['title'],
58 '#options' => $options,
59 '#url' => $link['url'],