function ViewsThemeHooks::preprocessViewsView

Prepares variables for view templates.

Default template: views-view.html.twig.

Parameters

array $variables: An associative array containing:

  • view: The ViewExecutable object.

File

core/modules/views/src/Hook/ViewsThemeHooks.php, line 241

Class

ViewsThemeHooks
Hook implementations for views.

Namespace

Drupal\views\Hook

Code

public function preprocessViewsView(array &$variables) : void {
  $view = $variables['view'];
  $id = $view->storage
    ->id();
  $variables['css_name'] = Html::cleanCssIdentifier($id);
  $variables['id'] = $id;
  $variables['display_id'] = $view->current_display;
  // Override the title to be empty by default. For example, if viewing a page
  // view, 'title' will already be populated in $variables. This can still be
  // overridden to use a title when needed. See
  // \Drupal\views_ui\Hook\ViewsUiThemeHooks::preprocessViewsView() for an
  // example of this.
  $variables['title'] = '';
  $css_class = $view->display_handler
    ->getOption('css_class');
  if (!empty($css_class)) {
    $sanitized_classes = array_map('\\Drupal\\Component\\Utility\\Html::cleanCssIdentifier', explode(' ', $css_class));
    // Merge the view display classes into any existing classes if they exist.
    $variables['attributes']['class'] = !empty($variables['attributes']['class']) ? array_merge($variables['attributes']['class'], $sanitized_classes) : $sanitized_classes;
    $variables['css_class'] = implode(' ', $sanitized_classes);
  }
  // \Drupal\contextual\Hook\ContextualThemeHooks::preprocess() only works on
  // render elements, and since this theme hook is not for a render element,
  // \Drupal\contextual\Hook\ContextualThemeHooks::preprocess() falls back to
  // the first argument and checks if that is a render element. The first
  // element is view_array. However, view_array does not get set anywhere, but
  // since we do have access to the View object, we can also access the View
  // object's element, which is a render element that does have
  // #contextual_links set if the display supports it. Doing this allows
  // \Drupal\contextual\Hook\ContextualThemeHooks::preprocess() to access this
  // theme hook's render element, and therefore allows this template to have
  // contextual links.
  // @see views_theme()
  $variables['view_array'] = $variables['view']->element;
  // Attachments are always updated with the outer view, never by themselves,
  // so they do not have dom ids.
  if (empty($view->is_attachment)) {
    // Our JavaScript needs to have some means to find the HTML belonging to
    // this view.
    //
    // It is true that the DIV wrapper has classes denoting the name of the
    // view/ and its display ID, but this is not enough to unequivocally match
    // a view with its HTML, because one view may appear several times on the
    // page. So we set up a hash with the current time, $dom_id, to issue a
    // "unique" identifier for each view. This identifier is written to both
    // drupalSettings and the DIV wrapper.
    $variables['dom_id'] = $view->dom_id;
  }
}

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