This repository has been archived by the owner on Feb 23, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created a model class to move entity hook functions out of .module file.
- Loading branch information
Showing
3 changed files
with
168 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
<?php | ||
|
||
namespace Drupal\rng; | ||
|
||
use Drupal\Core\Entity\EntityInterface; | ||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use Drupal\courier\Service\IdentityChannelManagerInterface; | ||
use Drupal\rng\Entity\Registrant; | ||
use Drupal\rng\Plugin\Action\CourierTemplateCollection; | ||
|
||
/** | ||
* Enforces RNG model relationships. | ||
*/ | ||
class RngModel { | ||
|
||
/** | ||
* Storage for registration entities. | ||
* | ||
* @var \Drupal\Core\Entity\EntityStorageInterface | ||
*/ | ||
protected $registrationStorage; | ||
|
||
/** | ||
* Storage for registrant entities. | ||
* | ||
* @var \Drupal\Core\Entity\EntityStorageInterface | ||
*/ | ||
protected $registrantStorage; | ||
|
||
/** | ||
* Storage for registration group entities. | ||
* | ||
* @var \Drupal\Core\Entity\EntityStorageInterface | ||
*/ | ||
protected $registrationGroupStorage; | ||
|
||
/** | ||
* Storage for RNG rule entities. | ||
* | ||
* @var \Drupal\Core\Entity\EntityStorageInterface | ||
*/ | ||
protected $ruleStorage; | ||
|
||
/** | ||
* The RNG event manager. | ||
* | ||
* @var \Drupal\rng\EventManagerInterface | ||
*/ | ||
protected $eventManager; | ||
|
||
/** | ||
* The identity channel manager. | ||
* | ||
* @var \Drupal\courier\Service\IdentityChannelManagerInterface | ||
*/ | ||
protected $identityChannelManager; | ||
|
||
/** | ||
* Constructs a new RegistrantFactory object. | ||
* | ||
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager | ||
* The entity type manager. | ||
* @param \Drupal\rng\EventManagerInterface $event_manager | ||
* The RNG event manager. | ||
* @param \Drupal\courier\Service\IdentityChannelManagerInterface $identity_channel_manager | ||
* The identity channel manager. | ||
*/ | ||
public function __construct(EntityTypeManagerInterface $entity_type_manager, EventManagerInterface $event_manager, IdentityChannelManagerInterface $identity_channel_manager) { | ||
$this->registrationStorage = $entity_type_manager->getStorage('registration'); | ||
$this->registrantStorage = $entity_type_manager->getStorage('registrant'); | ||
$this->registrationGroupStorage = $entity_type_manager->getStorage('registration_group'); | ||
$this->ruleStorage = $entity_type_manager->getStorage('rng_rule'); | ||
$this->eventManager = $event_manager; | ||
$this->identityChannelManager = $identity_channel_manager; | ||
} | ||
|
||
/** | ||
* React to Drupal `hook_entity_predelete` hook. | ||
* | ||
* @param \Drupal\Core\Entity\EntityInterface $entity | ||
* The entity object for the entity that is about to be deleted. | ||
* | ||
* @see hook_entity_predelete(); | ||
*/ | ||
public function hook_entity_predelete(EntityInterface $entity) { | ||
if (in_array($entity->getEntityType(), $this->identityChannelManager->getIdentityTypes())) { | ||
$this->deletePerson($entity); | ||
} | ||
|
||
if ($this->eventManager->isEvent($entity)) { | ||
$this->deleteRngEvent($entity); | ||
} | ||
|
||
if ($entity instanceof RuleComponentInterface) { | ||
$this->deleteRngRuleComponent($entity); | ||
} | ||
} | ||
|
||
/** | ||
* Delete related entities when a person entity. | ||
* | ||
* @param \Drupal\Core\Entity\EntityInterface $entity | ||
* An identity/person entity. | ||
*/ | ||
protected function deletePerson(EntityInterface $entity) { | ||
// Remove registrant references to this identity. | ||
$registrant_ids = Registrant::getRegistrantsIdsForIdentity($entity); | ||
foreach ($this->registrantStorage->loadMultiple($registrant_ids) as $registrant) { | ||
/** @var \Drupal\rng\RegistrantInterface $registrant */ | ||
$registrant->clearIdentity(); | ||
$registrant->save(); | ||
} | ||
} | ||
|
||
/** | ||
* Delete related entities when an event is deleted. | ||
* | ||
* @param \Drupal\Core\Entity\EntityInterface $entity | ||
* An RNG event entity. | ||
*/ | ||
protected function deleteRngEvent(EntityInterface $entity) { | ||
// Don't need to catch exception from getMeta(), it is already checked by | ||
// the calling method. | ||
$event_meta = $this->eventManager->getMeta($entity); | ||
|
||
// Delete registrations. | ||
$registrations = $event_meta->getRegistrations(); | ||
$this->registrationStorage | ||
->delete($registrations); | ||
|
||
// Delete groups. | ||
$groups = $event_meta->getGroups(); | ||
$this->registrationGroupStorage | ||
->delete($groups); | ||
|
||
// Delete rules. | ||
$rules = $event_meta->getRules(NULL, FALSE, NULL); | ||
$this->ruleStorage | ||
->delete($rules); | ||
} | ||
|
||
/** | ||
* Delete related entities when a rule component entity is deleted. | ||
* | ||
* @param \Drupal\rng\RuleComponentInterface $entity | ||
* An RNG rule component entity. | ||
*/ | ||
protected function deleteRngRuleComponent(RuleComponentInterface $entity) { | ||
// Delete a TemplateCollection if the entity is a component with | ||
// configuration for 'rng_courier_message'. | ||
if ('rng_courier_message' == $entity->getPluginId()) { | ||
$action = $entity->createInstance(); | ||
if ($action instanceof CourierTemplateCollection) { | ||
$template_collection = $action->getTemplateCollection(); | ||
if ($template_collection) { | ||
$template_collection->delete(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |