pm-4.1.x-dev/modules/pm_epic/pm_epic.module

modules/pm_epic/pm_epic.module
<?php

/**
 * @file
 * Provides a pm epic entity type.
 */

use Drupal\Core\Render\Element;
use Drupal\user\UserInterface;

/**
 * Implements hook_theme().
 */
function pm_epic_theme() {
  return [
    'pm_epic' => [
      'render element' => 'elements',
    ],
  ];
}

/**
 * Prepares variables for pm epic templates.
 *
 * Default template: pm-epic.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - elements: An associative array containing
 *     the pm epic information and any
 *     fields attached to the entity.
 *   - attributes: HTML attributes for the containing element.
 */
function template_preprocess_pm_epic(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_epic_user_cancel($edit, UserInterface $account, $method) {
  switch ($method) {
    case 'user_cancel_reassign':
      // Anonymize pm epics.
      $storage = \Drupal::entityTypeManager()->getStorage('pm_epic');
      $pm_epic_ids = $storage->getQuery()
        ->condition('uid', $account->id())
        ->accessCheck(FALSE)
        ->execute();
      foreach ($storage->loadMultiple($pm_epic_ids) as $pm_epic) {
        $pm_epic->setOwnerId(0);
        $pm_epic->save();
      }
      break;
  }
}

/**
 * Implements hook_ENTITY_TYPE_predelete() for user entities.
 */
function pm_epic_user_predelete(UserInterface $account) {
  // Delete pm epics.
  $storage = \Drupal::entityTypeManager()->getStorage('pm_epic');
  $pm_epic_ids = $storage->getQuery()
    ->condition('uid', $account->id())
    ->accessCheck(FALSE)
    ->execute();
  $pm_epics = $storage->loadMultiple($pm_epic_ids);
  $storage->delete($pm_epics);
}


/**
 * Implements hook_entity_base_field_info().
 */
function pm_epic_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_epic') {
    // 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 Epic'))
      ->setReadOnly(TRUE)
      ->setSetting('target_type', $parent_type)
      ->setSetting('handler', 'default')
      ->setDisplayOptions('view', [
        'label' => 'above',
        'weight' => -11,
      ])
      ->setDisplayConfigurable('view', TRUE)
      ->setCardinality(1);
  }
  return $fields;
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc