Node properties are not retained

Created on 31 July 2023, over 1 year ago
Updated 19 August 2023, over 1 year ago

Problem/Motivation

In version 1.5 a node property is retained upon a scheduled publish, while in version 2.0.1 the node properties are lost.

Steps to reproduce

  1. Set a node property in a presave hook:
    $node->my_property = "my_value";
  2. In a postsave hook (this is using hook_post_action which implements hook_entity_insert) query the value of the node property:
    $my_value = $node->my_property;

In v1.5 the property is still available, in v2.0.1 it isn't.

Proposed resolution

Unsure if this should be supported? Or if I'm doing something wrong. Can do some more testing but thought it was worth checking if this was expected behaviour.

🐛 Bug report
Status

Closed: cannot reproduce

Version

2.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @gaddman
  • 🇬🇧United Kingdom jonathan1055

    Hi gaddman,
    We did make some changes to the "save" process when working on Scheduler 2.x, but I think this was because 1.x was doing too many saves, and doing them in the wrong places. It might be that the property you had was being saved as a side-effect of Scheduler 1.x doing the wrong thing.

    Here are some issues that might be related

    Can you give a bit more detail on the example you have, so that I can replicate it - is the property simply being created in a presave hook, or is it defined in a schema and just being set a value in the presave hook?

  • Thanks Jonathon,
    Still doing some debugging but perhaps this will help point in the right direction. To answer your question though, I'm creating the property in that presave hook, literally just copying a field value (defined in the schema) to a node property (not in schema):

    $node->my_property = $node->field_myfield->value;
    

    I mentioned a postsave hook earlier but the value has already been lost in the presave hook so I think it's safe to ignore that. Although I've included it just in case it may be relevant. And I may well be getting confused, but it seems as though the value of my node's field has been lost.

    In v1.5:
    On initial save:

    1. scheduler_node_presave: myfield=x
    2. mymodule_node_presave: published=false so do nothing (don't copy myfield to node->my_property)
    3. mymodule_node_postsave: published=false so do nothing

    On scheduled publish:

    1. scheduler_cron
    2. scheduler_node_presave: myfield=x
    3. mymodule_node_presave: published=true so copy myfield to node->my_property
    4. mymodule_node_postsave: published=true and node->my_property=x so do some stuff

    In v2.0.1:
    On initial save:

    1. mymodule_node_presave: published=false so do nothing (don't copy myfield to node->my_property)
    2. scheduler_entity_presave: myfield has no value!
    3. mymodule_node_postsave: published=false so do nothing

    On scheduled publish:

    1. scheduler_cron: myfield has no value
    2. mymodule_node_presave: published=true, myfield empty so nothing to copy to node->my_property
    3. scheduler_entity_presave: myfield has no value
    4. mymodule_node_postsave: published=true, node->my_property not present so do nothing

    Maybe some difference between the entity presave hook and the previous version's node presave hook? Or the order (scheduler module vs my module)?
    thanks a heap!
    chris

  • I've got some other funky stuff going on with form_alter hooks and trying to ensure my node_presave hook runs after the scheduler_presave hook, so I'll dig about with that. Most likely something I've done wrong here.

  • Status changed to Closed: cannot reproduce over 1 year ago
  • Found the problem. Version 1.5 uses a node presave hook and v2.0 uses an entity presave hook. My own node hook was originally getting called after the scheduler hook, and with the change to an entity hook it ended up being called before. That messed up my check for the published state, so changed how I do that and it's all fine now.

    Thanks @jonathan1055 for the help!

  • 🇬🇧United Kingdom jonathan1055

    Hey that's good, pleased you have solved it for your scenario. Do you think there is anything that should/could be done in Scheduler to avoid others having this problem?

  • Mmm, good question. If I generalise what tripped me up it'd be the following:

    1. Using a custom module with a node presave hook; and
    2. That custom module's hook must be run before or after Scheduler's presave hook.

    I'm guessing that's not particularly common, so I don't think it'd make sense to change any code for this scenario.

    Aside from that, some ideas from a documentation viewpoint that may have helped (I say these with no appreciation for what might be involved and also conscious that many people won't read it):

    • A highlight list of the major, and especially breaking, changes upgrading from 1.5 to 2.0.
    • Some guidance on choosing 1.5 vs 2.0 (besides core compatibility).
    • Renaming this issue to something more related to the cause than the symptom.
Production build 0.71.5 2024