0

My page content type contains the field field_subheading. How can I make this field available in the template page-title.html.twig? Below code doesn't work, probably because $variables['node'] isn't set. Also, I don't know, if it's correkt to retrieve the field value by $variables['node']->field_subheading->value;. Thanks!

/**
 * Implements hook_theme_preprocess_page_title().
 */
function mytheme_preprocess_page_title(&$variables) {
    $node = \Drupal::request()->attributes->get('node');
    if ($node && $node->bundle() == 'page') {
        if (isset($variables['node'])) {
            $variables['subheading'] = $variables['node']->field_subheading->value;
        }
    }
}

kk

1 Answer 1

1

Try this

function mytheme_preprocess_page_title(&$variables) {
  $node = \Drupal::request()->attributes->get('node');
  if ($node && $node->bundle() == 'page') {
      $variables['subheading'] = $node->field_subheading->value;
  }
}

And in your twig file use {{ subheading }}

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.