Skip to content
Snippets Groups Projects
Commit 0157f2c0 authored by dpi's avatar dpi
Browse files

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
parent 978dff42
No related branches found
No related tags found
No related merge requests found
......@@ -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
......@@ -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}
*/
......
......@@ -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);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment