I modified the meta-tag title for a view in hook_preprocess_html() using the following code.

if (isset($variables['head_title'])) {
    $variables['head_title'] = $variables['page']['#title'] . " | My web";

Is it possible to modify the description in the same hook?


That depends on your theme and its use of $variables in combination with the description meta tag.

A code snippet for Drupal 8 that does what you want for the Bartik Theme would be the following:

function bartik_page_attachments_alter(array &$page) {
  $description = array(
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => array(
      'name' => 'description',
      'content' => 'your description content',
  $page['#attached']['html_head'][] = [$description, 'description'];

This was inspired by this post => https://drupal.stackexchange.com/a/134145/57183

Maybe this does solve your problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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