From 0157f2c0b8b6b7e5585f35fe6585f177c96faac8 Mon Sep 17 00:00:00 2001
From: dpi <pro@danielph.in>
Date: Wed, 24 Jun 2015 00:21:56 +0800
Subject: [PATCH] Identities are now disassociated from registrants when
 deleted.
Fixed documentation RegistrantInterface.
Added ClearIdentity method to Registrant.
Added method to get registrants for an identity.
Fixed #30
---
 rng.module                  | 17 +++++++++++++++++
 src/Entity/Registrant.php   | 18 ++++++++++++++++++
 src/RegistrantInterface.php | 23 +++++++++++++++++++++++
 3 files changed, 58 insertions(+)
diff --git a/rng.module b/rng.module
index 3934746..fe8bcff 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 92fd8c5..9fb6125 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 f49b4fa..24c7efd 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);
+
 }
-- 
GitLab