Updated the Bootstrap theme.
[yaffs-website] / web / themes / contrib / bootstrap / src / Plugin / Preprocess / MenuLocalAction.php
1 <?php
2
3 namespace Drupal\bootstrap\Plugin\Preprocess;
4
5 use Drupal\bootstrap\Bootstrap;
6 use Drupal\bootstrap\Utility\Element;
7 use Drupal\bootstrap\Utility\Variables;
8 use Drupal\Component\Render\FormattableMarkup;
9
10 /**
11  * Pre-processes variables for the "menu_local_action" theme hook.
12  *
13  * @ingroup plugins_preprocess
14  *
15  * @BootstrapPreprocess("menu_local_action")
16  */
17 class MenuLocalAction extends PreprocessBase implements PreprocessInterface {
18
19   /**
20    * {@inheritdoc}
21    */
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;
26
27     $icon = Bootstrap::glyphiconFromString($link['title']);
28     $options = isset($link['localized_options']) ? $link['localized_options'] : [];
29
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'] = [];
35       }
36       $string = is_string($options['attributes']['class']);
37       if ($string) {
38         $options['attributes']['class'] = explode(' ', $options['attributes']['class']);
39       }
40       $options['attributes']['class'][] = 'btn';
41       $options['attributes']['class'][] = 'btn-xs';
42       $options['attributes']['class'][] = 'btn-' . $class;
43       if ($string) {
44         $options['attributes']['class'] = implode(' ', $options['attributes']['class']);
45       }
46
47       $variables['link'] = [
48         '#type' => 'link',
49         '#title' => $icon ? new FormattableMarkup(Element::create($icon)->renderPlain() . '@text', ['@text' => $link['title']]) : $link['title'],
50         '#options' => $options,
51         '#url' => $link['url'],
52       ];
53     }
54     else {
55       $variables['link'] = [
56         '#type' => 'link',
57         '#title' => $link['title'],
58         '#options' => $options,
59         '#url' => $link['url'],
60       ];
61     }
62   }
63
64 }