Commit cf2b4e27 authored by Florent Torregrosa's avatar Florent Torregrosa Committed by Florent Torregrosa
Browse files

Issue #3278423 by Grimreaper: Put preprocess hooks into classes.

parent d7a24f25
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
<?php

declare(strict_types = 1);

namespace Drupal\ui_suite_bootstrap\HookHandler;

/**
 * Handle CSS classes.
 */
class PreprocessPage {

  /**
   * Handle CSS classes.
   *
   * @param array $variables
   *   The preprocessed variables.
   */
  public function preprocess(array &$variables): void {
    // @todo It would be better to have a setting, like bootstrap_barrio do with
    // bootstrap_barrio_fluid_container ('container-fluid' : 'container')
    $variables['container'] = 'container';
  }

}
+66 −0
Original line number Diff line number Diff line
<?php

declare(strict_types = 1);

namespace Drupal\ui_suite_bootstrap\HookHandler;

/**
 * Handle CSS classes.
 */
class PreprocessPatternCard {

  /**
   * Handle CSS classes.
   *
   * @param array $variables
   *   The preprocessed variables.
   */
  public function preprocess(array &$variables): void {
    // @todo If header, parse its content and if nav pattern found, add class
    // matching the variant.

    if (!array_key_exists('image', $variables) || !is_array($variables['image'])) {
      return;
    }

    $image_class = '';
    if ($variables['variant'] === 'default' && $variables['image_position'] === 'top') {
      $image_class = 'card-img-top';
    }
    elseif ($variables['variant'] === 'default' && $variables['image_position'] === 'bottom') {
      $image_class = 'card-img-bottom';
    }
    elseif ($variables['variant'] === 'horizontal') {
      $image_class = 'img-fluid rounded-start';
    }

    if (!empty($image_class)) {
      foreach ($variables['image'] as &$item) {
        $this->addCardImageClass($item, $image_class);
      }
    }
  }

  /**
   * Add expected class in card's image.
   */
  protected function addCardImageClass(&$item, string $image_class): void {
    if (!is_array($item)) {
      return;
    }

    if (array_key_exists('#theme', $item)) {
      if ($item['#theme'] === 'image') {
        $item['#attributes']['class'][] = $image_class;
      }
      if ($item['#theme'] === 'image_formatter') {
        $item['#item_attributes']['class'][] = $image_class;
      }
    }

    foreach ($item as &$next) {
      $this->addCardImageClass($next, $image_class);
    }
  }

}
+49 −0
Original line number Diff line number Diff line
<?php

declare(strict_types = 1);

namespace Drupal\ui_suite_bootstrap\HookHandler;

/**
 * Handle CSS classes.
 */
class PreprocessPatternCardBody {

  /**
   * Handle CSS classes.
   *
   * @param array $variables
   *   The preprocessed variables.
   */
  public function preprocess(array &$variables): void {
    if (array_key_exists('links', $variables) && is_array($variables['links'])) {
      foreach ($variables['links'] as &$item) {
        $this->addCardLinkClass($item);
      }
    }
  }

  /**
   * Add expected class in card's link.
   */
  protected function addCardLinkClass(&$item): void {
    if (!is_array($item)) {
      return;
    }

    if (array_key_exists('#type', $item)) {
      $class = 'card-link';
      if ($item['#type'] === 'link') {
        $item['#attributes']['class'][] = $class;
      }
      if ($item['#type'] === 'html_tag' && $item['#tag'] === 'a') {
        $item['#attributes']['class'][] = $class;
      }
    }

    foreach ($item as &$next) {
      $this->addCardLinkClass($next);
    }
  }

}
+61 −0
Original line number Diff line number Diff line
<?php

declare(strict_types = 1);

namespace Drupal\ui_suite_bootstrap\HookHandler;

use Drupal\Component\Utility\Html;

/**
 * Handle CSS classes and variables structure.
 */
class PreprocessPatternCarousel {

  /**
   * Handle CSS classes and variables structure.
   *
   * @param array $variables
   *   The preprocessed variables.
   */
  public function preprocess(array &$variables): void {
    $variables['carousel_id'] = Html::getUniqueId('bootstrap-carousel');

    // 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'] = $this->extractCarouselImage($slide);
      }
    }

    // Nicer preview with fixed width and local backgrounds.
    if ($variables['context']->getType() == 'preview') {
      $variables['attributes']['style'] = "width: 800px";
    }
  }

  /**
   * Extract image from carousel slide.
   */
  protected function extractCarouselImage(&$item) {
    if (!is_array($item)) {
      return FALSE;
    }

    if (array_key_exists('#theme', $item)) {
      if ($item['#theme'] === 'image' || $item['#theme'] === 'image_formatter') {
        $image = $item;
        $item = [];
        return $image;
      }
    }
    foreach ($item as &$next) {
      $dig = $this->extractCarouselImage($next);
      if ($dig) {
        return $dig;
      }
    }

    return FALSE;
  }

}
+22 −0
Original line number Diff line number Diff line
<?php

declare(strict_types = 1);

namespace Drupal\ui_suite_bootstrap\HookHandler;

/**
 * Ensure variable has proper type.
 */
class PreprocessPatternPagination {

  /**
   * Ensure variable has proper type.
   *
   * @param array $variables
   *   The preprocessed variables.
   */
  public function preprocess(array &$variables): void {
    $variables['current'] = (int) (string) $variables['current'];
  }

}
Loading