0

Actually twig only suggest templates files name like this:

<!-- FILE NAME SUGGESTIONS:
   * html--node--20.html.twig
   * html--node--%.html.twig
   * html--node.html.twig
   x html.html.twig
-->

But I would have 2 kinds of template suggestion (like on my D6 website): One based on page + content type : page--CONTENT-TYPE.html.twig And other based on nodes + content-type : node--CONTENT-TYPE.html.twig or page--node-CONTENT-TYPE.html.twig

Actually I have a function in my .theme file like this but nothing change about suggestions:

function starterpf_d8_2019_theme_suggestions_page_alter(&$suggestions, $variables) {
  $path = \Drupal::requestStack()->getCurrentRequest()->getPathInfo();
  if ($path != '/') {
    $path = trim($path, '/');
    $arg = str_replace(["/", '-'], ['_', '_'], $path);
    $suggestions[] = 'page__' . $arg;
  }
}

Code found here

Thanks for help

EDIT: after exploring the output HTML, I get the page suggestions like this:

<!-- FILE NAME SUGGESTIONS:
   x page--front.html.twig
   * page--node.html.twig
   * page.html.twig
-->

So, can I add different kind of suggestions with putting together part of your codes ? Like this:

function starterpf_d8_2019_theme_suggestions_page_alter(&$suggestions, $variables) {
  $path = \Drupal::requestStack()->getCurrentRequest()->getPathInfo();
  if ($path != '/') {
    $path = trim($path, '/');
    $arg = str_replace(["/", '-'], ['_', '_'], $path);
    $suggestions[] = 'page__' . $arg;
  }
  #Suggestions pour les nodes
   $node = \Drupal::routeMatch()->getParameter('node');
  if ($node instanceof \Drupal\node\NodeInterface) {
    $suggestions[] = 'page__' . $node->bundle();
    $suggestions[] = 'page__node_' . $node->bundle();
  }
  
  #Suggestions pour les types de contenus
   if ($node = \Drupal::routeMatch()->getParameter('node')) {
    $content_type = $node->bundle();
    $suggestions[] = 'page__'.$content_type;
  }
}

And if I only want to create a file like node--CONTENT-TYPE.html.twig instead of a page one ? Interesting in use of node--CONTENT-TYPE-nid.html.twig too. And what would be better to use ? Knowing that I include field datas inside template to theme easily them ? In some use cases I will need to theme depending taxonomy term, is this native with D8 ?

1
  • Explore the HTML output, there are dozens of templates and suggestions for every single URL, varying by context and content
    – Hudri
    Dec 20, 2018 at 13:22

3 Answers 3

1

Try this out:

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

hook_theme_suggestions_page_alter modifies suggestions for page.html.twig, if you want to change suggestions for html.html.twig you need to use hook_theme_suggestion_html_alter

function starterpf_d8_2019_theme_suggestions_html_alter(&$suggestions, $variables) {
  $path = \Drupal::requestStack()->getCurrentRequest()->getPathInfo();
  if ($path != '/') {
    $path = trim($path, '/');
    $arg = str_replace(["/", '-'], ['_', '_'], $path);
    $suggestions[] = 'html__' . $arg;
  }
}
0

Try this:

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function THEME_NAME_theme_suggestions_page_alter(array &$suggestions, array $variables) {

  if ($node = \Drupal::routeMatch()->getParameter('node')) {
    $content_type = $node->bundle();
    $suggestions[] = 'page__'.$content_type;
  }
}

Found here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.