organizer-1.0.x-dev/organizer.module
organizer.module
<?php
/**
* @file
* Primary module hooks for Organizer module.
*/
use Drupal\Core\Render\Element;
use Drupal\user\UserInterface;
use Drupal\organizer\OrganizerInterface;
use Drupal\Core\Entity\EntityInterface;
require_once __DIR__ . '/includes/organizer.query.inc';
/**
* Implements hook_theme().
*/
function organizer_theme(): array {
return [
'organizer' => ['render element' => 'elements'],
];
}
/**
* Prepares variables for organizer templates.
*
* Default template: organizer.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the organizer information and any
* fields attached to the entity.
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_organizer(array &$variables): void {
$variables['view_mode'] = $variables['elements']['#view_mode'];
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}
/**
* Implements hook_user_cancel().
*/
function organizer_user_cancel($edit, UserInterface $account, $method): void {
switch ($method) {
case 'user_cancel_block_unpublish':
// Unpublish organizers.
$storage = \Drupal::entityTypeManager()->getStorage('organizer');
$organizer_ids = $storage->getQuery()
->condition('uid', $account->id())
->condition('status', 1)
->accessCheck(FALSE)
->execute();
foreach ($storage->loadMultiple($organizer_ids) as $organizer) {
$organizer->set('status', FALSE)->save();
}
break;
case 'user_cancel_reassign':
// Anonymize organizers.
$storage = \Drupal::entityTypeManager()->getStorage('organizer');
$organizer_ids = $storage->getQuery()
->condition('uid', $account->id())
->accessCheck(FALSE)
->execute();
foreach ($storage->loadMultiple($organizer_ids) as $organizer) {
$organizer->setOwnerId(0)->save();
}
break;
}
}
/**
* Implements hook_ENTITY_TYPE_predelete() for user entities
*/
function organizer_user_predelete(UserInterface $account): void {
// Delete organizers that belong to this account.
$storage = \Drupal::entityTypeManager()->getStorage('organizer');
$organizer_ids = $storage->getQuery()
->condition('uid', $account->id())
->accessCheck(FALSE)
->execute();
$storage->delete(
$storage->loadMultiple($organizer_ids)
);
// Delete old revisions.
$organizer_ids = $storage->getQuery()
->allRevisions()
->condition('uid', $account->id())
->accessCheck(FALSE)
->execute();
foreach (array_keys($organizer_ids) as $revision_id) {
$storage->deleteRevision($revision_id);
}
}
/**
* Implements hook_ENTITY_TYPE_delete() for group relationships
*/
function organizer_group_relationship_delete(EntityInterface $entity): void {
// Delete the related entity as well.
if ($related_entity = $entity->getEntity()) {
if ($related_entity instanceof OrganizerInterface) {
$related_entity->delete();
}
}
}
