2

I search for a node with field value client id and I need to update values. How do I do that in drupal 8? I search with the following code

$profileNode = \Drupal::entityTypeManager()
          ->getStorage('node')
          ->loadByProperties(['field_user_id' => $id]);

I get an array in response to this which corresponds to this. How do I update the node value with this?

1
  • This is a bit too broad of a question. There are many ways to update node values, which node values are you updating? The Working with the Entity API documentation page should help you. Additionally api.drupal.org is a good resource to lookup the loadByProperties method return value (if you are not using an IDE such as PHPStorm or Visual Studio Code that would show you).
    – mradcliffe
    Nov 20 '18 at 16:15
2

Assuming you response array will contain one node as suggested by the variable name $profileNode

here is a suggestion to make it :

$profileNode = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadByProperties(['field_user_id' => $id]);
// Get the node object.
$profileNodeObject = reset($profileNode);
if ($profileNodeObject) {
  // Make the changes we want : per example set the author to user 4.
  $profileNodeObject->setOwnerId(4);
  // Save the changes.
  $profileNodeObject->save();
}
1
  • Nice example, tested with application code, works well. Dec 14 '20 at 22:01

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