Version 1
[yaffs-website] / web / themes / contrib / bootstrap / src / Plugin / Preprocess / BootstrapCarousel.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\bootstrap\Plugin\Preprocess\BootstrapCarousel.
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 use Drupal\Component\Utility\Html;
15 use Drupal\Core\Template\Attribute;
16 use Drupal\Core\Url;
17
18 /**
19  * Pre-processes variables for the "bootstrap_carousel" theme hook.
20  *
21  * @ingroup plugins_preprocess
22  *
23  * @BootstrapPreprocess("bootstrap_carousel")
24  */
25 class BootstrapCarousel extends PreprocessBase implements PreprocessInterface {
26
27   /**
28    * {@inheritdoc}
29    */
30   protected function preprocessVariables(Variables $variables) {
31     // Retrieve the ID, generating one if needed.
32     $id = $variables->getAttribute('id', Html::getUniqueId($variables->offsetGet('id', 'bootstrap-carousel')));
33     unset($variables['id']);
34
35     // Build slides.
36     foreach ($variables->slides as $key => &$slide) {
37       if (!isset($slide['attributes'])) {
38         $slide['attributes'] = [];
39       }
40       $slide['attributes'] = new Attribute($slide['attributes']);
41     }
42
43     // Build controls.
44     if ($variables->controls) {
45       $left_icon = Bootstrap::glyphicon('chevron-left');
46       $right_icon = Bootstrap::glyphicon('chevron-right');
47       $url = Url::fromUserInput("#$id");
48       $variables->controls = [
49         'left' => [
50           '#type' => 'link',
51           '#title' => new FormattableMarkup(Element::create($left_icon)->renderPlain() . '<span class="sr-only">@text</span>', ['@text' => t('Previous')]),
52           '#url' => $url,
53           '#attributes' => [
54             'class' => ['left', 'carousel-control'],
55             'role' => 'button',
56             'data-slide' => 'prev',
57           ],
58         ],
59         'right' => [
60           '#type' => 'link',
61           '#title' => new FormattableMarkup(Element::create($right_icon)->renderPlain() . '<span class="sr-only">@text</span>', ['@text' => t('Next')]),
62           '#url' => $url,
63           '#attributes' => [
64             'class' => ['right', 'carousel-control'],
65             'role' => 'button',
66             'data-slide' => 'next',
67           ],
68         ],
69       ];
70     }
71
72     // Build indicators.
73     if ($variables->indicators) {
74       $variables->indicators = [
75         '#theme' => 'item_list__bootstrap_carousel_indicators',
76         '#list_type' => 'ol',
77         '#items' => array_keys($variables->slides),
78         '#target' => "#$id",
79         '#start_index' => $variables->start_index,
80       ];
81     }
82
83     // Ensure all attributes are proper objects.
84     $this->preprocessAttributes();
85   }
86
87 }