Programmatically updating a node Drupal 8

Public

Warning message

We recommend you to assign at least one category to this snippet so you could easily navigate it in the future. Edit your snippet here.

Get raw version
php
  1. use Drupal\node\Entity\Node;
  2.  
  3. $node = Node::load($nid);
  4. //set value for field
  5. $node->body->value = 'body';
  6. $node->body->format = 'full_html';
  7. //field tag
  8. $node->field_tags = [1];
  9. //field image
  10. $field_image = array(
  11. 'target_id' => $fileID,
  12. 'alt' => "My 'alt'",
  13. 'title' => "My 'title'",
  14. );
  15. $node->field_image = $field_image;
  16.  
  17. //save to update node
  18. $node->save();

Comments

visabhishek's picture

For node_load in D8

  1. $node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
nehal.rupani's picture

better to use Drupal\node\Entity\Node for loading node as it seems standard way to and safer then playing directly with storage.