8

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.

1
  • 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, 2017 at 23:06

4 Answers 4

10

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);
6
  • 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, 2017 at 11:40
  • @StevanRS I think that you need to explain better your question, and then let me know. Apr 20, 2017 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, 2017 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, 2017 at 12:01
  • 4
    Note that passing a true/false to $node->setPublished() is deprecated as of 8.3.x. Instead use $node->setPublished() to publish and $node->setUnpublished() to unpublish.
    – donut
    Jan 6, 2020 at 15:16
3

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

-2

you can un publish by setting status to 0

          $node = \Drupal\node\Entity\Node::load($nids);
          //set Unpublishing node
          $node->status = 0;
          //save to update node
          $node->save();
1
  • 1
    This is not good advice. You really should be using setPublished() instead of accessing the status property directly. The existing answers cover this adequately.
    – anonymous
    May 27, 2021 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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