1

I have a template for a page content type that works perfect. But when I try to edit or delete the node of that content type it blocks the standard template.

So node/130/edit? or node/130/delete? are not working because I don't get the right page.

I tried strpos($_SERVER['REQUEST_URI']) versions but it doesn't work or like the one below only gives me the "original" Drupal template when the path is node/130/edit? so the second variable gets through.

function mytheme_theme_suggestions_page_alter(array &$suggestions, array $variables) {
      // Add content type suggestions.
      if (($node = \Drupal::request()->attributes->get('node')) && 
      (strpos($_SERVER['REQUEST_URI'], "delete") || strpos($_SERVER['REQUEST_URI'], "edit") === false)
      ) {
        array_splice($suggestions, 1, 0, 'page__node__' . $node->getType());
      }
    }

The array_splice hint comes from
https://www.drupal.org/forum/support/theme-development/2015-07-02/how-to-add-in-drupal-8-a-custom-suggestion-page-template#comment-10684366

Any help?

1

A slightly different approach: Add a custom theme suggestion for the canonical route of your node type(s).

This way, you neither have to meddle with path arguments whose position might change, e.g. when using subpath aliases in a later stage of your site's development cycle, nor have to extend your checks for additional routes added by third party modules, as e.g. node/{nid}/translate, node/{nid}/revisions, node/{nid}/devel, and so on.

I didn't test it though, but you could do something like this:

/**
 * Implements hook_theme_suggestions_HOOK_alter() for page.html.twig.
 */
function mytheme_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  if (
    $route_name = \Drupal::routeMatch()->getRouteName()
    && $route_name == 'entity.node.canonical'
    && $node = \Drupal::request()->attributes->get('node')
  ) {
    $suggestions[] = 'page__node__' . $node->getType() . '__canonical';
  }
}

Then you could rename your page template to page--node--my-type--canonical.html.twig and it should be picked up only for your node type my_type at its canonical (view) route.

  • This feels like the right track, but I get this error: Notice: Undefined variable: route_name in isutheme_theme_suggestions_page_alter() (line 55 of themes/isubit/isutheme/isutheme_theme.theme). – Sarah C Jul 9 at 14:30
  • I think I figured it out: I had to add $route_name = \Drupal::routeMatch()->getRouteName(); before and just outside the if – Sarah C Jul 9 at 16:52
0

You can work with path_args instead of $_SERVER['REQUEST_URI'].
Something like:

function mythem_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  // Add content type suggestions.
  // Get current path.
  $current_path = \Drupal::service('path.current')->getPath();
  // explode args.
  $path_args = explode('/', $current_path);
  if (($node = \Drupal::request()->attributes->get('node')) && isset($path_args[3]) && ($path_args[3] == 'edit' || $path_args[3] == 'delete')) {
    array_splice($suggestions, 1, 0, 'page__node__' . $node->getType());
  }
}

For more infos about how to get url args take a look at https://www.drupal.org/node/2274705

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.