5

I'm trying to work out how I can add my own template suggestions based on the set URL alias. I've found lots of information out there regarding Drupal 7, but very little for 8.

I know I can use hook_theme_suggestions_page_alter(array &$suggestions, array $variables).

Basically, I'm looking to convert this to Drupal 8.

$alias = drupal_get_path_alias($_GET['q']);
if ($alias != $_GET['q']) {
  $template_filename = 'page';
  // Break it down for each piece of the alias path.
  foreach (explode('/', $alias) as $path_part) {
    // Added the str_replace because path parts with - were not being picked up properly by the theme_hook_suggestions().
    $template_filename = $template_filename . '__' . str_replace("-", "_", $path_part);
    $variables['theme_hook_suggestions'][] = $template_filename;
  }
}
3

OK, I seem to have got it working.

function THEME_NAME_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  $current_path = \Drupal::service('path.current')->getPath();
  $result = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);

  $path_alias = trim($result, '/');
  $path_alias = str_replace('/', '-', $path_alias);

  $suggestions[] = 'page__path_alias__'.$path_alias;
}

Here you can see the suggestions showing in HTML comments

1
  • This works, but you should probably update $path_alias = str_replace('/', '-', $path_alias); to $path_alias = str_replace(['/','-'], '_', $path_alias); - just in case the path alias also has hyphens in it. I believe that everything has to be underscores as well, before it goes into the suggestions array. – Blue Waters Aug 10 '16 at 13:42
2

Adding to Daniel's answer. If you have a dash the path alias, such as "our-community", make sure you additionally convert the dash to an underscore, i.e. 'our_community'.

function THEME_NAME_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  $current_path = \Drupal::service('path.current')->getPath();
  $result = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);

  $path_alias = trim($result, '/');
  $path_alias = str_replace('/', '-', $path_alias);
  $path_alias = str_replace('-', '_', $path_alias);

  $suggestions[] = 'page__path_alias__'.$path_alias;
}
0

If your path continues, you can target more templates, so it's actually just small improvement of previous answers.

E.g your path is: "blog/article-title", you will have two suggestions:

  • page--path-alias--blog--article-title.html.twig
  • page--path-alias--blog.html.twig

    function THEME_NAME_theme_suggestions_page_alter(array &$suggestions, array $variables) {
      $current_path = \Drupal::service('path.current')->getPath();
      $result = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);
    
      $path_alias = trim($result, '/');
      $path_alias = explode("/", $path_alias);
      $suggestion = "page__path-alias";
      foreach ($path_alias as &$alias) {
         $suggestion .= $alias = "__" . str_replace('-', '_', $alias);
         $suggestions[] = $suggestion;
      }
    }
    

Not the answer you're looking for? Browse other questions tagged or ask your own question.