You are here

public static function BlazyGrid::build in Blazy 7

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

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

@todo implement cache.

Parameters

array $items: The grid items being modified.

array $settings: The given settings.

Return value

array The modified array of grid items.

1 call to BlazyGrid::build()
BlazyManager::preRenderBuild in src/BlazyManager.php
Builds the Blazy outputs as a structured array ready for ::renderer().

File

src/BlazyGrid.php, line 23

Class

BlazyGrid
Provides grid utilities.

Namespace

Drupal\blazy

Code

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

    // Support non-Blazy which normally uses item_id.
    if (is_array($item)) {
      $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;
      if (isset($item['#build']) && isset($item['#build']['settings'])) {
        $item['#build']['settings'] += $settings;
        $item['#build']['settings']['delta'] = $delta;
      }
      unset($item['settings'], $item['attributes'], $item['item']);
    }
    if (!empty($item_settings['grid_item_class'])) {
      $attributes['class'][] = $item_settings['grid_item_class'];
    }

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

    // Supports single formatter field or complex fields such as Views.
    // Views or entity_view may flattened $item into a string.
    $content = is_string($item) ? [
      '#markup' => $item,
    ] : [
      'content' => $item,
    ];
    $content = $is_grid ? Blazy::container($content, [
      'class' => array_merge([
        'grid__content',
      ], $content_classes),
    ]) : $content;
    $classes = isset($attributes['class']) ? $attributes['class'] : [];
    $classes = array_merge([
      $class_item,
    ], $classes);
    $contents[$delta] = [
      'data' => drupal_render($content),
      'class' => $classes,
    ];
  }
  $settings['count'] = empty($settings['count']) ? count($contents) : $settings['count'];
  $element = [
    '#theme' => 'item_list',
    '#items' => $contents,
    '#context' => [
      'settings' => $settings,
    ],
    '#attributes' => [],
  ];
  self::attributes($element['#attributes'], $settings);
  return $element;
}