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