View source
<?php
namespace Drupal\opigno_calendar_event\Entity;
use Drupal\opigno_calendar_event\CalendarEventInterface;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EditorialContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\entity\BundleFieldDefinition;
class CalendarEvent extends EditorialContentEntityBase implements CalendarEventInterface {
protected static $date_field_types = [];
protected $dateFieldName;
public function getOwnerId() {
return $this
->get('uid')->target_id;
}
public function getDateItems() {
if (!isset($this->dateFieldName)) {
$type = static::getCalendarEventType($this
->bundle());
$this->dateFieldName = static::getDateFieldName($type);
}
return $this
->get($this->dateFieldName);
}
public static function getDateFieldName(ConfigEntityInterface $type) {
return 'date_' . static::getDateFieldType($type);
}
protected static function getDateFieldType(ConfigEntityInterface $type) {
$date_field_type =& static::$date_field_types[$type
->id()];
if (!isset($date_field_type)) {
$date_field_type = $type
->get('date_field_type');
if (!$date_field_type) {
$date_field_type = 'timestamp';
\Drupal::logger('opigno_calendar_event')
->critical('No date type defined for bundle "@bundle".', [
'@bundle' => $type
->id(),
]);
}
}
return $date_field_type;
}
protected static function getCalendarEventType($type_id) {
try {
$type = \Drupal::entityTypeManager()
->getStorage('opigno_calendar_event_type')
->load($type_id);
} catch (InvalidPluginDefinitionException $e) {
throw new \RuntimeException($e
->getMessage(), $e
->getCode(), $e);
}
return $type;
}
public function isDisplayed() {
return (bool) $this
->get('displayed')->value;
}
public function setDisplayed($displayed) {
$this
->set('displayed', $displayed);
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(new TranslatableMarkup('Title'))
->setRequired(TRUE)
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE);
$fields['description'] = BaseFieldDefinition::create('text_long')
->setLabel(t('Description'))
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'text_default',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('form', [
'type' => 'text_textfield',
'weight' => 0,
])
->setDisplayConfigurable('form', TRUE);
$fields['displayed'] = BaseFieldDefinition::create('boolean')
->setLabel(new TranslatableMarkup('Show on calendar'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDefaultValue(TRUE)
->setDisplayConfigurable('form', TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(new TranslatableMarkup('Authored by'))
->setDescription(new TranslatableMarkup('The username of the calendar event author.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setDefaultValueCallback('Drupal\\opigno_calendar_event\\Entity\\CalendarEvent::getCurrentUserId')
->setTranslatable(TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(new TranslatableMarkup('Authored on'))
->setDescription(new TranslatableMarkup('The time that the calendar event was created.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(new TranslatableMarkup('Changed'))
->setDescription(new TranslatableMarkup('The time that the calendar event was last edited.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE);
$fields[static::getRevisionMetadataKey($entity_type, 'revision_log_message')]
->setDisplayOptions('form', []);
return $fields;
}
public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
$fields = [];
$type = static::getCalendarEventType($bundle);
if (!$type) {
\Drupal::logger('opigno_calendar_event')
->error('Invalid bundle %bundle specified when providing bundle field definitions.', [
'%bundle' => $bundle,
]);
return $fields;
}
$field_type = static::getDateFieldType($type);
$field_name = static::getDateFieldName($type);
$fields[$field_name] = BundleFieldDefinition::create($field_type)
->setName($field_name)
->setTargetEntityTypeId($entity_type
->id())
->setLabel(new TranslatableMarkup('Date'))
->setRequired(TRUE)
->setRevisionable(TRUE)
->setTranslatable(FALSE)
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('view', [
'type' => $field_type . '_default',
'weight' => -3,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('form', [
'type' => $field_type . '_default',
'weight' => -3,
]);
return $fields;
}
public static function storageFieldDefinitions(EntityTypeInterface $entity_type) {
$storage_definitions = [];
$entity_type_id = $entity_type
->id();
$bundle_info = \Drupal::service('entity_type.bundle.info');
foreach ($bundle_info
->getBundleInfo($entity_type_id) as $bundle => $info) {
$storage_definitions += static::bundleFieldDefinitions($entity_type, $bundle, []);
}
return $storage_definitions;
}
public static function getCurrentUserId() {
return [
\Drupal::currentUser()
->id(),
];
}
}