diff --git a/rng.module b/rng.module index 3934746a35d7e1376b14152d4207a368fc422f46..fe8bcfff6d1d2c6c5b0352e9142e64c4af237363 100644 --- a/rng.module +++ b/rng.module @@ -6,6 +6,7 @@ use Drupal\Core\Session\AccountInterface; use Drupal\Core\Access\AccessResult; use Drupal\Core\Form\FormStateInterface; use Drupal\rng\Entity\RuleSchedule; +use Drupal\rng\Entity\Registrant; /** * Implements hook_help(). @@ -79,3 +80,19 @@ function rng_cron() { ->execute(); $rule_scheduler_storage->delete($rule_scheduler_storage->loadMultiple($ids)); } + +/** + * Implements hook_entity_predelete(). + */ +function rng_entity_predelete(EntityInterface $entity) { + /** @var \Drupal\courier\IdentityChannelManagerInterface $icm */ + $icm = \Drupal::service('plugin.manager.identity_channel'); + if (in_array($entity->getEntityType(), $icm->getIdentityTypes())) { + // Remove registrant references to this identity. + $registrant_ids = Registrant::getRegistrantsIdsForIdentity($entity); + foreach (Registrant::loadMultiple($registrant_ids) as $registrant) { + $registrant->clearIdentity(); + $registrant->save(); + } + } +} \ No newline at end of file diff --git a/src/Entity/Registrant.php b/src/Entity/Registrant.php index 92fd8c58ee903a7f97618745c634a97ae47495b3..9fb6125db59c2a5f521d437633bdf46dc2b0616d 100644 --- a/src/Entity/Registrant.php +++ b/src/Entity/Registrant.php @@ -59,6 +59,14 @@ class Registrant extends ContentEntityBase implements RegistrantInterface { return $this; } + /** + * {@inheritdoc} + */ + public function clearIdentity() { + $this->identity->setValue(NULL); + return $this; + } + /** * {@inheritdoc} */ @@ -67,6 +75,16 @@ class Registrant extends ContentEntityBase implements RegistrantInterface { return $entity->getEntityTypeId() == $keys['entity_type'] && $entity->id() == $keys['entity_id']; } + /** + * {@inheritdoc} + */ + public static function getRegistrantsIdsForIdentity(EntityInterface $identity) { + return \Drupal::entityQuery('registrant') + ->condition('identity__target_type', $identity->getEntityTypeId(), '=') + ->condition('identity__target_id', $identity->id(), '=') + ->execute(); + } + /** * {@inheritdoc} */ diff --git a/src/RegistrantInterface.php b/src/RegistrantInterface.php index f49b4fa678fd831c3d7f794977d84d27d5cc7ccb..24c7efd43c1607b68ffb5d141a656e02c01b4c09 100644 --- a/src/RegistrantInterface.php +++ b/src/RegistrantInterface.php @@ -35,18 +35,41 @@ interface RegistrantInterface extends ContentEntityInterface { * Set associated identity. * * @param \Drupal\Core\Entity\EntityInterface $entity + * The identity to set. * * @return \Drupal\rng\RegistrantInterface * Returns registrant for chaining. */ public function setIdentity(EntityInterface $entity); + /** + * Removes identity associated with this registrant. + * + * @return \Drupal\rng\RegistrantInterface + * Returns registrant for chaining. + */ + public function clearIdentity(); + /** * Checks if the identity is the registrant. * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The identity to check is associated with this registrant. + * * @return boolean * Whether the identity is the registrant. */ public function hasIdentity(EntityInterface $entity); + /** + * Get registrants belonging to an identity. + * + * @param \Drupal\Core\Entity\EntityInterface $identity + * An identity entity. + * + * @return int[] + * An array of registrant entity IDs. + */ + public static function getRegistrantsIdsForIdentity(EntityInterface $identity); + }