You are here

public function GridStackViews::buildElements in GridStack 8.2

Same name and namespace in other branches
  1. 8 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 150

Class

GridStackViews
GridStack style plugin.

Namespace

Drupal\gridstack\Plugin\views\style

Code

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

    // Adds box type where image can be anything, not only image.
    if (!empty($settings['image']) && $this
      ->getField($delta, $settings['image'])) {
      $settings['type'] = $row->_entity
        ->hasField($settings['image']) ? $row->_entity
        ->getFieldDefinition($settings['image'])
        ->getFieldStorageDefinition()
        ->getType() : '';
    }
    $box['settings'] = $settings;

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

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

    // Allows extending contents without overriding the entire loop method.
    $this
      ->buildElementExtra($box, $row, $delta);

    // Extracts stamp from existing box.
    $stamp = [];
    if ($delta == $stamp_index && !empty($box['stamp'])) {
      $stamp = $box['stamp'];
      $stamp[$stamp_index]['settings']['type'] = 'stamp';
      unset($box['stamp']);
    }

    // Build gridstack items.
    $build[] = $box;

    // Inserts stamp to the array.
    if ($delta == $stamp_index && !empty($stamp)) {
      array_splice($build, $stamp_index, 0, $stamp);
    }
    unset($box);
  }
  unset($this->view->row_index);
  return $build;
}