Drupal - Programmatically updating a node

You can try this code

<?php
use Drupal\node\Entity\Node;

$node = Node::load($nid);
//set value for field
$node->body->value = 'body';
$node->body->format = 'full_html';
//field tag
$node->field_tags = [1];
//field image
$field_image = array(
    'target_id' => $fileID,
    'alt' => "My 'alt'",
    'title' => "My 'title'",
);
$node->field_image = $field_image;

//save to update node
$node->save();

$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);

node_load is deprecated in Drupal 8.x, will be removed before Drupal 9.0. Use \Drupal\node\Entity\Node::load().

Ref https://api.drupal.org/api/drupal/core%21modules%21node%21node.module/function/node_load/8.3.x

Tags:

Nodes

8