1

In Drupal 7, this was possible. (See How to hide page title based on node type using template_preprocess_page().)

function MYTHEME_preprocess_page(&$variables) {
  if (!empty($variables['node']) && $variables['node']->type == 'concert') {
    $variables['show_title'] = FALSE;
  }
}

What is the equivalent in Drupal 8?

5

OUTDATED - please use this solution instead ...

In D8 i used

function MYTHEME_preprocess_page(&$variables) {
    if (!empty($variables['node']) && $variables['node']->getType() == 'page') {
        $variables['title'] = '';
    }
}

You still find the "node" object but you can't simply call the "type". The variable is protected, so you have to get the value via the function getType(). To get an overview install Devel and print with Kint the methods. This should only be a simple help, but maybe someone can deeper explain the concepts. Is it better to unset $variables['title'] in some way then to use $variables['title'] = ''?

2

Similar to @joekers it was not working for me either and had to use theme_preprocess_page_title() here is the details. Put this in your MY_THEME.theme file or modify that function if it already exists.

function MYTHEME_preprocess_page_title(&$variables) {
    $node = \Drupal::routeMatch()->getParameter('node');
    if( $node->getType() == 'page' ) {
        $variables['title'] = "";
    }
}

To use it you might have to substitute the variable page with your content type machine name.

  • Thanks ... I used the page preprocess function in early D8 times - i'm shure the API has changed ... – Sachbearbeiter Feb 7 '17 at 10:31
  • 1
    One 'weird' thing I found is that if I have node revisions turned on and I enter a previous node revision page, the code suggested here has some undesired results. It will throw an exception like this: "Call to a member function getType() on string". It looks like that $node = \Drupal::routeMatch()->getParameter('node') will deliver something which is not a node. By doing kat($node) on the result of the above code, I will actually get the node ID, not the node object itself. Let me know if my excplanation isn't clear enough. Thank you. – Marcos Buarque Nov 8 '17 at 12:00
  • Have not tried it but you are probably right ... drupal.org/node/2495703 and follow to the duplicate issue ... its getting fixed – GiorgosK Nov 8 '17 at 14:52
1

Depending upon how your pages are set in Drupal 8 (say using Blocks or Panels), coding fixes may not be necessary at all.

If you are using Blocks for layout, say, go to: Admin > Structure > Block Layout

And select the Page Title block, then make your desired visibility/hidden settings.

Here's a look at doing this for hiding the page title for the front page and every user profile: Block Visibility Settings Detail

If you are using another method to display content (say Panels/Panels Everywhere), there's a similar process in Page Manager to do so.

0

For some reason this wasn't working for me in theme_preprocess_page() so I had to use theme_preprocess_page_title() as suggested by Twig debug.

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.