function Field::process_entity

Process an entity before using it for rendering.

Replaces values with aggregated values if aggregation is enabled. Takes delta settings into account (@todo remove in #1758616).

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity to be processed.

Return value

TRUE if the processing completed successfully, otherwise FALSE.

1 call to Field::process_entity()
Field::getItems in drupal/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php
Return an array of items for the field.

File

drupal/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php, line 749
Definition of Drupal\field\Plugin\views\field\Field.

Class

Field
A field that displays fieldapi fields.

Namespace

Drupal\field\Plugin\views\field

Code

function process_entity(EntityInterface $entity) {
  $processed_entity = clone $entity;
  $langcode = $this
    ->field_langcode($processed_entity);

  // If we are grouping, copy our group fields into the cloned entity.
  // It's possible this will cause some weirdness, but there's only
  // so much we can hope to do.
  if (!empty($this->group_fields)) {

    // first, test to see if we have a base value.
    $base_value = array();

    // Note: We would copy original values here, but it can cause problems.
    // For example, text fields store cached filtered values as
    // 'safe_value' which doesn't appear anywhere in the field definition
    // so we can't affect it. Other side effects could happen similarly.
    $data = FALSE;
    foreach ($this->group_fields as $field_name => $column) {
      if (property_exists($values, $this->aliases[$column])) {
        $base_value[$field_name] = $values->{$this->aliases[$column]};
        if (isset($base_value[$field_name])) {
          $data = TRUE;
        }
      }
    }

    // If any of our aggregated fields have data, fake it:
    if ($data) {

      // Now, overwrite the original value with our aggregated value.
      // This overwrites it so there is always just one entry.
      $processed_entity->{$this->definition['field_name']}[$langcode] = array(
        $base_value,
      );
    }
    else {
      $processed_entity->{$this->definition['field_name']}[$langcode] = array();
    }
  }

  // The field we are trying to display doesn't exist on this entity.
  if (!isset($processed_entity->{$this->definition['field_name']})) {
    return FALSE;
  }

  // We are supposed to show only certain deltas.
  if ($this->limit_values && !empty($processed_entity->{$this->definition['field_name']})) {
    $all_values = !empty($processed_entity->{$this->definition['field_name']}[$langcode]) ? $processed_entity->{$this->definition['field_name']}[$langcode] : array();
    if ($this->options['delta_reversed']) {
      $all_values = array_reverse($all_values);
    }

    // Offset is calculated differently when row grouping for a field is
    // not enabled. Since there are multiple rows, the 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($values->{$this->aliases['delta']})) {
      $delta_limit = 1;
      $offset = $values->{$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's an offset, and
      // in that case we're limiting to all values after the offset.
      if ($delta_limit == 'all') {
        $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 = array();
    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];
        }
      }
    }
    $processed_entity->{$this->definition['field_name']}[$langcode] = $new_values;
  }
  return $processed_entity;
}