You are here

class BlazyGrid in Blazy 8

Same name and namespace in other branches
  1. 8.2 src/BlazyGrid.php \Drupal\blazy\BlazyGrid
  2. 7 src/BlazyGrid.php \Drupal\blazy\BlazyGrid

Provides grid utilities.

Hierarchy

Expanded class hierarchy of BlazyGrid

4 files declare their use of BlazyGrid
BlazyFormatterTrait.php in src/Plugin/Field/FieldFormatter/BlazyFormatterTrait.php
BlazyGridUnitTest.php in tests/src/Unit/BlazyGridUnitTest.php
BlazyViews.php in src/Plugin/views/style/BlazyViews.php
BlazyViewsTest.php in tests/modules/blazy_test/src/Plugin/views/style/BlazyViewsTest.php

File

src/BlazyGrid.php, line 8

Namespace

Drupal\blazy
View source
class BlazyGrid {

  /**
   * Returns items as a grid display wrapped by theme_item_list().
   *
   * @param array $items
   *   The grid items being modified.
   * @param array $settings
   *   The given settings.
   *
   * @return array
   *   The modified array of grid items.
   */
  public static function build(array $items = [], array $settings = []) {
    $settings['style'] = empty($settings['style']) ? 'grid' : $settings['style'];
    $grids = [];
    foreach ($items as $delta => $item) {

      // @todo: Support non-Blazy which normally uses item_id.
      $item_settings = isset($item['#build']) && isset($item['#build']['settings']) ? $item['#build']['settings'] : $settings;
      $item_settings['delta'] = $delta;

      // Supports both single formatter field and complex fields such as Views.
      $grid = [];
      $grid['content'] = [
        '#theme' => 'container',
        '#children' => $item,
        '#attributes' => [
          'class' => [
            'grid__content',
          ],
        ],
      ];
      self::buildGridItemAttributes($grid, $item_settings);
      $grids[] = $grid;
      unset($grid);
    }
    $count = empty($settings['count']) ? count($grids) : $settings['count'];
    $element = [
      '#theme' => 'item_list',
      '#items' => $grids,
      '#context' => [
        'settings' => $settings,
      ],
      '#attributes' => [
        'class' => [
          'blazy--grid',
          'block-' . $settings['style'],
          'block-count-' . $count,
        ],
      ],
      '#wrapper_attributes' => [
        'class' => [
          'item-list--blazy',
          'item-list--blazy-' . $settings['style'],
        ],
      ],
    ];

    // Merge classes and data attributes.
    Blazy::containerAttributes($element['#attributes'], $settings);
    $settings['grid_large'] = $settings['grid'];
    foreach ([
      'small',
      'medium',
      'large',
    ] as $grid) {
      if (!empty($settings['grid_' . $grid])) {
        $element['#attributes']['class'][] = $grid . '-block-' . $settings['style'] . '-' . $settings['grid_' . $grid];
      }
    }
    return $element;
  }

  /**
   * Modifies the grid item wrapper attributes.
   *
   * @param array $grid
   *   The grid item being modified.
   * @param array $settings
   *   The given settings.
   */
  public static function buildGridItemAttributes(array &$grid = [], array $settings = []) {
    if (!empty($settings['grid_item_class'])) {
      $grid['#wrapper_attributes']['class'][] = $settings['grid_item_class'];
    }
    $grid['#wrapper_attributes']['class'][] = 'grid';
    if (!empty($settings['type'])) {
      $grid['#wrapper_attributes']['class'][] = 'grid--' . $settings['type'];
    }
    if (!empty($settings['media_switch'])) {
      $grid['#wrapper_attributes']['class'][] = 'grid--' . str_replace('_', '-', $settings['media_switch']);
    }
    $grid['#wrapper_attributes']['class'][] = 'grid--' . $settings['delta'];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlazyGrid::build public static function Returns items as a grid display wrapped by theme_item_list().
BlazyGrid::buildGridItemAttributes public static function Modifies the grid item wrapper attributes.