with a custom module, in drupal 8, how can I test whether I am on a node entity or not, when i browse to that page?
If I do this $node = \Drupal::routeMatch()->getParameter('node'); I get an error if i'm not on a node.
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communitywith a custom module, in drupal 8, how can I test whether I am on a node entity or not, when i browse to that page?
If I do this $node = \Drupal::routeMatch()->getParameter('node'); I get an error if i'm not on a node.
Getting the route parameter is not a problem, this returns NULL if the route parameter is not present. But you have to check the node is not NULL, before using it:
if ($node = \Drupal::routeMatch()->getParameter('node')) {
$value = $node->field_name->value;
}
You can do:
if(\Drupal::routeMatch()->getRouteName() == 'entity.node.canonical'){
//Do what you want.
}