Share icon

Hey, Drupal 8 or Drupal 9 allow you override the template, so you can able to apply styling and structured the html in better way. So many of the templates override options are available in drupal and you can see here for page template, html twig template, region template, block template, node template etc etc

For example if you want to create the node template for specific content type you can override it my placing the template in your theme folder with proper naming conventions e,g node--[nodeid]--[viewmode].html.twig or node--[content-type].html.twig.

But the problem is you can't do similar with page template, if you want to do like that then you need to alter drupal hook and tell drupal to you want to use page template like node template override, below we can see how you will do that with just simple 4 line of code in your .theme file,

1) Navigate to your theme, say and open yourthemename.theme file.

2) put below code in that ( replace yourthemenname to your actual theme name )

/**
 * Implements hook_theme_suggestions_page_alter().
 */
function yourthemename_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  $node = \Drupal::routeMatch()->getParameter('node');
  if ($node instanceof \Drupal\node\NodeInterface) {
    $suggestions[] = 'page__' . $node->bundle();
  }
}

3) Now copy your default page.html.twig and rename according to your content type name like that , for example you have content type named as service then your template naming convention will be page--service.html.twig i.e page--[content-type].html.twig.

4) Now clear the cache and reload the content type page and see it will work.

If you found any issue with this article please comment below to solve the problem by learning and If you found it useful share and comment is also welcome, thanks for reading 

Add new comment

Restricted HTML