View source
<?php
namespace Drupal\rng\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\rng\EventTypeInterface;
use Drupal\rng\EventManagerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\courier\Entity\CourierContext;
use Drupal\field\Entity\FieldConfig;
use Drupal\Core\Entity\Entity\EntityFormMode;
use Drupal\rng\RegistrantTypeInterface;
class EventType extends ConfigEntityBase implements EventTypeInterface {
protected $id;
protected $entity_type;
protected $bundle;
public $mirror_operation_to_event_manage;
public $custom_rules = TRUE;
protected $default_registrant;
protected $people_types = [];
var $fields = [
EventManagerInterface::FIELD_REGISTRATION_TYPE,
EventManagerInterface::FIELD_REGISTRATION_GROUPS,
EventManagerInterface::FIELD_STATUS,
EventManagerInterface::FIELD_CAPACITY,
EventManagerInterface::FIELD_EMAIL_REPLY_TO,
EventManagerInterface::FIELD_ALLOW_DUPLICATE_REGISTRANTS,
EventManagerInterface::FIELD_REGISTRATION_REGISTRANTS_MINIMUM,
EventManagerInterface::FIELD_REGISTRATION_REGISTRANTS_MAXIMUM,
];
function getEventEntityTypeId() {
return $this->entity_type;
}
function setEventEntityTypeId($entity_type) {
$this->entity_type = $entity_type;
return $this;
}
function getEventBundle() {
return $this->bundle;
}
function setEventBundle($bundle) {
$this->bundle = $bundle;
return $this;
}
function getEventManageOperation() {
return $this->mirror_operation_to_event_manage;
}
function setEventManageOperation($permission) {
$this->mirror_operation_to_event_manage = $permission;
return $this;
}
function getAllowCustomRules() {
return $this->custom_rules;
}
function setAllowCustomRules($allow) {
$this->custom_rules = $allow;
return $this;
}
function getDefaultRegistrantType() {
return $this->default_registrant;
}
public function canIdentityTypeCreate($entity_type, $bundle) {
$key = $this
->getIdentityTypeKey($entity_type, $bundle);
return !empty($this->people_types[$key]['create']);
}
public function setIdentityTypeCreate($entity_type, $bundle, $enabled) {
$key = $this
->getIdentityTypeKey($entity_type, $bundle);
$this->people_types[$key]['create'] = $enabled;
return $this;
}
public function getIdentityTypeEntityFormMode($entity_type, $bundle) {
$key = $this
->getIdentityTypeKey($entity_type, $bundle);
return !empty($this->people_types[$key]['entity_form_mode']);
}
public function getIdentityTypeEntityFormModes() {
$result = [];
foreach ($this->people_types as $people_type) {
$required_keys = [
'entity_type',
'bundle',
'entity_form_mode',
];
if (count($required_keys) === count(array_intersect_key(array_flip($required_keys), $people_type))) {
$entity_type = $people_type['entity_type'];
$bundle = $people_type['bundle'];
$result[$entity_type][$bundle] = $people_type['entity_form_mode'];
}
}
return $result;
}
public function setIdentityTypeEntityFormMode($entity_type, $bundle, $form_mode) {
$key = $this
->getIdentityTypeKey($entity_type, $bundle);
$this->people_types[$key]['entity_form_mode'] = $form_mode;
return $this;
}
public function canIdentityTypeReference($entity_type, $bundle) {
$key = $this
->getIdentityTypeKey($entity_type, $bundle);
return !empty($this->people_types[$key]['existing']);
}
public function setIdentityTypeReference($entity_type, $bundle, $enabled) {
$key = $this
->getIdentityTypeKey($entity_type, $bundle);
$this->people_types[$key]['existing'] = $enabled;
return $this;
}
protected function getIdentityTypeKey($entity_type, $bundle, $create_key = TRUE) {
if (isset($this->people_types)) {
$pairs = $this->people_types;
foreach ($pairs as $k => $pair) {
if ($pair['entity_type'] == $entity_type && $pair['bundle'] == $bundle) {
return $k;
}
}
}
if ($create_key) {
$next_key = count($this->people_types) + 1;
$this->people_types[$next_key] = [
'entity_type' => $entity_type,
'bundle' => $bundle,
];
return $next_key;
}
return FALSE;
}
function setDefaultRegistrantType($registrant_type_id) {
$this->default_registrant = $registrant_type_id;
return $this;
}
public function id() {
return $this
->getEventEntityTypeId() . '.' . $this
->getEventBundle();
}
static function courierContextCC($entity_type, $operation) {
$event_types = \Drupal::service('rng.event_manager')
->eventTypeWithEntityType($entity_type);
if (!count($event_types)) {
$courier_context = CourierContext::load('rng_registration_' . $entity_type);
if ($courier_context) {
if ($operation == 'delete') {
$courier_context
->delete();
}
}
else {
if ($operation == 'create') {
$entity_type_info = \Drupal::entityTypeManager()
->getDefinition($entity_type);
$courier_context = CourierContext::create([
'label' => t('Event (@entity_type): Registration', [
'@entity_type' => $entity_type_info
->getLabel(),
]),
'id' => 'rng_registration_' . $entity_type,
'tokens' => [
$entity_type,
'registration',
],
]);
$courier_context
->save();
}
}
}
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
if ($this
->isNew()) {
$this
->courierContextCC($this->entity_type, 'create');
}
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
$mode_id = $this->entity_type . '.rng_event';
if (!EntityFormMode::load($mode_id)) {
EntityFormMode::create([
'id' => $mode_id,
'targetEntityType' => $this->entity_type,
'label' => 'Event Settings',
'status' => TRUE,
])
->save();
}
if (!$update) {
module_load_include('inc', 'rng', 'rng.field.defaults');
foreach ($this->fields as $field) {
rng_add_event_field_storage($field, $this->entity_type);
rng_add_event_field_config($field, $this
->getEventEntityTypeId(), $this
->getEventBundle());
}
}
$display = entity_get_form_display($this->entity_type, $this->bundle, 'rng_event');
if ($display
->isNew()) {
foreach (array_keys($display
->getComponents()) as $name) {
if (!in_array($name, $this->fields)) {
$display
->removeComponent($name);
}
}
$field_weights = [
EventManagerInterface::FIELD_STATUS,
EventManagerInterface::FIELD_ALLOW_DUPLICATE_REGISTRANTS,
EventManagerInterface::FIELD_CAPACITY,
EventManagerInterface::FIELD_EMAIL_REPLY_TO,
EventManagerInterface::FIELD_REGISTRATION_TYPE,
EventManagerInterface::FIELD_REGISTRATION_GROUPS,
EventManagerInterface::FIELD_REGISTRATION_REGISTRANTS_MINIMUM,
EventManagerInterface::FIELD_REGISTRATION_REGISTRANTS_MAXIMUM,
];
module_load_include('inc', 'rng', 'rng.field.defaults');
foreach ($this->fields as $name) {
rng_add_event_form_display_defaults($display, $name);
if (in_array($name, $field_weights)) {
$component = $display
->getComponent($name);
$component['weight'] = array_search($name, $field_weights);
$display
->setComponent($name, $component);
}
}
$display
->save();
}
\Drupal::service('router.builder')
->setRebuildNeeded();
\Drupal::service('plugin.manager.menu.local_action')
->clearCachedDefinitions();
}
public function delete() {
foreach ($this->fields as $field) {
$field = FieldConfig::loadByName($this
->getEventEntityTypeId(), $this
->getEventBundle(), $field);
if ($field) {
$field
->delete();
}
$display = entity_get_form_display($this->entity_type, $this->bundle, 'rng_event');
if (!$display
->isNew()) {
$display
->delete();
}
}
parent::delete();
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
if ($event_type = reset($entities)) {
EventType::courierContextCC($event_type->entity_type, 'delete');
}
\Drupal::service('router.builder')
->setRebuildNeeded();
\Drupal::service('plugin.manager.menu.local_action')
->clearCachedDefinitions();
}
public function calculateDependencies() {
parent::calculateDependencies();
$entity_type = \Drupal::entityTypeManager()
->getDefinition($this
->getEventEntityTypeId());
if ($entity_type) {
$bundle_entity_type = $entity_type
->getBundleEntityType();
if ($bundle_entity_type && $bundle_entity_type !== 'bundle') {
$bundle = \Drupal::entityTypeManager()
->getStorage($entity_type
->getBundleEntityType())
->load($this
->getEventBundle());
if ($bundle) {
$this
->addDependency('config', $bundle
->getConfigDependencyName());
}
}
else {
$this
->addDependency('module', $entity_type
->getProvider());
}
}
$registrant_type_id = $this
->getDefaultRegistrantType();
if ($registrant_type_id) {
$registrant_type = RegistrantType::load($registrant_type_id);
if ($registrant_type) {
$this
->addDependency('config', $registrant_type
->getConfigDependencyName());
}
}
return $this;
}
public function onDependencyRemoval(array $dependencies) {
$changed = parent::onDependencyRemoval($dependencies);
foreach ($dependencies['config'] as $entity) {
if ($entity instanceof RegistrantTypeInterface) {
if ($entity
->id() === $this
->getDefaultRegistrantType()) {
$this
->setDefaultRegistrantType(NULL);
$changed = TRUE;
}
}
}
return $changed;
}
}