In SchedulerManager->publish():

  1. first the node is published and saved:
    $this->entityManager->getStorage('action')->load('node_publish_action')->getPlugin()->execute($node);
  2. and then saved again:
    $event->getNode()->save();

Once should be enough.

Also it seems in the first save preSave() is not called on the node object, only hook_presave.

Comments

gaele created an issue. See original summary.

jonathan1055’s picture

Thanks for raising this. The fragment of code in question is:

// Use the actions system to publish the node.
$this->entityManager->getStorage('action')->load('node_publish_action')->getPlugin()->execute($node);

// Invoke the event to tell Rules that Scheduler has published this node.
if ($this->moduleHandler->moduleExists('scheduler_rules_integration')) {
  _scheduler_rules_integration_dispatch_cron_event($node, 'publish');
 }

// Trigger the PUBLISH event so that modules can react after the node is
// published.
$event = new SchedulerEvent($node);
$dispatcher->dispatch(SchedulerEvents::PUBLISH, $event);
$event->getNode()->save();

In between the two lines you mention, we are allowing Rules to react, and then invoking our own PUBLISH event. Maybe one save might be enough - but not sure exactly where it should be. 3rd-party code which reacts to the Scheduler event can modify the $node object (I think) and that is the reason for the final save. Do you think it would work if the node_publish_action was not done before the Rules calls?

gaele’s picture

Do you think it would work if the node_publish_action was not done before the Rules calls?

Yes that's what I would prefer, just replace the node_publish_action with $node->setPublished(NODE_PUBLISHED);
It seems to work fine. But I don't use Rules.

aoturoa’s picture

StatusFileSize
new1.22 KB

This patch applies to drupal/scheduler:8.x-1.0

kishor_kolekar’s picture

StatusFileSize
new1.22 KB

This patch applies to drupal/scheduler:8.x-1.1

kishor_kolekar’s picture

kishor_kolekar’s picture

kishor_kolekar’s picture

kishor_kolekar’s picture

kishor_kolekar’s picture

kishor_kolekar’s picture

Status: Active » Needs review
StatusFileSize
new1.18 KB

Rerolling the patch from #4 against 8.x-1.x-dev branch.

Please review
Thanks

Status: Needs review » Needs work

The last submitted patch, 11: node-save-twice-2824038-11.patch, failed testing. View results

kishor_kolekar’s picture

StatusFileSize
new1.13 KB

Rerolling the patch from #4 against 8.x-1.x-dev branch.

hash6’s picture

I have applied the patch and https://www.drupal.org/files/issues/2019-10-11/node_save_twice-2824038-1... and it fixes the issue of "node saved twice".

gaele’s picture

Status: Needs work » Needs review
hash6’s picture

Status: Needs review » Reviewed & tested by the community
jonathan1055’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new2.41 KB
new1.1 KB

I did not like the idea of abandoning the standard actions system for publishing and unpublishing the nodes. So I've made a small change to re-use that method at the end, instead of the plain ->save(). See the interdiff file. I would like to know how to test the Actions system, to make sure we are doing the right thing here. I know that the current tests all pass, but I am not certain that this is actually covered in our tests. If anyone can help, please do, as I want to get this committed. Thanks.

  • jonathan1055 committed 81e2a0c on 8.x-1.x
    Issue #2824038 by kishor_kolekar, jonathan1055, aoturoa: Node is saved...
jonathan1055’s picture

Status: Needs review » Fixed

No further input since 8th Nov. Decided to commit this as I want to release Scheduler 8.x-1.2

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.