Skip to main content
1 of 3
Mrweiner
  • 2k
  • 4
  • 26
  • 49

If you want to alter html, generally you'll be working with hook_preprocess_hook or a twig template override. This is the case for views as well. In the case of views, the template that renders the title is views-view.html.twig.

So if you wanted to render the title with a b tag, you could do:

function my_module_preprocess_views_view(&$variables) {
  $existing_title = $variables['title'];
  $variables['title'] = [
    '#type' => 'html_tag',
    '#tag' => 'b',
    '#value' => $existing_title
  ];
}

However, it's frowned upon to use tags for presentation and preferred to use classes+css. If you just want to add a class to target, you could do a similar thing:

function my_module_preprocess_views_view(&$variables) {
  $existing_title = $variables['title'];
  $variables['title'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'views-title', // or whatever 
      ],
    ],
    '#value' => $existing_title
  ];
}

Both #container and #html_tag are render elements, and there are a lot available to you.

Of course, depending on what your theme and other modules are doing, the existing $variables['title'] might not just be a string, so you may need to adjust accordingly.

Mrweiner
  • 2k
  • 4
  • 26
  • 49