13

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.

25

You can try this code:

function MODULE_NAME_node_presave(Drupal\node\NodeInterface $node) {
  $node->setTitle('new Title');
  $node->set('body', 'this is body');
}
  • Thanks this worked, if anyone else wants to add other possible variations, feel free to add another answer. – No Sssweat May 12 '16 at 4:02
  • 1
    @NoSssweat : am I right in assuming that the line with "body" in the code shown in this answer is not really needed to answer your question? And that this line rather illustrates how you can do similar things with (eg) body? Also, since your question was about a "field", could you use a similar line as the line with "body" for a custom field, with machine name (say) field_my_extra_field? Sorry for what ay seem as hijacking an answer to ask new questions, but just "showing code" in an answer (with no explanation at all) is not perceived as a good practise around here either, right? – Pierre.Vriens Aug 24 '16 at 7:38
  • 1
    @Pierre.Vriens I used title in my Q for simplicity. I figured if I knew how to change the title, it would be the same for any field, just change machine name. Turns out that this answer uses a specific method to change the title, which would not work for any other field, so it is actually important that he shows the method for the body. – No Sssweat Aug 24 '16 at 8:21
  • I cannot get this hook to fire. I edit a content type, click submit, but nothing. – Mr Pablo Nov 17 '17 at 12:07
7

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.

  • 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 '16 at 6:48
  • 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). – 4k4 May 12 '16 at 6:56
  • 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 '16 at 7:13
  • 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. – 4k4 May 12 '16 at 7:17
  • 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 '16 at 7:20
2

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, privacy policy and cookie policy

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