Skip to content
This repository has been archived by the owner on Feb 23, 2020. It is now read-only.

Commit

Permalink
Created a model class to move entity hook functions out of .module file.
Browse files Browse the repository at this point in the history
  • Loading branch information
dpi committed Dec 30, 2016
1 parent cfb4cb7 commit 9e4c96f
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 45 deletions.
48 changes: 3 additions & 45 deletions rng.module
Expand Up @@ -190,51 +190,9 @@ function _rng_entity_postsave(EntityInterface $entity) {
* Implements hook_entity_predelete().
*/
function rng_entity_predelete(EntityInterface $entity) {
/** @var \Drupal\courier\Service\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();
}
}

// Delete related entities when an event is deleted.
/** @var \Drupal\rng\EventManagerInterface $event_manager */
$event_manager = \Drupal::service('rng.event_manager');
try{
$event_meta = $event_manager->getMeta($entity);

// Delete registrations.
\Drupal::entityTypeManager()
->getStorage('registration')
->delete($event_meta->getRegistrations());

// Delete groups.
\Drupal::entityTypeManager()
->getStorage('registration_group')
->delete($event_meta->getGroups());

// Delete rules.
\Drupal::entityTypeManager()
->getStorage('rng_rule')
->delete($event_meta->getRules(NULL, FALSE, NULL));
}
// Not an event, thrown by getMeta().
catch (InvalidEventException $e) { }

if ($entity instanceof RuleComponentInterface) {
// Delete a TemplateCollection if the entity is a component with
// configuration for 'rng_courier_message'.
if ($entity->getPluginId() == 'rng_courier_message') {
$action = $entity->createInstance();
if (($action instanceof CourierTemplateCollection) && ($template_collection = $action->getTemplateCollection())) {
$template_collection->delete();
}
}
}
/** @var \Drupal\rng\RngModel $rng_model */
$rng_model = \Drupal::service('rng.entity.model');
$rng_model->hook_entity_predelete($entity);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions rng.services.yml
Expand Up @@ -6,6 +6,9 @@ services:
class: Drupal\rng\EventManager
arguments: ['@entity.manager']
parent: container.trait
rng.entity.model:
class: Drupal\rng\RngModel
arguments: ['@rng.event_manager', '@plugin.manager.identity_channel']
rng.registrant.factory:
class: Drupal\rng\RegistrantFactory
arguments: ['@entity_type.manager', '@rng.event_manager']
Expand Down
162 changes: 162 additions & 0 deletions src/RngModel.php
@@ -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();
}
}
}
}

}

0 comments on commit 9e4c96f

Please sign in to comment.