To change a view title, you can implement hook_preprocess_views_view()
, which is what the Views UI module does, with views_ui_preprocess_views_view()
.
// Render title for the admin preview.
if (!empty($view->live_preview)) {
$variables['title'] = [
'#markup' => $view->getTitle(),
];
}
Using that code avoids the HTML markup used in the title is escaped/sanitized.
When the View UI module is installed, and you want to set the same view title in the the admin preview, the hook implemented by your module needs to be invoked after the one implemented by the View UI module. Keep in mind that views_ui_preprocess_views_view()
uses also the following code.
if (!empty($view->live_preview) && \Drupal::moduleHandler()->moduleExists('contextual')) {
$view->setShowAdminLinks(FALSE);
foreach (['title', 'header', 'exposed', 'rows', 'pager', 'more', 'footer', 'empty', 'attachment_after', 'attachment_before'] as $section) {
if (!empty($variables[$section])) {
$variables[$section] = [
'#theme' => 'views_ui_view_preview_section',
'#view' => $view,
'#section' => $section,
'#content' => $variables[$section],
'#theme_wrappers' => [
'views_ui_container',
],
'#attributes' => [
'class' => [
'contextual-region',
],
],
];
}
}
}
As side note, the default views-view.html.twig template used by the Views module used two template variables more: title_prefix
and title_suffix
.
{{ title_prefix }}
{{ title }}
{{ title_suffix }}