Set created and changed timestamp values (Groups and Items)

Created on 21 February 2025, about 2 months ago

Problem/Motivation

Groups:
Now that we have changed timestamp on the groups, it seems reasonable to set it to the existing created timestamp value as that is the default behavior when creating a new entity.

Group items:
Now that we have created and changed timestamps, the existing group items will not get updates on the changed timestamp without a created timestamp.

It seems reasonable here to base a group items initial timestamps based on the group created timestamp.

Proposed resolution

Come up with a plan, either to leave old entities as is, or update the ones without the proper values.

Ideally we could do this in an update hook, but for those comfortable with Drush, below is a snippet that could be executed by Drush from a custom module for an example:

/**
 * Post process to set created and changed times.
 */
function MY_MODULE_migration_group_post_save() {
  $group_storage = \Drupal::entityTypeManager()->getStorage('commerce_vado_group');
  $groups = $group_storage->loadMultiple();

  /** @var \Drupal\commerce_vado\Entity\VadoGroupInterface $group */
  foreach ($groups as $group) {
    $timestamp = $group->getCreatedTime();
    if ($timestamp && $group->getChangedTime() == NULL) {
      $group->setChangedTime($timestamp);
      $group->save();
    }
    /** @var \Drupal\commerce_vado\Entity\VadoGroupItemInterface $group_item */
    foreach ($group->getItems() as $group_item) {
      if ($timestamp && $group_item->getCreatedTime() == NULL) {
        $group_item->setCreatedTime($timestamp);
        $group_item->setChangedTime($timestamp);
        $group_item->save();
      }
    }
  }

}

You could then run this via Drush:
drush ev "MY_MODULE_migration_group_post_save();"

🌱 Plan
Status

Active

Version

3.0

Component

Documentation

Created by

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

Comments & Activities

Production build 0.71.5 2024