7

Without checking the URL I would like to know if the current page is a node page. We plan on using URL aliases so we would like to not rely on the '/node/*' structure.

Is there some page variable or something that will indicate a node page?

14
\Drupal::request()->attributes->get('node');

Taken from here: https://api.drupal.org/api/drupal/core!lib!Drupal.php/function/Drupal%3A%3Arequest/8

Now the docs say to never use this function, but I have used this to determine what kind of page I am on.

There is another function, but I am not sure it applies in your case:

https://api.drupal.org/api/drupal/core%21modules%21node%21node.module/function/node_is_page/8

At the very least, if you do not have an entity, you could mimic that in your code:

  $route_match = \Drupal::routeMatch();

  if ($route_match->getRouteName() == 'entity.node.canonical') {
    return true;
  }
  • 1
    The route name check is fine, but don't use the node attribute. That's also present on many other pages, like a node edit form, revisions overviews and so on. Someone could even mess with your head and make a /tricked/you/{node} route that's actually a user ;) – Berdir Feb 29 '16 at 19:32
  • I needed this plus $route_match->getRouteName() == 'entity.node.revision' to work on revisions as well. – leymannx Oct 8 '18 at 9:06
6

In your own module I think the recommended way is

$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
  // It's a node!
}
5

In the page template a variable for the node is already available, which you can use in twig.

To tell, if the current page is a node, you can check, if node exists:

page.html.twig

{% if node %}
  <h1>This is a node</h1>
{% endif %}

This is possible, because this code is in core:

core/includes/theme.inc:
function template_preprocess_page(&$variables) {
  ...
  if ($node = \Drupal::routeMatch()->getParameter('node')) {
    $variables['node'] = $node;
  }
}

This will work on /node/[id] and url aliases.

  • What theme do you find this on because i havent seen that in Bartik? – BrandenB171 Feb 29 '16 at 18:17
  • This is not a hook in a theme, this is part of the preprocess in core, see core/includes/theme.inc. – 4k4 Feb 29 '16 at 19:14
0

The current answers don't work for previews or revisions. But normally you'd like that piece of logic to be fired on previews or revisions as well. Especially on the theming level where you at best should see how the node will look like after it got saved and/or published.

So here's is a snippet that gets the current node no matter if you view the full node or its preview or its revision. This may be placed inside a helper function under MYMODULE/src/Utils to be accessed as is or to make it a service.

$node = FALSE;
$route_match = \Drupal::routeMatch();

if ($route_match->getRouteName() == 'entity.node.canonical') {
  $node = $route_match->getParameter('node');
}
elseif ($route_match->getRouteName() == 'entity.node.revision') {
  $revision_id = $route_match->getParameter('node_revision');
  $node = node_revision_load($revision_id);
}
elseif ($route_match->getRouteName() == 'entity.node.preview') {
  $node = $route_match->getParameter('node_preview');
}

return $node;

BTW the same applies for the node_is_page() function that currently doesn't work on revisions or previews. I already opened an issue and provided a patch on https://www.drupal.org/project/drupal/issues/3005029.

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.