Version 1
[yaffs-website] / web / themes / contrib / bootstrap / src / Plugin / Preprocess / FilterTips.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\bootstrap\Plugin\Preprocess\FilterTips.
5  */
6
7 namespace Drupal\bootstrap\Plugin\Preprocess;
8
9 use Drupal\bootstrap\Annotation\BootstrapPreprocess;
10 use Drupal\bootstrap\Utility\Variables;
11 use Drupal\Core\Url;
12
13 /**
14  * Pre-processes variables for the "filter_tips" theme hook.
15  *
16  * @ingroup plugins_preprocess
17  *
18  * @BootstrapPreprocess("filter_tips",
19  *   replace = "template_preprocess_filter_tips"
20  * )
21  */
22 class FilterTips extends PreprocessBase implements PreprocessInterface {
23
24   /**
25    * {@inheritdoc}
26    */
27   public function preprocessVariables(Variables $variables) {
28     /** @var \Drupal\filter\FilterFormatInterface $current_format */
29     $current_format = \Drupal::routeMatch()->getParameter('filter_format');
30     $current_format_id = $current_format ? $current_format->id() : FALSE;
31
32     // Create a place holder for the tabs.
33     $build['tabs'] = [
34       '#theme' => 'item_list__filter_tips__tabs',
35       '#items' => [],
36       '#attributes' => [
37         'class' => ['nav', 'nav-tabs', 'filter-formats'],
38         'role' => 'tablist',
39       ],
40     ];
41
42     // Create a placeholder for the panes.
43     $build['panes'] = [
44       '#theme_wrappers' => ['container__filter_tips__panes'],
45       '#attributes' => [
46         'class' => ['tab-content'],
47       ],
48     ];
49
50     foreach (filter_formats(\Drupal::currentUser()) as $format_id => $format) {
51       // Set the current format ID to the first format.
52       if (!$current_format_id) {
53         $current_format_id = $format_id;
54       }
55
56       $tab = [
57         '#type' => 'link',
58         '#title' => $format->label(),
59         '#url' => Url::fromRoute('filter.tips', ['filter_format' => $format_id]),
60         '#attributes' => [
61           'role' => 'tab',
62           'data-toggle' => 'tab',
63           'data-target' => "#$format_id",
64         ],
65       ];
66       if ($current_format_id === $format_id) {
67         $tab['#wrapper_attributes']['class'][] = 'active';
68       }
69       $build['tabs']['#items'][] = $tab;
70
71       $tips = [];
72
73       // Iterate over each format's enabled filters.
74       /** @var \Drupal\filter\Plugin\FilterBase $filter */
75       foreach ($format->filters() as $name => $filter) {
76         // Ignore filters that are not enabled.
77         if (!$filter->status) {
78           continue;
79         }
80
81         $tip = $filter->tips(TRUE);
82         if (isset($tip)) {
83           $tips[] = ['#markup' => $tip];
84         }
85       }
86
87       // Construct the pane.
88       $pane = [
89         '#theme_wrappers' => ['container__filter_tips'],
90         '#attributes' => [
91           'class' => ['tab-pane', 'fade'],
92           'id' => $format_id,
93         ],
94         'list' => [
95           '#theme' => 'item_list',
96           '#items' => $tips,
97         ],
98       ];
99       if ($current_format_id === $format_id) {
100         $pane['#attributes']['class'][] = 'active';
101         $pane['#attributes']['class'][] = 'in';
102       }
103       $build['panes'][] = $pane;
104     }
105
106     $variables['tips'] = $build;
107   }
108
109 }