21

I want to update a node field's value when a node is updated.

Tried using the code mentioned here Programmatically updating a node Drupal 8

use Drupal\node\Entity\Node;

    function hello_world_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {

      $check = \Drupal::routeMatch()->getParameter('node');

      if ($check) {
      $node = Node::load($check->nid->value);
      $node->title->value = 'testing';     //set value for field
      $node->save();
      }
    }

but it did not work. I get a 500 error after saving the node. I also tried taking out the if statement and specifying a node id $node = Node::load(1);

I also tried using Update a node / Entity programmatically in Drupal 8

use Drupal\node\Entity\Node;


function hello_world_node_update(Drupal\node\NodeInterface $node) {

$node = Node::load(1);
$node->set("title", 'New value'); // also tried $node->setTitle('The new Title');
$node->save();

}

but no luck either.

In addition, I tried using different hooks entity_presave, entity_update, node_update, but did not make any difference.

3 Answers 3

31

You can try this code:

function MODULE_NAME_node_presave(Drupal\node\NodeInterface $node) {
  $node->setTitle('new Title');
  $node->set('body', 'this is body');
}
0
10

This is another variation with using the hook you originally tried to use.

I think the problem with your code is that you try to load a new instance of the node, but you should use the node that is provided as paramater $entity:

use Drupal\node\NodeInterface;

function hello_world_entity_presave(Drupal\Core\Entity\EntityInterface $entity)   {
  if ($entity instanceof NodeInterface) {
    $entity->title->value = 'testing';     //set value for field
    // $entity->save(); (not needed)
  }
}

Edit:

Added the use statement to the code.

10
  • 1
    just tried your code, but it's breaking the site, getting the classic The website encountered an unexpected error. Please try again later.
    – No Sssweat
    May 12, 2016 at 6:48
  • 1
    This code is only to demonstrate how to use the hook. You need the right use statements. You can find this out, if you look in watchdog (/admin/reports/dblog).
    – 4uk4
    May 12, 2016 at 6:56
  • 1
    adding use Drupal\node\NodeInterface; code no longer auto breaks the site, but after saving the node, I get the 500 error like I was getting in my question and the logs is not logging anything about it.
    – No Sssweat
    May 12, 2016 at 7:13
  • 1
    I only wanted to point at the problem: Don't load a new instance of the node, but use the one provided as function parameter. What error is behind 500? Please check your log files or switch on error logging.
    – 4uk4
    May 12, 2016 at 7:17
  • 1
    I clear the log, so I don't get confused with all the other previous errors, but after saving and getting the error multiple times log files are empty
    – No Sssweat
    May 12, 2016 at 7:20
4

Titles in Drupal 8 are not set like standard fields; they have their own function. Use $node->setTitle('New Title'); for a node or $entity->setLabel('New Title'); for a generic entity. See Node::setTitle for more info.

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.