4 * Contains \Drupal\bootstrap\Plugin\Preprocess\BootstrapCarousel.
7 namespace Drupal\bootstrap\Plugin\Preprocess;
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;
19 * Pre-processes variables for the "bootstrap_carousel" theme hook.
21 * @ingroup plugins_preprocess
23 * @BootstrapPreprocess("bootstrap_carousel")
25 class BootstrapCarousel extends PreprocessBase implements PreprocessInterface {
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']);
36 foreach ($variables->slides as $key => &$slide) {
37 if (!isset($slide['attributes'])) {
38 $slide['attributes'] = [];
40 $slide['attributes'] = new Attribute($slide['attributes']);
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 = [
51 '#title' => new FormattableMarkup(Element::create($left_icon)->renderPlain() . '<span class="sr-only">@text</span>', ['@text' => t('Previous')]),
54 'class' => ['left', 'carousel-control'],
56 'data-slide' => 'prev',
61 '#title' => new FormattableMarkup(Element::create($right_icon)->renderPlain() . '<span class="sr-only">@text</span>', ['@text' => t('Next')]),
64 'class' => ['right', 'carousel-control'],
66 'data-slide' => 'next',
73 if ($variables->indicators) {
74 $variables->indicators = [
75 '#theme' => 'item_list__bootstrap_carousel_indicators',
77 '#items' => array_keys($variables->slides),
79 '#start_index' => $variables->start_index,
83 // Ensure all attributes are proper objects.
84 $this->preprocessAttributes();