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