Version 1
[yaffs-website] / web / themes / contrib / bootstrap / src / Plugin / Preprocess / MenuLocalAction.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\bootstrap\Plugin\Preprocess\MenuLocalAction.
5  */
6
7 namespace Drupal\bootstrap\Plugin\Preprocess;
8
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;
14
15 /**
16  * Pre-processes variables for the "menu_local_action" theme hook.
17  *
18  * @ingroup plugins_preprocess
19  *
20  * @BootstrapPreprocess("menu_local_action")
21  */
22 class MenuLocalAction extends PreprocessBase implements PreprocessInterface {
23
24   /**
25    * {@inheritdoc}
26    */
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;
31
32     $icon = Bootstrap::glyphiconFromString($link['title']);
33     $options = isset($link['localized_options']) ? $link['localized_options'] : [];
34
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'] = [];
40       }
41       $string = is_string($options['attributes']['class']);
42       if ($string) {
43         $options['attributes']['class'] = explode(' ', $options['attributes']['class']);
44       }
45       $options['attributes']['class'][] = 'btn';
46       $options['attributes']['class'][] = 'btn-xs';
47       $options['attributes']['class'][] = 'btn-' . $class;
48       if ($string) {
49         $options['attributes']['class'] = implode(' ', $options['attributes']['class']);
50       }
51
52       $variables['link'] = [
53         '#type' => 'link',
54         '#title' => $icon ? new FormattableMarkup(Element::create($icon)->renderPlain() . '@text', ['@text' => $link['title']]) : $link['title'],
55         '#options' => $options,
56         '#url' => $link['url'],
57       ];
58     }
59     else {
60       $variables['link'] = [
61         '#type' => 'link',
62         '#title' => $link['title'],
63         '#options' => $options,
64         '#url' => $link['url'],
65       ];
66     }
67   }
68
69 }