You are here

protected function Fields::renderFields in Layout Plugin Views 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/views/row/Fields.php \Drupal\layout_plugin_views\Plugin\views\row\Fields::renderFields()

Renders the fields.

Parameters

\Drupal\views\ResultRow $row:

\Drupal\views\Plugin\views\field\FieldPluginBase[] $fieldsToRender:

Return value

\Drupal\Component\Render\MarkupInterface

Throws

NoMarkupGeneratedException

1 call to Fields::renderFields()
Fields::renderFieldsIntoRegions in src/Plugin/views/row/Fields.php
Renders the row's fields into the regions specified in the region map.

File

src/Plugin/views/row/Fields.php, line 167

Class

Fields
The layout_plugin_views 'fields' row plugin

Namespace

Drupal\layout_plugin_views\Plugin\views\row

Code

protected function renderFields(ResultRow $row, array $fieldsToRender) {

  // We have to override the available fields for rendering so we create a
  // backup of the original fields.
  $original_fields = $this
    ->getViewFieldDefinitions();
  $this
    ->setViewFieldDefinitions($fieldsToRender);

  // We can not just return a render array with a clone of a filtered view
  // because views assigns the view object just before rendering, which
  // results in all fields being rendered in each region.
  // We therefore have to force rendering outside of the render context of
  // this request.
  $renderer = $this
    ->getRenderer();
  $markup = $renderer
    ->executeInRenderContext(new RenderContext(), function () use ($row, $renderer) {

    // @codeCoverageIgnoreStart
    // We can never reach this code in our unit tests because we mocked out
    // the renderer. These two methods are however defined and tested by core.
    // There is no need for them to be tested by our unit tests.
    $render_array = parent::render($row);
    return $renderer
      ->render($render_array);

    // @codeCoverageIgnoreEnd
  });

  // Restore the original fields.
  $this
    ->setViewFieldDefinitions($original_fields);
  if (empty($markup)) {
    throw new NoMarkupGeneratedException();
  }
  return $markup;
}