You are here

public function GridStackViews::buildElements in GridStack 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/views/style/GridStackViews.php \Drupal\gridstack\Plugin\views\style\GridStackViews::buildElements()

Returns gridstack contents.

1 call to GridStackViews::buildElements()
GridStackViews::render in src/Plugin/views/style/GridStackViews.php
Overrides StylePluginBase::render().

File

src/Plugin/views/style/GridStackViews.php, line 143

Class

GridStackViews
GridStack style plugin.

Namespace

Drupal\gridstack\Plugin\views\style

Code

public function buildElements(array $settings, $rows, $grids = []) {
  $build = [];
  $view = $this->view;
  $item_id = $settings['item_id'];
  foreach ($rows as $delta => $row) {
    $view->row_index = $delta;
    $box = [];
    $box['delta'] = $delta;
    $box[$item_id] = [];

    // Overrides fallback breakpoint image_style with grid image_style.
    if (!empty($settings['breakpoints'])) {
      foreach ($settings['breakpoints'] as $key => &$breakpoint) {
        if (isset($breakpoint['image_style']) && !empty($breakpoint['grids'][$delta]) && !empty($breakpoint['grids'][$delta]['image_style'])) {
          $breakpoint['image_style'] = $breakpoint['grids'][$delta]['image_style'];
        }

        // Overrides image style to use a defined image style per grid item.
        // This allows each individual box to have different image style.
        if ($key == 'lg' && !empty($breakpoint['grids'][$delta]['image_style'])) {
          $settings['_dimensions'] = empty($settings['background']);
          $settings['image_style'] = $breakpoint['grids'][$delta]['image_style'];
        }
      }
    }
    $box['settings'] = $settings;
    if (!empty($settings['class'])) {
      $classes = $this
        ->getFieldString($row, $settings['class'], $delta);
      $box['settings']['class'] = empty($classes[$delta]) ? [] : $classes[$delta];
    }

    // Use Vanilla gridstack if so configured, ignoring GridStack markups.
    if (!empty($settings['vanilla'])) {
      $box[$item_id] = $view->rowPlugin
        ->render($row);
    }
    else {

      // Build individual row/element contents.
      $this
        ->buildElement($box, $row, $delta);
    }

    // Build gridstack items.
    $build[] = $box;
    unset($box);
  }
  unset($view->row_index);
  return $build;
}