Linked Questions

27 votes
7 answers
61k views

How to programmatically get the NID of the current node

I've pored over this old thread on drupal.org and it just kind of makes my head spin around. Pulling down the path and trying to parse out the NID from within it? There has to be a better way. And ...
  • 9,087
4 votes
1 answer
3k views

How to load node object in node preview page?

In drupal 8, how do you load the node object in hook_preprocess_page() when on the node preview page? I had a bug come up where this standard way of loading the current node object was failing on the ...
  • 13.5k
1 vote
1 answer
879 views

Notice: Trying to get property 'nid' of non-object

In Drupal\rsvplist\Form\RSVPForm->buildForm(), I wrote this to get the current nid: $node = \Drupal::routeMatch()->getParameter('node'); $nid = $node->nid->value; I get this error: ...
1 vote
2 answers
2k views

Add custom access check to user page

I would like to add an additional access condition to viewing user pages like user/1. I looked at the following guidelines, but have not succeeded in coming up with a solution. https://www.drupal.org/...
-1 votes
3 answers
556 views

Adding metatags in hook_preproccess_node()

I want to add variable meta tags depending on the node (a picture and custom title). These custom variables are generated in hook_preprocess_node(). Is there a way to add the metatags inside this ...
  • 193
1 vote
1 answer
460 views

How do I stop the node from caching

Sometimes when I run below code in the hook_preprocess_menu() of a node, I am getting a previously loaded node instead of my current node. \Drupal::routeMatch()->getParameter('node'); What should ...
0 votes
1 answer
186 views

Insert a node field programmatically

I have a <div> containing a node (a Basic Page), which has a title, body and a header image. I do not want to display the header image inside that div, so I removed it from the block layout. I ...
  • 207
0 votes
2 answers
113 views

Node ID from hook_comment_insert [duplicate]

I need the ID of the node the inserted comment is attached to. This is what I have tried: function my-module_comment_insert(Drupal\Core\Entity\EntityInterface $entity) { // According to https://...
  • 103
0 votes
1 answer
86 views

Get the ID that for the node that has been just created

I am trying to get the node ID that I have just created. If I do the following code from this answer. $node = \Drupal::routeMatch()->getParameter('node'); if ($node instanceof \Drupal\node\...
  • 28
0 votes
0 answers
107 views

Access a page.html.twig template variable in another twig template using a preprocess function

Drupal core 8.8.3 Version Using a preprocessor function as below function subtheme_bs_preprocess_book_tree(&$variables) { $variables['page_id'] = code here to read the node.nid variable ...