-2

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.

1
  • 4
    Please provide the code example of how you are creating your nodes programmatically. However if by any chance you have $node->save(); at the end, you just grab the id as: $node->id(); aftet saving, Commented Nov 28, 2017 at 20:22

3 Answers 3

1

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.

0

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

0

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.