You are here

public static function BlazyGrid::build in Blazy 8

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

Returns items as a grid display wrapped by theme_item_list().

Parameters

array $items: The grid items being modified.

array $settings: The given settings.

Return value

array The modified array of grid items.

4 calls to BlazyGrid::build()
BlazyFormatterTrait::viewElements in src/Plugin/Field/FieldFormatter/BlazyFormatterTrait.php
BlazyGridUnitTest::testBuild in tests/src/Unit/BlazyGridUnitTest.php
Tests \Drupal\blazy\BlazyGrid::build().
BlazyViews::render in src/Plugin/views/style/BlazyViews.php
Overrides StylePluginBase::render().
BlazyViewsTest::render in tests/modules/blazy_test/src/Plugin/views/style/BlazyViewsTest.php
Overrides StylePluginBase::render().

File

src/BlazyGrid.php, line 21

Class

BlazyGrid
Provides grid utilities.

Namespace

Drupal\blazy

Code

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;
}