It would be great to store the original (before update) focal_point value to be able to use it during hook_entity_presave or hook_entity_update, like $entity and $entity->original for updated node.

For example:

  $field_updated = true;
  ...
  // Check if focal_point has been updated.
  $focal_point = $entity->get('field_image')->focal_point;
  if (!is_null($entity->get('field_image')->focal_point_original)) {
    $field_updated = $field_updated || ($focal_point !== $entity->get('field_image')->focal_point_original);
  }

Comments

lebster created an issue. See original summary.

ugolek’s picture

Status: Active » Needs review
StatusFileSize
new949 bytes

Patch is attached.

bleen’s picture

I think this is a nice enhancement ... I'd love to hear about your specific use-case

ugolek’s picture

Hi bleen, my case is pretty simple.

For example, let's call function update_metadata when entity has been update.

So, I use drupal_register_shutdown_function in hook_entity_update() to register a callback that should be triggered.

drupal_register_shutdown_function('update_metadata', $entity, $entity->original);

So, does not matter what update_metadata function should do, I want to update metadata only if the image has been updated, ie file id has changed. So, it is pretty simple to do, I just check $entity and $entity->original.

I also want to update metadata, if the focal point coordinates has been changed too, because it could be a different metadata after the manipulations with the same image. There is no way to catch the previous saved value from the $entity->original. So, the only way is adding a new property like focal_point_original, that keep the old value.

Does it make sense?

maosmurf’s picture

Hi all,

I'd love to hear about your specific use-case

Our use-case is: we invalidate images from CDN, but only if the focal_point has changed.
So using MODULE_entity_update (must use hook_entity_update because hook_ENTITY_TYPE_update would be too early, irregardless of module weight) we check

    if ($entity->get('field_image')->focal_point !== $entity->get('field_image')->focal_point_original) {
      // do invalidate...
    }

Thank you for the patch!

bleen’s picture

@maosmurf ... have you tested the patch and confirmed it works as advertised?

maosmurf’s picture

@bleen yes, we have it in production since last week - works as advertised.

bleen’s picture

Status: Needs review » Reviewed & tested by the community

based on #7

  • bleen committed 2ec5f39 on 8.x-1.x authored by lebster
    Issue #3102633 by lebster: Keep the original focal_point value to be...
bleen’s picture

Awesome ... thanks for this new feature

bleen’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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