View source
<?php
namespace Drupal\rng\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\rng\Exception\InvalidEventException;
class Group extends ContentEntityBase implements GroupInterface {
public function getEvent() {
return $this
->get('event')->entity;
}
public function setEvent(ContentEntityInterface $entity = NULL) {
$this
->set('event', [
'entity' => $entity,
]);
return $this;
}
public function isUserGenerated() {
return $this
->getSource() === NULL;
}
public function getSource() {
return $this
->get('source')->value;
}
public function setSource($module = NULL) {
$this
->set('source', [
'value' => $module,
]);
return $this;
}
public function getDescription() {
return $this
->get('description')->value;
}
public function setDescription($description) {
$this
->set('description', [
'value' => $description,
]);
}
public function getDependentGroups() {
return $this->groups_dependent
->referencedEntities();
}
public function getConflictingGroups() {
return $this->groups_conflicting
->referencedEntities();
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('Group ID'))
->setDescription(t('The group ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The group UUID.'))
->setReadOnly(TRUE);
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The group language code.'));
$fields['event'] = BaseFieldDefinition::create('dynamic_entity_reference')
->setLabel(t('Event'))
->setDescription(t('The groups event, or leave empty for global.'))
->setReadOnly(TRUE);
$fields['source'] = BaseFieldDefinition::create('string')
->setLabel(t('Source module'))
->setDescription(t('The module which created this group. Or NULL if it is user created.'))
->setReadOnly(TRUE);
$fields['label'] = BaseFieldDefinition::create('string')
->setLabel(t('Label'))
->setDescription(t('Name of the group.'))
->setRequired(TRUE)
->setTranslatable(TRUE)
->setDefaultValue('')
->setSetting('max_length', 255)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 0,
])
->setDisplayConfigurable('form', TRUE);
$fields['description'] = BaseFieldDefinition::create('text_long')
->setLabel(t('Description'))
->setDescription(t('A description of the group.'))
->setTranslatable(TRUE)
->setDisplayOptions('form', [
'type' => 'text_textfield',
'weight' => 50,
])
->setDisplayConfigurable('form', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created on'))
->setDescription(t('The time that the group was created.'))
->setTranslatable(TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The last time the group was edited.'))
->setTranslatable(TRUE);
$fields['groups_dependent'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Dependent groups'))
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setDescription(t('Groups required for this group to be added to a registration.'))
->setSetting('target_type', 'registration_group')
->setSetting('handler', 'siblings')
->setDisplayOptions('form', [
'type' => 'options_select',
'weight' => 70,
])
->setDisplayConfigurable('form', TRUE);
$fields['groups_conflicting'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Conflicting groups'))
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setDescription(t('Groups cannot exist on a registration for this group to be added.'))
->setSetting('target_type', 'registration_group')
->setSetting('handler', 'siblings')
->setDisplayOptions('form', [
'type' => 'options_select',
'weight' => 75,
])
->setDisplayConfigurable('form', TRUE);
return $fields;
}
public static function preDelete(EntityStorageInterface $storage, array $entities) {
$event_manager = \Drupal::service('rng.event_manager');
foreach ($entities as $group) {
if ($event = $group
->getEvent()) {
try {
$event_meta = $event_manager
->getMeta($event);
$event_meta
->removeGroup($group
->id())
->save();
foreach ($event_meta
->getRegistrations() as $registration) {
$registration
->removeGroup($group
->id())
->save();
}
} catch (InvalidEventException $e) {
}
}
}
}
}