While creating node programmatically, 2 revisions are getting created as a draft. Am i missing anything here while using node::create?

I tried this code
$node->setNewRevision();
$node->isDefaultRevision();
$node->save();

It creates 2 revisions though I remove first two line of code.

When I create node through CMS, it creates only one draft revision.

Thanks

CommentFileSizeAuthor
revision_snap.PNG6.8 KBdinesh_kesarkar

Comments

dinesh_kesarkar created an issue. See original summary.

joseph.olstad’s picture

you shouldn't have to set a new revision when creating nodes programmatically unless there's a specific reason to, what are you trying to do?

joseph.olstad’s picture

remove these two lines from your script or module code:

$node->setNewRevision();
$node->isDefaultRevision();
Paramvir Dalal’s picture

In short we have to save data from a web form, which has English section and French section, which all gets submitted in one go, and gets saved in a node partly in english translation and partly in the french translation of it, (saving it twice once for each translation).(Create New Revision is enabled for this node, in the configuration itself.)
When we are done saving the content, everything works fine till we publish the content, (every save happens twice, once for each translation)

Post publishing is where the problem is, we need to initiate the change workflow, we try to programmatically change the moderation state to draft, but it gets changed only for English revision, while the French revision state remains as published. below is the code.

$node = $node_storage->load($nid);
//setting some english fields here
$node->moderation_state->value = 'draft';
$node->save();

$node_fr = $node->getTranslation('fr');
//setting some french fields here
$node_fr->moderation_state->value = 'draft';
$node_fr->save();

Adding the below 2 lines of code before each save, seem to make the above work OK.
$node->setNewRevision(TRUE);
$node->setDefaultRevision(FALSE);

Any ideas, what is going on?

joseph.olstad’s picture

This might help:

          $node->setSyncing(TRUE);
          $node->setRevisionTranslationAffected(TRUE);

if that isn't sufficient, save the english node, then register a shutdown function to add the french translation

drupal_register_shutdown_function('Drupal\mymodule\MyHelperClass::addNodeTranslation', $node, $langcode, 'draft');

in your addNodeTranslation function you'll add the translation, setRevisionTranslationAffected if you want the same revision id

joseph.olstad’s picture

I'm not sure why you're adding a revision, just let the save do the work?

However since you're having so much fun
if you want to debug a shutdown function, you will need to write to a log file or log to watchdog, nothing will display to the screen

The shutdown call runs after the english is saved to the database

You might not need the shutdown call, try the two boolean settings first

Paramvir Dalal’s picture

Thanks very much for the advice.
$node->setSyncing(TRUE);
$node->setRevisionTranslationAffected(TRUE);
The above boolean settings are not helping.

Even what we think is working -
$node->setNewRevision(TRUE);
$node->setDefaultRevision(FALSE);
does not seem to be truly consistent, not 100%.

The french translation does not seem to be changing its state inline with the english (default) translation, whatever we do.
Even when we are publishing the node, we have to publish the fr node separately.

Isn't there a way to apply moderation_state change to all the translations in one go?

sylus’s picture

Status: Active » Closed (outdated)