3 namespace Drupal\bootstrap\Plugin\Preprocess;
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;
14 * Pre-processes variables for the "bootstrap_carousel" theme hook.
16 * @ingroup plugins_preprocess
18 * @BootstrapPreprocess("bootstrap_carousel")
20 class BootstrapCarousel extends PreprocessBase implements PreprocessInterface {
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']);
31 foreach ($variables->slides as $key => &$slide) {
32 if (!isset($slide['attributes'])) {
33 $slide['attributes'] = [];
35 $slide['attributes'] = new Attribute($slide['attributes']);
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 = [
46 '#title' => new FormattableMarkup(Element::create($left_icon)->renderPlain() . '<span class="sr-only">@text</span>', ['@text' => t('Previous')]),
49 'class' => ['left', 'carousel-control'],
51 'data-slide' => 'prev',
56 '#title' => new FormattableMarkup(Element::create($right_icon)->renderPlain() . '<span class="sr-only">@text</span>', ['@text' => t('Next')]),
59 'class' => ['right', 'carousel-control'],
61 'data-slide' => 'next',
68 if ($variables->indicators) {
69 $variables->indicators = [
70 '#theme' => 'item_list__bootstrap_carousel_indicators',
72 '#items' => array_keys($variables->slides),
75 'start_index' => $variables->start_index,
80 // Ensure all attributes are proper objects.
81 $this->preprocessAttributes();