pm-4.1.x-dev/modules/pm_feature/pm_feature.module
modules/pm_feature/pm_feature.module
<?php
/**
* @file
* Provides a pm feature entity type.
*/
use Drupal\Core\Render\Element;
use Drupal\user\UserInterface;
/**
* Implements hook_theme().
*/
function pm_feature_theme() {
return [
'pm_feature' => [
'render element' => 'elements',
],
];
}
/**
* Prepares variables for pm feature templates.
*
* Default template: pm-feature.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing
* the pm feature information and any
* fields attached to the entity.
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_pm_feature(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_feature_user_cancel($edit, UserInterface $account, $method) {
switch ($method) {
case 'user_cancel_reassign':
// Anonymize pm features.
$storage = \Drupal::entityTypeManager()->getStorage('pm_feature');
$pm_feature_ids = $storage->getQuery()
->condition('uid', $account->id())
->accessCheck(FALSE)
->execute();
foreach ($storage->loadMultiple($pm_feature_ids) as $pm_feature) {
$pm_feature->setOwnerId(0);
$pm_feature->save();
}
break;
}
}
/**
* Implements hook_ENTITY_TYPE_predelete() for user entities.
*/
function pm_feature_user_predelete(UserInterface $account) {
// Delete pm features.
$storage = \Drupal::entityTypeManager()->getStorage('pm_feature');
$pm_feature_ids = $storage->getQuery()
->condition('uid', $account->id())
->accessCheck(FALSE)
->execute();
$pm_features = $storage->loadMultiple($pm_feature_ids);
$storage->delete($pm_features);
}
/**
* Implements hook_entity_base_field_info().
*/
function pm_feature_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_feature') {
// 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 Feature'))
->setReadOnly(TRUE)
->setSetting('target_type', $parent_type)
->setSetting('handler', 'default')
->setDisplayOptions('view', [
'label' => 'above',
'weight' => -11,
])
->setDisplayConfigurable('view', TRUE)
->setCardinality(1);
}
return $fields;
}
