This is related to RNG module, but not entirely dependent on it. I've written a module that creates nodes (it's intended to create multiple nodes at any given time). This is where the tricky part comes in... I need to modify the rng event settings of the nodes that are created. There are approximately 4 settings that need to be set. Thus far, the only option I have come up with is modifying the database with an insert/update...but to do this effectively, I need to know the node id of the nodes that were just created.
3 Answers
Assuming that you create a node programmatically as follows:
$node = Node::create([
'type' => 'article',
'title' => 'Node title',
]);
$node->save();
You can get the newly created Node ID with the $node->id()
method as the variable $node
is a referenced one.
I think the fastest way to do this is create a rule which emails you all the information about each node created.
The second fastest would be to create a view sorted by node id and filter by the author which I'm using is the same for each one of the programmatically created nodes.
The third is if you are creating these programmatically anyway, keep programming and use some combination of hook_node_insert() and hook_node_update.
The fourth is to steal the code from the Drupal project/hook_post_action https://www.drupal.org/project/hook_post_action
To get "node nid" from a node, you can use the "id()" function.
An example.
$node->id();
A complete example:
$nodenew= Drupal\node\Entity\Node::create([
'type' => 'article',
'title' => 'Node title',
]);
$nodenew->save();
//obtain the nid.
echo $nodenew->id();
If you want to get the complete node, you can use this code
$node= Node_load($nodenew->id());
//obtain the field title.
echo $node->get('title')->value;