You are here

public function DisplayPluginBase::buildRenderable in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/display/DisplayPluginBase.php \Drupal\views\Plugin\views\display\DisplayPluginBase::buildRenderable()

Builds a renderable array of the view.

Note: This does not yet contain the executed view, but just the loaded view executable.

Parameters

array $args: (optional) Arguments of the view.

bool $cache: (optional) Specify FALSE in order to opt out of render caching.

Return value

array The render array of a view.

Overrides DisplayPluginInterface::buildRenderable

1 call to DisplayPluginBase::buildRenderable()
Embed::buildRenderable in core/modules/views/src/Plugin/views/display/Embed.php
Builds a renderable array of the view.
1 method overrides DisplayPluginBase::buildRenderable()
Embed::buildRenderable in core/modules/views/src/Plugin/views/display/Embed.php
Builds a renderable array of the view.

File

core/modules/views/src/Plugin/views/display/DisplayPluginBase.php, line 2391

Class

DisplayPluginBase
Base class for views display plugins.

Namespace

Drupal\views\Plugin\views\display

Code

public function buildRenderable(array $args = [], $cache = TRUE) {
  $this->view->element += [
    '#type' => 'view',
    '#name' => $this->view->storage
      ->id(),
    '#display_id' => $this->display['id'],
    '#arguments' => $args,
    '#embed' => FALSE,
    '#view' => $this->view,
    '#cache_properties' => [
      '#view_id',
      '#view_display_show_admin_links',
      '#view_display_plugin_id',
    ],
  ];

  // When something passes $cache = FALSE, they're asking us not to create our
  // own render cache for it. However, we still need to include certain pieces
  // of cacheability metadata (e.g.: cache contexts), so they can bubble up.
  // Thus, we add the cacheability metadata first, then modify / remove the
  // cache keys depending on the $cache argument.
  $this
    ->applyDisplayCacheabilityMetadata($this->view->element);
  if ($cache) {
    $this->view->element['#cache'] += [
      'keys' => [],
    ];

    // Places like \Drupal\views\ViewExecutable::setCurrentPage() set up an
    // additional cache context.
    $this->view->element['#cache']['keys'] = array_merge([
      'views',
      'display',
      $this->view->element['#name'],
      $this->view->element['#display_id'],
    ], $this->view->element['#cache']['keys']);

    // Add arguments to the cache key.
    if ($args) {
      $this->view->element['#cache']['keys'][] = 'args';
      $this->view->element['#cache']['keys'][] = implode(',', $args);
    }
  }
  else {

    // Remove the cache keys, to ensure render caching is not triggered. We
    // don't unset the other #cache values, to allow cacheability metadata to
    // still be bubbled.
    unset($this->view->element['#cache']['keys']);
  }
  return $this->view->element;
}