Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Update a node / Entity programmatically in Drupal 8

By editor , 5 December, 2015
Question

How to update a node or an Entity programmatically in Drupal 8 ?
Update title, Update body text or other custom field;

Update a node:

Example 1 : Update title
$node = \Drupal\node\Entity\Node::load($nid);
$node->setTitle('The new Title')
$node->save();

Example 2 : Update a field ('body' and 'field_name')
$node = \Drupal\node\Entity\Node::load($nid);
$node->set("body", 'New body text');
$node->set("field_name", 'New value');
$node->save();

 

To make changes after click on save / edit button, you can use the hook_entity_presave.
hook_entity_presave(Drupal\Core\Entity\EntityInterface $entity);
Example:
function CUSTOM_MODULE_node_presave(Drupal\node\NodeInterface $node) {
  $node->setTitle('Edited Title');
  $node->set('body', 'this is the bew body');
  //CAUTION : Do not save here, because it's automatic.
}

Note : Do not re save inside of hook_entity_presave().

Using HOOK_entity_presave()

function mymodule_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
  if ($entity->getEntityType()->id() == 'node') {
    $entity->setTitle('The new Title');
    //CAUTION : Do not save here, because it's automatic.
  }
}

 

Tags

  • Drupal 8
  • Code
  • Node
  • Add new comment
Create a node programmatically in Drupal 8
Handle database schemas on Drupal 8

Comments5

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Brown Thumb (not verified)

10 years 9 months ago

Type O

How to update a node or (not oe) thank you for this site it is very helful

  • Reply

editor

10 years 4 months ago

In reply to Type O by Brown Thumb (not verified)

Thankyou

Thankyou

  • Reply

Duane (not verified)

9 years 8 months ago

kudos

Many thanks for posting this! Just read the Drupal 8 docs for this hook (https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/function/hook_entity_presave/8.2.x) and this would probably be most people's next logical question.

  • Reply

antrecu (not verified)

9 years 6 months ago

Thank you!

but can you also explain how to update just the value of a field without saving the whole entity? meaning an alternative to field_attach_update in D7, deprecated on D8

http://www.drupalcontrib.org/api/drupal/drupal!core!modules!field!field.attach.inc/function/field_attach_update/8

  • Reply

H b (not verified)

8 years 8 months ago

you have raised an vital…

you have raised an vital difficulty..thank you for sharing..I would love to examine extra contemporary affairs from this blog..preserve posting.. voyance qualité

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer