function template_preprocess_views_view

Same name and namespace in other branches
  1. 9 core/modules/views/views.theme.inc \template_preprocess_views_view()
  2. 8.9.x core/modules/views/views.theme.inc \template_preprocess_views_view()
  3. 10 core/modules/views/views.theme.inc \template_preprocess_views_view()

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/views.theme.inc, line 23

Code

function template_preprocess_views_view(&$variables) {
    $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 views_ui_preprocess_views_view()
    // 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);
    }
    // contextual_preprocess() only works on render elements, and since this theme
    // hook is not for a render element, contextual_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 contextual_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.