function EntityFieldRenderer::render

Same name in other branches
  1. 9 core/modules/views/src/Entity/Render/EntityFieldRenderer.php \Drupal\views\Entity\Render\EntityFieldRenderer::render()
  2. 8.9.x core/modules/views/src/Entity/Render/EntityFieldRenderer.php \Drupal\views\Entity\Render\EntityFieldRenderer::render()
  3. 11.x core/modules/views/src/Entity/Render/EntityFieldRenderer.php \Drupal\views\Entity\Render\EntityFieldRenderer::render()

Renders entity field data.

Parameters

\Drupal\views\ResultRow $row: A single row of the query result.

\Drupal\views\Plugin\views\field\EntityField $field: (optional) A field to be rendered.

Return value

array A renderable array for the entity data contained in the result row.

Overrides RendererBase::render

File

core/modules/views/src/Entity/Render/EntityFieldRenderer.php, line 138

Class

EntityFieldRenderer
Renders entity fields.

Namespace

Drupal\views\Entity\Render

Code

public function render(ResultRow $row, ?EntityField $field = NULL) {
    // The method is called for each field in each result row. In order to
    // leverage multiple-entity building of formatter output, we build the
    // render arrays for all fields in all rows on the first call.
    if (!isset($this->build)) {
        $this->build = $this->buildFields($this->view->result);
    }
    if (isset($field)) {
        $field_id = $field->options['id'];
        // Pick the render array for the row / field we are being asked to render,
        // and remove it from $this->build to free memory as we progress.
        if (isset($this->build[$row->index][$field_id])) {
            $build = $this->build[$row->index][$field_id];
            unset($this->build[$row->index][$field_id]);
        }
        elseif (isset($this->build[$row->index])) {
            // In the uncommon case where a field gets rendered several times
            // (typically through direct Views API calls), the pre-computed render
            // array was removed by the unset() above. We have to manually rebuild
            // the render array for the row.
            $build = $this->buildFields([
                $row,
            ])[$row->index][$field_id];
        }
        else {
            // In case the relationship is optional, there might not be any fields
            // to render for this row.
            $build = [];
        }
    }
    else {
        // Same logic as above, in the case where we are being called for a whole
        // row.
        if (isset($this->build[$row->index])) {
            $build = $this->build[$row->index];
            unset($this->build[$row->index]);
        }
        else {
            $build = $this->buildFields([
                $row,
            ])[$row->index];
        }
    }
    return $build;
}

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