Do you want to know how to Drupal?

Let's Drupal

How to edit an Entity in Drupal?

In order to edit an entity in Drupal firstly we need to load it. We can use the static method of specific entity class, in the current example it is Node class


$node = Node::load(1);


We can also use entity storage to load entities


// Load single entity
$entity = \Drupal::entityTypeManager()->getStorage('node')->load(1);

// Load multiple entities
$entities = \Drupal::entityTypeManager()->getStorage($entity_type)->loadMultiple([1, 2, 3]);


Then you can set necessary fields and properties of the entity


// Call the specific setter
$node->setTitle('new Title');

// Call general set method
$node->set('body', 'Body text');

// Save the entity