$entity->original gets stale between updates

Created on 20 November 2013, over 11 years ago
Updated 12 April 2023, about 2 years ago

In contrib it is common for workflows to be implemented by resaving the entity in a hook_entity_update or a matching Rule.
So if my commerce order needs to go through 4 different statuses, and can't skip them by just doing hook_entity_presave(), it is going to do do
a status change and a save as the last hook_entity_update operation / rule.
However, D7 / D8 / Commerce / Entity API have the following code in their entity controllers:

if (!$entity->isNew() && !isset($entity->original)) {

This prevents $entity->original from being reloaded, so when I react on a fourth status change, $entity->original->status is still pointing at the first one.
Ideally workflows like this would be handled in events / hooks outside of the save pipeline, but Drupal is not currently discouraging this practice.
So, the controller's need to stop doing the isset check (it's premature optimization anyway), and then the concrete bug is fixed.

D7 test code:

$node = entity_create('node', array('type' => 'page'));
$node->title = 'Drupal 6 rocks!';
node_save($node);
$node->title = 'Drupal 7 rocks!';
node_save($node);

/**
 * Implements hook_node_update().
 */
function mymodule_node_update($node) {
  // The node has been created with the title 'Drupal 6 rocks!'.
  // Then we update it to say "Drupal 7 rocks!".
  // This hook catches that update, and changes it to "Drupal 8 rocks!".
  // This is a simple example that could be replaced with a presave() hook,
  // but in the case of workflow changes, every status needs to be cycled
  // through, no skipping allowed.
  if ($node->title == 'Drupal 7 rocks!') {
    $node->title = 'Drupal 8 rocks!';
    node_save($node);
  }
  // On second update, check the value of $node->original.
  elseif ($node->title == 'Drupal 8 rocks!') {
    if ($node->original->title == 'Drupal 7 rocks!') {
      dpm('The node has the correct original value');
    }
    elseif ($node->original->title == 'Drupal 6 rocks!') {
      dpm('The node has an incorrect original value');
    }
  }
}
πŸ› Bug report
Status

Postponed: needs info

Version

10.1 ✨

Component
EntityΒ  β†’

Last updated about 9 hours ago

Created by

πŸ‡·πŸ‡ΈSerbia bojanz

Live updates comments and jobs are added and updated live.
  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024