function EntityField::createEntityForGroupBy

Same name in other branches
  1. 8.9.x core/modules/views/src/Plugin/views/field/EntityField.php \Drupal\views\Plugin\views\field\EntityField::createEntityForGroupBy()
  2. 10 core/modules/views/src/Plugin/views/field/EntityField.php \Drupal\views\Plugin\views\field\EntityField::createEntityForGroupBy()
  3. 11.x core/modules/views/src/Plugin/views/field/EntityField.php \Drupal\views\Plugin\views\field\EntityField::createEntityForGroupBy()

Creates a fake entity with grouped field values.

Parameters

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

\Drupal\views\ResultRow $row: The result row object containing the values.

Return value

bool|\Drupal\Core\Entity\FieldableEntityInterface Returns a new entity object containing the grouped field values.

1 call to EntityField::createEntityForGroupBy()
EntityField::getItems in core/modules/views/src/Plugin/views/field/EntityField.php
Gets an array of items for the field.

File

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

Class

EntityField
A field that displays entity field data.

Namespace

Drupal\views\Plugin\views\field

Code

protected function createEntityForGroupBy(EntityInterface $entity, ResultRow $row) {
    // Retrieve the correct translation object.
    $processed_entity = clone $this->getEntityFieldRenderer()
        ->getEntityTranslation($entity, $row);
    // Copy our group fields into the cloned entity. It is possible this will
    // cause some weirdness, but there is only so much we can hope to do.
    if (!empty($this->group_fields) && isset($entity->{$this->definition['field_name']})) {
        // first, test to see if we have a base value.
        $base_value = [];
        // Note: We would copy original values here, but it can cause problems.
        // For example, text fields store cached filtered values as 'safe_value'
        // which does not appear anywhere in the field definition so we cannot
        // affect it. Other side effects could happen similarly.
        $data = FALSE;
        foreach ($this->group_fields as $field_name => $column) {
            if (property_exists($row, $this->aliases[$column])) {
                $base_value[$field_name] = $row->{$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']} = [
                $base_value,
            ];
        }
        else {
            $processed_entity->{$this->definition['field_name']} = [];
        }
    }
    return $processed_entity;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.