1

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?

closed as unclear what you're asking by Kevin, Pierre.Vriens, mradcliffe, leymannx, Alfred Armstrong Dec 5 '18 at 9:18

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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
0

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();
}

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