7

I'm using data from JSON to create new nodes or update fields on existing if node exist on my Drupal 8 website. So, when I use method to update nodes, I need to save in array node id of updated nodes and keys of fields with changed values in this node.

After update is finished I need to print table with all nodes of this content type where fields of updated nodes need to have different style (color, background, etc).

So if my old node have body = "body1" and description = "description 1" and new node have body = "body2"and description = "description 1" after update is finished i need to get output like this:

enter image description here

Here is an example:

$node = \Drupal\node\Entity\Node::load($nid);
$node->set("body", 'New body text');
$node->set("description", 'New value');
$node->save();

Any suggestion?

Note: when I use $node->save(); if all updated values is same nothing is changed (date of node in content from admin panel is same as before update), but if values is different I need to store node id, keys and values only of changed fields in array.

  • Not really clear when and how you want to have that output, but as suggested by one answer, if you use the diff module and save a new revision, then you get a diff between the old and new values for any change that you ever made (assuming you stored it as a new revision) – Berdir Apr 20 '17 at 23:06
7

To set the status you can use the setPublished() method:

public function Node::setPublished

Sets the published status of a node..

Parameters

bool $published: TRUE to set this node to published, FALSE to set it to unpublished.

In your case:

$node = \Drupal\node\Entity\Node::load($nid);
$node->set("body", 'New body text');
$node->set("field_name", 'New value');
$node->setPublished(TRUE);
  • Status is ok, I have this in my code. I need something different, for example when I get data from JSON, some fields have same values, i just need to know which field have different value from previous. – StevanRS Apr 20 '17 at 11:40
  • @StevanRS I think that you need to explain better your question, and then let me know. – Adrian Cid Almaguer Apr 20 '17 at 11:48
  • The node status in Drupal refers to its published state. So, without clarification, this is the answer. Another way to look for a node that has been updated without looking at published state is to look at its changed property, the time it was last modified. – Kevin Apr 20 '17 at 11:57
  • @Adrian Cid Almaguer when i use $node->save(); if all updated values is same nothing is changed (date of node in content from admin panel is same as before update), but if values is different i need to store node id, keys and values only of changed fields in array. – StevanRS Apr 20 '17 at 12:01
  • @StevanRS can you edit your question and add an example please? I can't get the point – Adrian Cid Almaguer Apr 20 '17 at 13:21
2

Do you save a new revision of the updated node? Maybe you can use the diff module to see the changes between the current & previous node? This is not an in depth solution, but maybe could be useful pointer for you.

0

Don't forget the node->save() or your node changes will be ignored.

$node = \Drupal\node\Entity\Node::load($nid);
$node->set("body", 'New body text');
$node->set("field_name", 'New value');
$node->setPublished(TRUE);
$node->save();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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