9

I'm developing a module to update nodes(enable revision). It looks like this.

 $nodes = node_load_multiple(array(), array('type' => $bundleType));
 foreach ($ndoes as $node){
    $w = entity_metadata_wrapper('node',$node);
    $w->MyFields->set($someValue);
    $w->save();
 }

It set fields correctly but node's revision-id didn't change after $w->save().

How can I update the revision ID?

4
  • Is the create new revision flag set on the node for the content type? Have you tried like $w->revision->set(1); before saving? ....
    – tenken
    May 29, 2014 at 14:30
  • Yes, "create new revision" is on. I don't know much about $w->revision->set(1), is that a manual way to let the drupal knows that the node has been updated? I will try it.
    – zhuchun
    May 29, 2014 at 15:46
  • hmm, $w->revision->set() works. It seems I misunderstand $w->save(). I thought it should increase the revision-id automatically while saving. Is there anyway to get next increase revision id? Thank you
    – zhuchun
    May 29, 2014 at 16:05
  • no, revision->set(1) tells the node to record a new revision on the upcoming/current edit of the node. I'm not sure why the metadata wrapper isn't seeing the global setting of create new revision ..... but setting the revision flag is a very simple workaround as you've now seen. I'm provided my comment as an answer below if you care to upvote it to help other users in the future find a correct answer.
    – tenken
    May 29, 2014 at 16:09

4 Answers 4

6

Is the create new revision flag set on the node for the content type? Have you tried like $w->revision->set(1); before saving? ....

EDIT:

Author says setting $w->revision->set(1) solves his issue.

1
  • It's a easy workaround! Just don't understand why it's needed lol. Thank you!
    – zhuchun
    May 29, 2014 at 16:13
10

When you edit a node via the UI, it is done via node_form(). This has a call near the top to node_object_prepare(), which sets up default values for that node. This includes setting the default value of $node->revision, which is used by node_save() to determine whether a new revision is needed or not.

The Entity API is revision aware, but node_object_prepare() is a node-specific thing. This means that you need to manage the values yourself, including setting the revision property to mimic what the node edit form will do.

0
7

None of these answers are actually correct, doing ->set(1) makes it sound like your actually changing the revision number and therefore should increment it.

This is actually just a boolean flag so the correct answer is $node->revision->set(true);

As zhuchun says: he doesn't understand why its needed, when explained as a boolean flag it makes perfect sense rather than the number.

3
  • There was a time when creating new drupal content involved most easily calling drupal_submit_form for example. If you inspect the POST values for a node submit form you'll see things like revision = 1 in the POST fields because the form items create new revision box was checked. Since 1 == TRUE the code works either way. But, your point is valid.
    – tenken
    Jun 7, 2016 at 21:11
  • this should be accepted answer Oct 12, 2017 at 0:16
0

If the node type doesn't have revisions enabled by default, the most succinct way to request a new revision is:

$w->revision = TRUE;

which is exactly the same as using:

$w->revision->set(TRUE);

but with less typing required.

Your Answer

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

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