0

I am using Drupal 8. I want to add Prefix and suffix to view title. I tired with following code but in result html is not rendering.

function Hook_preprocess_page_title(&$variables) {
$variables['title_prefix'] ='<div class="row heading-section"><div class="col-md-9 el-col-md-middle">
        <div class="row">';
$variables['title_suffix'] ='</div></div></div>';

} 

Result

<div class="row heading-section"><div class="col-md-9 el-col-md-middle"> <div class="row">My Title</div></div></div>
3
  • Is your function really called Hook_preprocess_page_title? If yes, you will have to replace the Hook part by either the name of your theme or the name of the module where you placed the function. Don't forget to clear the caches after you change hook names or add new hooks. This applies to all hook functions btw. Jan 25, 2018 at 15:38
  • @MarioSteinitz I have already replace Hook with my theme name
    – amol
    Jan 25, 2018 at 15:45
  • Ok, just liked to make sure. - See my answer. Jan 25, 2018 at 16:03

1 Answer 1

4

The variables title_prefix and title_suffix will be rendered using Twig, and therefore filtered from markup and potentially harmful code.

If you like to use HTML within Twig variables, you need to use an instance of the Markup class:

use Drupal\Core\Render\Markup;

/**
 * Implements hook_preprocess_HOOK() for page_title.
 */
function mytheme_preprocess_page_title(array &$variables) {
  $variables['title_prefix']['#markup'] = Markup::create('<div class="row heading-section"><div class="col-md-9 el-col-md-middle"><div class="row">');
  $variables['title_suffix']['#markup'] = Markup::create('</div></div></div>');
}

PS.: The above hook would add the markup to all titles. If this really is your intention, consider altering the page-title.html.twig template instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.