0

I am trying to set different classes in the page-title.html.twig based on what drupal is actually showing. The first condition works in a node template, but not in the page-title.html. This is a pseudo-code of what I am trying to achieve:

{%
  set classes = [
    'title--h1',
    node.type.entity.label == 'Blogpost' ? 'ico ico--time',
    page.machine_name == 'about_me' ? 'ico ico--world',
    view.machine_name == 'blogpost_list' ? 'ico ico--target'
  ]
%}
0

Try adding this to your MYTHEME.theme. This will provide the machine name value to your page.html.twig template files.

MYTHEME.theme:

/**
 * Implements hook_preprocess_HOOK().
 */
function MYTHEME_preprocess_page($variables) {
  $variables['machine_name'] = $variables['page']['#type'];
}

Then in page.html.twig:

{%
  set classes = [
    'title--h1',
    machine_name == 'about_me' ? 'ico ico--world',
  ]
%}

{{ machine_name }}

For node.html.twig templates, use bundle to get the machine name:

{%
  set classes = [
    node.bundle == 'content__type' ? 'one-class' : 'another-class'
  ]
%}

You cannot access the bundle type in MYTHEME_preprocess_page_title() directly.

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.