diff --git a/field_encrypt.install b/field_encrypt.install index 5cb2088808e4100be7bc45ee85ddd327a2a54103..a2a71b44880cc24b8c0c75916dc2c9bf2c254daa 100644 --- a/field_encrypt.install +++ b/field_encrypt.install @@ -28,6 +28,8 @@ function field_encrypt_uninstall(bool $is_syncing): void { /** * Implements hook_requirements(). + * + * Remove this function when the minimum Drupal version is 11.2. */ function field_encrypt_requirements(string $phase): array { $requirements = []; diff --git a/src/Entity/FieldEncryptEntityType.php b/src/Entity/FieldEncryptEntityType.php index ee015dcd00143184f9b90f7dbefbffeef589bb59..b7a7e90646da7143281f488ef7ce1a2c573ea3f2 100644 --- a/src/Entity/FieldEncryptEntityType.php +++ b/src/Entity/FieldEncryptEntityType.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Drupal\field_encrypt\Entity; +use Drupal\Component\Utility\DeprecationHelper; use Drupal\Core\Config\Entity\ConfigEntityBase; use Drupal\Core\Entity\Attribute\ConfigEntityType; use Drupal\Core\Entity\EntityStorageInterface; @@ -117,7 +118,15 @@ class FieldEncryptEntityType extends ConfigEntityBase { */ public function postSave(EntityStorageInterface $storage, $update = TRUE): void { parent::postSave($storage, $update); - if (!$update || $this->getBaseFields() !== $this->original->getBaseFields()) { + + $original_entity = DeprecationHelper::backwardsCompatibleCall( + \Drupal::VERSION, + '11.2', + fn () => $this->getOriginal(), + fn () => $this->original, + ); + + if (!$update || $this->getBaseFields() !== $original_entity->getBaseFields()) { self::queueEntityUpdates($this->id()); } // Update the field_encrypt module's state. diff --git a/src/ProcessEntities.php b/src/ProcessEntities.php index 9ddd530a2b0c1d647d2d3ff6c33cccf008632d22..2512d026db34e83c3344f68d0d21f66e26ec0336 100644 --- a/src/ProcessEntities.php +++ b/src/ProcessEntities.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Drupal\field_encrypt; +use Drupal\Component\Utility\DeprecationHelper; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Field\FieldItemListInterface; @@ -77,16 +78,22 @@ class ProcessEntities { } // The entity storage handler has clever logic to ensure that configurable - // fields are only saved if necessary. If entity->original is set we need + // fields are only saved if necessary. If entity->getOriginal() we need // to ensure the field values are the values in the database and not the // unencrypted values so that they are saved if necessary. This is // particularly important when a previously encrypted field is set to be // unencrypted. // @see \Drupal\Core\Entity\Sql\SqlContentEntityStorage::saveToDedicatedTables() // @see \Drupal\Core\Entity\ContentEntityStorageBase::hasFieldValueChanged() - if (isset($translated_entity->original) && $translated_entity->original instanceof ContentEntityInterface) { - if ($translated_entity->original->hasTranslation($language->getId())) { - $translated_original = $translated_entity->original->getTranslation($language->getId()); + $translated_original = DeprecationHelper::backwardsCompatibleCall( + \Drupal::VERSION, + '11.2', + fn () => $translated_entity->getOriginal(), + fn () => $translated_entity->original ?? NULL + ); + if ($translated_original instanceof ContentEntityInterface) { + if ($translated_original->hasTranslation($language->getId())) { + $translated_original = $translated_original->getTranslation($language->getId()); $this->setEncryptedFieldValues($translated_original, 'getUnencryptedPlaceholderValue'); } } @@ -118,9 +125,15 @@ class ProcessEntities { $translated_entity = $entity->getTranslation($language->getId()); $this->setEncryptedFieldValues($translated_entity); // Reverse the logic to encrypt the original entity in ::encryptEntity(). - if (isset($translated_entity->original) && $translated_entity->original instanceof ContentEntityInterface) { - if ($translated_entity->original->hasTranslation($language->getId())) { - $this->decryptEntity($translated_entity->original); + $translated_original = DeprecationHelper::backwardsCompatibleCall( + \Drupal::VERSION, + '11.2', + fn () => $translated_entity->getOriginal(), + fn () => $translated_entity->original ?? NULL + ); + if ($translated_original instanceof ContentEntityInterface) { + if ($translated_original->hasTranslation($language->getId())) { + $this->decryptEntity($translated_original); } } } diff --git a/tests/modules/field_encrypt_test/src/Hook/FieldEncryptTestHooks.php b/tests/modules/field_encrypt_test/src/Hook/FieldEncryptTestHooks.php index ea2288af4443910522cc0f4c244682c740d252e8..c63f404927d0f27a50a10ee075af007068ca65d2 100644 --- a/tests/modules/field_encrypt_test/src/Hook/FieldEncryptTestHooks.php +++ b/tests/modules/field_encrypt_test/src/Hook/FieldEncryptTestHooks.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Drupal\field_encrypt_test\Hook; +use Drupal\Component\Utility\DeprecationHelper; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeInterface; @@ -41,9 +42,17 @@ class FieldEncryptTestHooks { $this->messenger->addMessage( $this->t('Field encrypt test hook_entity_update: @label', ['@label' => $entity->label()]) ); - if (isset($entity->original)) { + $original_entity = DeprecationHelper::backwardsCompatibleCall( + \Drupal::VERSION, + '11.2', + fn () => $entity->getOriginal(), + fn () => $entity->original ?? NULL + ); + if (isset($original_entity)) { $this->messenger->addMessage( - $this->t('Field encrypt test hook_entity_update: Original label -> @label', ['@label' => $entity->original->label()]) + $this->t('Field encrypt test hook_entity_update: Original label -> @label', [ + '@label' => $original_entity->label(), + ]) ); } } @@ -55,11 +64,21 @@ class FieldEncryptTestHooks { #[Hook('node_update')] public function nodeUpdate(EntityInterface $entity): void { $this->messenger->addMessage( - $this->t('Field encrypt test hook_ENTITY_TYPE_update: @label', ['@label' => $entity->label()]) + $this->t('Field encrypt test hook_ENTITY_TYPE_update: @label', [ + '@label' => $entity->label(), + ]) + ); + $original_entity = DeprecationHelper::backwardsCompatibleCall( + \Drupal::VERSION, + '11.2', + fn () => $entity->getOriginal(), + fn () => $entity->original ?? NULL ); - if (isset($entity->original)) { + if (isset($original_entity)) { $this->messenger->addMessage( - $this->t('Field encrypt test hook_ENTITY_TYPE_update: Original label -> @label', ['@label' => $entity->original->label()]) + $this->t('Field encrypt test hook_ENTITY_TYPE_update: Original label -> @label', [ + '@label' => $original_entity->label(), + ]) ); } }