1

I have migrated D7 date fields to D9 Smart Date (with recurring). During the migration, I've parsed the legacy repeat rules and populated them into the new D9 Smart Date field.

However, the repeating dates do not show up in Views unless I first manually edit and save the node (without making any changes).

The problem is that I have hundreds of nodes that need to be resaved and none of the progratic methods work. Only manually editing and saving is forcing the repeat rule to do what needs to be done to show up.

I've tried using Views Bulks Action's Save Node function. I've tried using the Save All module. I've tried creating a PHP script to load and save the node.

None of these work. My script is below. Any ideas why only manually saving works?

enter image description here

5
  • 1
    Node's generally won't do anything when saved if no values have changed.
    – Jaypan
    Dec 13, 2021 at 15:43
  • I just modified the script to change the sticky to true and it's still not working. So the node is saving but whatever the Smart Date field needs to do is only working when the node is saved manually. Is there a way to mimick saving the node manually through code? Not sure where the discrepancy is coming from. Dec 13, 2021 at 15:51
  • It's possible that the module has some functionality that is being performed on form save, rather than entity save. I'd go through the module and look at the form definition and/or any form alter hooks to see if they're doing something that way.
    – Jaypan
    Dec 13, 2021 at 16:07
  • Thank you. That makes sense. Is there a way to do a form save programmatically? Dec 13, 2021 at 16:10
  • In Drupal 7, you could use drupal_form_submit(). This change record (drupal.org/node/2121003) says that you can use \Drupal::formBuilder()->submitForm(). I don't know the mechanics of it though
    – Jaypan
    Dec 13, 2021 at 16:14

1 Answer 1

1

Any ideas why only manually saving works?

As Jaypan mentioned in the comments, this is because Drupal won't save the node if nothing has changed.

When you submit the form for an entity which has a created/changed date (i.e. implements EntityChangedInterface), it does this:

if ($entity instanceof EntityChangedInterface) {
  $entity->setChangedTime($this->time->getRequestTime());
}

Which changes a field value, forcing the entity to save.

You could use the same method to force a save in your code.

3
  • Thank you for the info @Clive. I was hoping it would be something simple like you suggested but when I change the sticky from false to true and resave the Smart Date is still not doing it's magic that occurs when manually saving. It was suggested that perhaps the module is doing something special that only occurs during a node form edit/save but I can't figure out how to save the form programmatically. Dec 13, 2021 at 16:40
  • There's an example here: drupal.stackexchange.com/questions/253009/… - problem is it's an entity form, so not exactly simple to populate the submission values. There will be another API method for that too, but forms are a bit tricky to work with programatically (from the outside at least). It might be less effort to look through the code in the smart date module and see if it's doing something unusual
    – Clive
    Dec 13, 2021 at 16:43
  • Yeah, you may just need to pull the code out of their module and put it in a hook_update_N() or make a drush script that does the same thing.
    – Jaypan
    Dec 13, 2021 at 16:47

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.