subscriptions-2.0.x-dev/subscriptions.module
subscriptions.module
<?php
/**
* @file
* Module file for the basic Subscriptions framework.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\user\UserInterface;
/**
* Returns an array of all SubscriptionType types.
*
* This returns an array of all entity types declared in instances of
* SubscriptionType plugins defined in the `type` in the annotation.
*
* @return array
* An array of all SubscriptionType entity types.
*/
function _get_subscription_type_entity_types(): array {
// @todo This could possibly benefit from caching at some point since
// SubscriptionType definitions should not change in any way without
// a deployment.
$subscription_types = [];
// Get all SubscriptionType plugin definitions.
$subscription_type_plugins = \Drupal::service('plugin.manager.subscriptions.subscription_type')
->getDefinitions();
foreach ($subscription_type_plugins as $subscription_type) {
if (!empty($subscription_type) && isset($subscription_type['id'])) {
// The actual (string) subscription type is in $subscription_type['id'].
// Ex. 'taxonomy_term', 'node', 'node_type', etc.
$subscription_types[] = $subscription_type['id'];
}
}
return $subscription_types;
}
/**
* Implements hook_ENTITY_TYPE_delete().
*/
function subscriptions_user_delete(UserInterface $user) {
\Drupal::database()->delete('subscriptions_user')
->condition('uid', $user->id())
->execute();
\Drupal::database()->delete('subscriptions_last_sent')
->condition('uid', $user->id())
->execute();
}
/**
* Implements hook_user_cancel().
*/
function subscriptions_user_cancel($edit, UserInterface $account, $method) {
subscriptions_user_delete($account);
}
/**
* Implements hook_entity_delete().
*
* When an entity is deleted, we want to delete any Subscription and
* SubscriptionFilter entities that reference the deleted entity.
*
* NOTE: We cannot assume a typehint of EntityInterface for our $entity
* parameter because, for whatever reason (despite this directly contradicting
* the Drupal API docs :angel:), this hook is invoked and passed entity
* paramaters that are not an instance of \Drupal\Core\Entity\EntityInterface
* like (for one example) search_api's Server entity.
*/
function subscriptions_entity_delete($entity) {
if ($entity instanceof EntityInterface) {
$entity_type_id = $entity->getEntityTypeId();
if (in_array($entity_type_id, _get_subscription_type_entity_types())) {
$subscription_storage = \Drupal::entityTypeManager()->getStorage('subscription');
$subscription_query_builder = $subscription_storage->getQuery();
$subscription_filter_storage = \Drupal::entityTypeManager()->getStorage('subscription_filter');
$subscription_filter_query_builder = $subscription_filter_storage->getQuery();
// Delete Subscriptions to the specific entity being deleted.
$sids = $subscription_query_builder
->condition('type', $entity_type_id)
->condition('value', $entity->id())
->execute();
// Delete Subscription filters that might be appended to any
// subscription(s) to the deleted entity.
foreach ($sids as $sid) {
$subscription = $subscription_storage->load($sid);
/** @var \Drupal\Core\Entity\EntityInterface[] **/
$filters = $subscription->getFilters();
// $filters can return null, so we will explicitly check that it is the
// array we expect.
if (is_array($filters)) {
foreach ($filters as $filter) {
$filter->delete();
}
$subscription->delete();
}
}
// Delete SubscriptionFilters that directly reference the deleted entity.
$sfids = $subscription_filter_query_builder
->condition('type', $entity_type_id)
->condition('value', $entity->id())
->execute();
// Delete (parent) Subscriptions to the SubscriptionsFilter(s) that
// reference the deleted entity.
foreach ($sfids as $sfid) {
$subscription_filter = $subscription_filter_storage->load($sfid);
/** @var \Drupal\Core\Entity\EntityInterface **/
$subscription = $subscription->getSubscription();
$subscription->delete();
$subscription_filter->delete();
}
}
}
}
