Skip to main content

Request a Drupal 8 node NID on page load

Member for

2 years 7 months
Submitted by admin on

This function does exactly what it says, it returns the current Drupal 8 node NID that is being requested by the user. Be careful where you call it if the page is not a node it can break the website.

/**
 * @return string
 */
function _access_node_id() {
  $node = \Drupal::routeMatch()->getParameter('node');
  return $node->id();
}