You are here

public static function BlazyGrid::build in Blazy 8.2

Same name and namespace in other branches
  1. 8 src/BlazyGrid.php \Drupal\blazy\BlazyGrid::build()
  2. 7 src/BlazyGrid.php \Drupal\blazy\BlazyGrid::build()

Returns items wrapped by theme_item_list(), can be a grid, or plain list.

Parameters

array $items: The grid items being modified.

array $settings: The given settings.

Return value

array The modified array of grid items.

2 calls to BlazyGrid::build()
BlazyGridUnitTest::testBuild in tests/src/Unit/BlazyGridUnitTest.php
Tests \Drupal\blazy\BlazyGrid::build().
BlazyManager::preRenderBuild in src/BlazyManager.php
Builds the Blazy outputs as a structured array ready for ::renderer().

File

src/BlazyGrid.php, line 21

Class

BlazyGrid
Provides grid utilities.

Namespace

Drupal\blazy

Code

public static function build(array $items = [], array $settings = []) {
  $settings += BlazyDefault::htmlSettings();
  $style = empty($settings['style']) ? '' : $settings['style'];
  $is_grid = isset($settings['_grid']) ? $settings['_grid'] : !empty($settings['style']) && !empty($settings['grid']);
  $class_item = $is_grid ? 'grid' : 'blazy__item';
  $contents = [];
  foreach ($items as $item) {

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

    // Good for Bootstrap .well/ .card class, must cast or BS will reset.
    $classes = empty($item_settings['grid_content_class']) ? [] : (array) $item_settings['grid_content_class'];

    // Supports both single formatter field and complex fields such as Views.
    $content['content'] = $is_grid ? [
      '#theme' => 'container',
      '#children' => $item,
      '#attributes' => [
        'class' => array_merge([
          'grid__content',
        ], $classes),
      ],
    ] : $item;
    if (!empty($item_settings['grid_item_class'])) {
      $attributes['class'][] = $item_settings['grid_item_class'];
    }
    $classes = isset($attributes['class']) ? $attributes['class'] : [];
    $attributes['class'] = array_merge([
      $class_item,
    ], $classes);
    $content['#wrapper_attributes'] = $attributes;
    $contents[] = $content;
  }
  $settings['count'] = empty($settings['count']) ? count($contents) : $settings['count'];
  $wrapper = [
    'item-list--blazy',
    'item-list--blazy-' . $style,
  ];
  $wrapper = $style ? $wrapper : [
    'item-list--blazy',
  ];
  $wrapper = array_merge([
    'item-list',
  ], $wrapper);
  $element = [
    '#theme' => 'item_list',
    '#items' => $contents,
    '#context' => [
      'settings' => $settings,
    ],
    '#attributes' => [],
    '#wrapper_attributes' => [
      'class' => $wrapper,
    ],
  ];

  // Supports field label via Field UI, unless use_field takes place.
  if (empty($settings['use_field']) && isset($settings['label'], $settings['label_display']) && $settings['label_display'] != 'hidden') {
    $element['#title'] = $settings['label'];
  }
  self::attributes($element['#attributes'], $settings);
  return $element;
}