Commit be1bf5e6 authored by Pierre Dureau's avatar Pierre Dureau
Browse files

Issue #3319153: Carousel: remove _ui_suite_bootstrap_extract_carousel_image...

Issue #3319153: Carousel: remove _ui_suite_bootstrap_extract_carousel_image hook, split slides field between slides and captions, and don't forget to update the carousel example.
parent 0ab9eac4
Loading
Loading
Loading
Loading
+17 −12
Original line number Diff line number Diff line
@@ -22,29 +22,34 @@ carousel:
    slides:
      type: render
      label: "Slides"
      description: "Each slide is a collection of title, subtitle and slide image."
      description: "List of renderables. Each slide is an image."
      preview:
        - - theme: "image"
        - theme: "image"
          uri: "assets/slide-1.svg"
          - type: html_tag
        - theme: "image"
          uri: "assets/slide-2.svg"
        - theme: "image"
          uri: "assets/slide-3.svg"
    captions:
      type: render
      label: "Captions"
      description: "List of renderables. Captions (title, subtitle...) for each slide"
      preview:
        - - type: html_tag
            tag: h5
            value: "First slide label"
          - type: html_tag
            tag: p
            value: "Nulla vitae elit libero, a pharetra augue mollis interdum."
        - - theme: "image"
            uri: "/assets/slide-2.svg"
          - type: html_tag
        - - type: html_tag
            tag: h5
            value: "Second slide label"
            value: "First slide label"
          - type: html_tag
            tag: p
            value: "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
        - - theme: "image"
            uri: "/assets/slide-3.svg"
          - type: html_tag
        - - type: html_tag
            tag: h5
            value: "Third slide label"
            value: "First slide label"
          - type: html_tag
            tag: p
            value: "Praesent commodo cursus magna, vel scelerisque nisl consectetur."
+4 −2
Original line number Diff line number Diff line
@@ -17,10 +17,12 @@
  <div class="carousel-inner" role="listbox">
    {% for slide in slides %}
      <div class="carousel-item{% if loop.first %} active{% endif %}">
        {{ slide.image }}
        {{ slide }}
        {% if captions[loop.index0] %}
        <div class="carousel-caption">
          {{ slide|without('image') }}
          {{ captions[loop.index0] }}
        </div>
        {% endif %}
      </div>
    {% endfor %}
  </div>
+7 −39
Original line number Diff line number Diff line
@@ -22,11 +22,11 @@ function ui_suite_bootstrap_preprocess_page(array &$variables): void {
 * See also: https://getbootstrap.com/docs/4.6/components/card/.
 */
function ui_suite_bootstrap_preprocess_pattern_card(array &$variables): void {
  $base_path = \Drupal::service('extension.list.theme')->getPath('ui_suite_bootstrap');
  if (array_key_exists('image', $variables) && is_array($variables['image'])) {
    foreach ($variables['image'] as &$item) {
      _ui_suite_bootstrap_add_card_image_class($item, $variables['variant']);
      if ($variables['context']->getType() == 'preview') {
        $base_path = \Drupal::service('extension.list.theme')->getPath('ui_suite_bootstrap');
        $uri = isset($item['#uri']) ? implode('/', [
          '',
          $base_path,
@@ -93,52 +93,20 @@ function _ui_suite_bootstrap_add_card_link_class(&$item): void {
 */
function ui_suite_bootstrap_preprocess_pattern_carousel(array &$variables): void {

  // Move first image of each slide in a specific array key.
  if (array_key_exists('slides', $variables) && is_array($variables['slides'])) {
    foreach ($variables['slides'] as &$slide) {
      $slide['image'] = _ui_suite_bootstrap_extract_carousel_image($slide);
    }
  }

  // Nicer preview with fixed width and local backgrounds.
  if ($variables['context']->getType() == 'preview') {
    $variables['attributes']['style'] = "width: 800px";
    $base_path = \Drupal::service('extension.list.theme')->getPath('ui_suite_bootstrap');
    foreach ($variables['slides'] as $i => $slide) {
      if (isset($slide['image']) && is_array($slide['image'])) {
        $image = $slide['image'];
        $uri = isset($image['#uri']) ? implode('/', [
      $uri = isset($slide['#uri']) ? implode('/', [
        '',
        $base_path,
          $image['#uri'],
        $slide['#uri'],
      ]) : '';
        $variables['slides'][$i]['image']['#uri'] = $uri;
      $variables['slides'][$i]['#uri'] = $uri;
    }
  }
  }

}

/**
 * Extract image from carousel slide.
 */
function _ui_suite_bootstrap_extract_carousel_image(&$item) {
  if (is_array($item) && array_key_exists('#theme', $item)) {
    if ($item['#theme'] === 'image' || $item['#theme'] === 'image_formatter') {
      $image = $item;
      $item = [];
      return $image;
    }
  }
  if (isset($item) && is_array($item)) {
    foreach ($item as &$next) {
      $dig = _ui_suite_bootstrap_extract_carousel_image($next);
      if ($dig) {
        return $dig;
      }
    }
  }
  return FALSE;
}

/*
+79 −87
Original line number Diff line number Diff line
@@ -531,12 +531,14 @@ carousel:
        with_controls: true
      "#fields":
        slides:
          - image:
              "#theme": image
              "#uri": >-
                data:image/gif;base64,R0lGODlhAQABAIAAAHd3dwAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==
            caption:
              - "#type": html_tag
          - "#theme": image
            "#uri": data:image/gif;base64,R0lGODlhAQABAIAAAHd3dwAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==
          - "#theme": image
            "#uri": data:image/gif;base64,R0lGODlhAQABAIAAAHd3dwAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==
          - "#theme": image
            "#uri": data:image/gif;base64,R0lGODlhAQABAIAAAHd3dwAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==
        captions:
          - - "#type": html_tag
              "#tag": h1
              "#value": Example headline.
              "#attributes":
@@ -563,12 +565,7 @@ carousel:
                "#fields":
                  label: Sign up today
                  url: "#"
          - image:
              "#theme": image
              "#uri": >-
                data:image/gif;base64,R0lGODlhAQABAIAAAHd3dwAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==
            caption:
              - "#type": html_tag
          - - "#type": html_tag
              "#tag": h1
              "#value": Another example headline.
            - "#type": html_tag
@@ -586,12 +583,7 @@ carousel:
                "#fields":
                  label: Learn more
                  url: "#"
          - image:
              "#theme": image
              "#uri": >-
                data:image/gif;base64,R0lGODlhAQABAIAAAHd3dwAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==
            caption:
              - "#type": html_tag
          - - "#type": html_tag
              "#tag": h1
              "#value": One more for good measure.
              "#attributes":