Linked Questions

26
votes
7answers
58k 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 ...
4
votes
1answer
2k 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 ...
1
vote
1answer
713 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
2answers
689 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
vote
1answer
415 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 ...
-1
votes
3answers
320 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 ...
0
votes
1answer
154 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 ...
0
votes
1answer
73 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\...
0
votes
2answers
91 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://...
0
votes
0answers
46 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 ...