file BootstrapCarousel.php

Namespace

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

Classes

Name Description
BootstrapCarousel Pre-processes variables for the "bootstrap_carousel" theme hook.