pm-4.1.x-dev/modules/pm_story/pm_story.module
modules/pm_story/pm_story.module
<?php
/**
* @file
* Provides a pm story entity type.
*/
use Drupal\Core\Render\Element;
use Drupal\user\UserInterface;
/**
* Implements hook_theme().
*/
function pm_story_theme() {
return [
'pm_story' => [
'render element' => 'elements',
],
];
}
/**
* Prepares variables for pm story templates.
*
* Default template: pm-story.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing
* the pm story information and any
* fields attached to the entity.
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_pm_story(array &$variables) {
$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 pm_story_user_cancel($edit, UserInterface $account, $method) {
switch ($method) {
case 'user_cancel_reassign':
// Anonymize pm stories.
$storage = \Drupal::entityTypeManager()->getStorage('pm_story');
$pm_story_ids = $storage->getQuery()
->condition('uid', $account->id())
->accessCheck(FALSE)
->execute();
foreach ($storage->loadMultiple($pm_story_ids) as $pm_story) {
$pm_story->setOwnerId(0);
$pm_story->save();
}
break;
}
}
/**
* Implements hook_ENTITY_TYPE_predelete() for user entities.
*/
function pm_story_user_predelete(UserInterface $account) {
// Delete pm stories.
$storage = \Drupal::entityTypeManager()->getStorage('pm_story');
$pm_story_ids = $storage->getQuery()
->condition('uid', $account->id())
->accessCheck(FALSE)
->execute();
$pm_stories = $storage->loadMultiple($pm_story_ids);
$storage->delete($pm_stories);
}
/**
* Implements hook_entity_base_field_info().
*/
function pm_story_entity_base_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type) {
$fields = [];
$parent_type = \Drupal::service('pm.config')
->getParentType($entity_type->id());
if ($parent_type == 'pm_story') {
// Capture a reverse entity reference,
// updated when parent entity gets updated.
$fields['pm_parent_' . $parent_type] = \Drupal\Core\Field\BaseFieldDefinition::create('entity_reference')
->setLabel(t('Parent Story'))
->setReadOnly(TRUE)
->setSetting('target_type', $parent_type)
->setSetting('handler', 'default')
->setDisplayOptions('view', [
'label' => 'above',
'weight' => -11,
])
->setDisplayConfigurable('view', TRUE)
->setCardinality(1);
}
return $fields;
}
