You are here

protected function EntityField::prepareItemsByDelta in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views/src/Plugin/views/field/EntityField.php \Drupal\views\Plugin\views\field\EntityField::prepareItemsByDelta()

Adapts the $items according to the delta configuration.

This selects displayed deltas, reorders items, and takes offsets into account.

Parameters

array $all_values: The items for individual rendering.

Return value

array The manipulated items.

2 calls to EntityField::prepareItemsByDelta()
EntityField::renderItems in core/modules/views/src/Plugin/views/field/EntityField.php
Render all items in this field together.
FieldTestEntityField::executePrepareItemsByDelta in core/modules/views/tests/src/Unit/Plugin/field/FieldTest.php

File

core/modules/views/src/Plugin/views/field/EntityField.php, line 743

Class

EntityField
A field that displays entity field data.

Namespace

Drupal\views\Plugin\views\field

Code

protected function prepareItemsByDelta(array $all_values) {
  if ($this->options['delta_reversed']) {
    $all_values = array_reverse($all_values);
  }

  // We are supposed to show only certain deltas.
  if ($this->limit_values) {
    $row = $this->view->result[$this->view->row_index];

    // Offset is calculated differently when row grouping for a field is not
    // enabled. Since there are multiple rows, delta needs to be taken into
    // account, so that different values are shown per row.
    if (!$this->options['group_rows'] && isset($this->aliases['delta']) && isset($row->{$this->aliases['delta']})) {
      $delta_limit = 1;
      $offset = $row->{$this->aliases['delta']};
    }
    elseif (!$this->options['group_rows'] && !$this->multiple) {
      $delta_limit = 1;
      $offset = 0;
    }
    else {
      $delta_limit = $this->options['delta_limit'];
      $offset = intval($this->options['delta_offset']);

      // We should only get here in this case if there is an offset, and in
      // that case we are limiting to all values after the offset.
      if ($delta_limit === 0) {
        $delta_limit = count($all_values) - $offset;
      }
    }

    // Determine if only the first and last values should be shown.
    $delta_first_last = $this->options['delta_first_last'];
    $new_values = [];
    for ($i = 0; $i < $delta_limit; $i++) {
      $new_delta = $offset + $i;
      if (isset($all_values[$new_delta])) {

        // If first-last option was selected, only use the first and last
        // values.
        if (!$delta_first_last || $new_delta == $offset || $new_delta == $delta_limit + $offset - 1) {
          $new_values[] = $all_values[$new_delta];
        }
      }
    }
    $all_values = $new_values;
  }
  return $all_values;
}