recurring_events-2.0.x-dev/src/Entity/EventInstance.php
src/Entity/EventInstance.php
<?php
declare(strict_types=1);
namespace Drupal\recurring_events\Entity;
use Drupal\Core\Entity\EditorialContentEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\recurring_events\EventInterface;
use Drupal\recurring_events\EventUserTrait;
/**
* Defines the Event Instance entity.
*
* @ingroup recurring events
*
* This is the main definition of the entity type. From it, an entityType is
* derived. The most important properties in this example are listed below.
*
* id: The unique identifier of this entityType. It follows the pattern
* 'moduleName_xyz' to avoid naming conflicts.
*
* label: Human readable name of the entity type.
*
* handlers: Handler classes are used for different tasks. You can use
* standard handlers provided by D8 or build your own, most probably derived
* from the standard class. In detail:
*
* - view_builder: we use the standard controller to view an instance. It is
* called when a route lists an '_entity_view' default for the entityType
* (see routing.yml for details. The view can be manipulated by using the
* standard drupal tools in the settings.
*
* - list_builder: We derive our own list builder class from the
* entityListBuilder to control the presentation.
* If there is a view available for this entity from the views module, it
* overrides the list builder. @todo: any view? naming convention?
*
* - form: We derive our own forms to add functionality like additional fields,
* redirects etc. These forms are called when the routing list an
* '_entity_form' default for the entityType. Depending on the suffix
* (.add/.edit/.delete) in the route, the correct form is called.
*
* - access: Our own accessController where we determine access rights based on
* permissions.
*
* More properties:
*
* - base_table: Define the name of the table used to store the data. Make sure
* it is unique. The schema is automatically determined from the
* BaseFieldDefinitions below. The table is automatically created during
* installation.
*
* - fieldable: Can additional fields be added to the entity via the GUI?
* Analog to content types.
*
* - entity_keys: How to access the fields. Analog to 'nid' or 'uid'.
*
* - links: Provide links to do standard tasks. The 'edit-form' and
* 'delete-form' links are added to the list built by the
* entityListController. They will show up as action buttons in an additional
* column.
*
* There are many more properties to be used in an entity type definition. For
* a complete overview, please refer to the '\Drupal\Core\Entity\EntityType'
* class definition.
*
* The following construct is the actual definition of the entity type which
* is read and cached. Don't forget to clear cache after changes.
*
* @ContentEntityType(
* id = "eventinstance",
* label = @Translation("Event Instance entity"),
* handlers = {
* "storage" = "Drupal\recurring_events\EventInstanceStorage",
* "list_builder" = "Drupal\recurring_events\EventInstanceListBuilder",
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "views_data" = "Drupal\views\EntityViewsData",
* "translation" = "Drupal\recurring_events\EventInstanceTranslationHandler",
* "form" = {
* "edit" = "Drupal\recurring_events\Form\EventInstanceForm",
* "delete" = "Drupal\recurring_events\Form\EventInstanceDeleteForm",
* "default" = "Drupal\recurring_events\Form\EventInstanceForm",
* "clone" = "Drupal\recurring_events\Form\EventInstanceCloneForm",
* },
* "access" = "Drupal\recurring_events\EventInstanceAccessControlHandler",
* "route_provider" = {
* "html" = "Drupal\recurring_events\EventInstanceHtmlRouteProvider",
* },
* },
* base_table = "eventinstance",
* data_table = "eventinstance_field_data",
* revision_table = "eventinstance_revision",
* revision_data_table = "eventinstance_field_revision",
* show_revision_ui = TRUE,
* translatable = TRUE,
* admin_permission = "administer eventinstance entity",
* fieldable = TRUE,
* bundle_entity_type = "eventinstance_type",
* entity_keys = {
* "id" = "id",
* "revision" = "vid",
* "published" = "status",
* "langcode" = "langcode",
* "uuid" = "uuid",
* "label" = "title",
* "bundle" = "type",
* "uid" = "uid",
* },
* revision_metadata_keys = {
* "revision_user" = "revision_uid",
* "revision_created" = "revision_timestamp",
* "revision_log_message" = "revision_log"
* },
* links = {
* "canonical" = "/events/{eventinstance}",
* "edit-form" = "/events/{eventinstance}/edit",
* "delete-form" = "/events/{eventinstance}/delete",
* "collection" = "/events",
* "admin_collection" = "/admin/content/events/instances",
* "clone-form" = "/events/{eventinstance}/clone",
* "version-history" = "/events/{eventinstance}/revisions",
* "revision" = "/events/{eventinstance}/revisions/{eventinstance_revision}/view",
* "revision_revert" = "/events/{eventinstance}/revisions/{eventinstance_revision}/revert",
* "revision_delete" = "/events/{eventinstance}/revisions/{eventinstance_revision}/delete",
* "translation_revert" = "/events/{eventinstance}/revisions/{eventinstance_revision}/revert/{langcode}",
* },
* field_ui_base_route = "entity.eventinstance_type.edit_form",
* )
*
* The 'links' above are defined by their path. For core to find the
* corresponding route, the route name must follow the correct pattern:
*
* entity.<entity-name>.<link-name> (replace dashes with underscores)
* Example: 'entity.eventinstance.canonical'
*
* See routing file above for the corresponding implementation
*
* The 'EventInstance' class defines methods and fields for the eventinstance
* entity.
*
* Being derived from the ContentEntityBase class, we can override the methods
* we want. In our case we want to provide access to the standard fields about
* creation and changed time stamps.
*
* Our interface (see EventInterface) also exposes the
* EntityOwnerInterface. This allows us to provide methods for setting
* and providing ownership information.
*
* The most important part is the definitions of the field properties for this
* entity type. These are of the same type as fields added through the GUI, but
* they can by changed in code. In the definition we can define if the user with
* the rights privileges can influence the presentation (view, edit) of each
* field.
*/
class EventInstance extends EditorialContentEntityBase implements EventInterface {
use EventUserTrait;
/**
* {@inheritdoc}
*
* When a new entity instance is added, set the user_id entity reference to
* the current user as the creator of the instance.
*/
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'uid' => \Drupal::currentUser()->id(),
];
}
/**
* {@inheritdoc}
*/
protected function urlRouteParameters($rel) {
$uri_route_parameters = parent::urlRouteParameters($rel);
if ($rel === 'revision_revert' && $this instanceof RevisionableInterface) {
$uri_route_parameters[$this->getEntityTypeId() . '_revision'] = $this->getRevisionId();
}
elseif ($rel === 'revision_delete' && $this instanceof RevisionableInterface) {
$uri_route_parameters[$this->getEntityTypeId() . '_revision'] = $this->getRevisionId();
}
return $uri_route_parameters;
}
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
foreach (array_keys($this->getTranslationLanguages()) as $langcode) {
$translation = $this->getTranslation($langcode);
// If no owner has been set explicitly, make the anonymous user the owner.
if (!$translation->getOwner()) {
$translation->setOwnerId(0);
}
}
// If no revision author has been set explicitly, make the node owner the
// revision author.
if (!$this->getRevisionUser()) {
$this->setRevisionUserId($this->getOwnerId());
}
}
/**
* {@inheritdoc}
*/
public function getType() {
return $this->bundle();
}
/**
* {@inheritdoc}
*/
public function getCreatedTime() {
return $this->get('created')->value;
}
/**
* {@inheritdoc}
*/
public function getChangedTime(): int {
return (int) $this->get('changed')->value;
}
/**
* {@inheritdoc}
*/
public function setChangedTime($timestamp) {
$this->set('changed', $timestamp);
return $this;
}
/**
* {@inheritdoc}
*/
public function getRevisionCreationTime() {
return $this->revision_timestamp->value;
}
/**
* {@inheritdoc}
*/
public function setRevisionCreationTime($timestamp) {
$this->revision_timestamp->value = $timestamp;
return $this;
}
/**
* {@inheritdoc}
*
* Define the field properties here.
*
* Field name, type and size determine the table structure.
*
* In addition, we can define how the field and its content can be manipulated
* in the GUI. The behavior of the widgets used can be determined here.
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields += static::ownerBaseFieldDefinitions($entity_type);
// Standard field, used as unique if primary index.
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the event entity.'))
->setReadOnly(TRUE);
// Standard field, unique outside of the scope of the current project.
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the event entity.'))
->setReadOnly(TRUE);
$fields['type'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Type'))
->setDescription(t('The eventinstance type.'))
->setSetting('target_type', 'eventinstance_type')
->setReadOnly(TRUE);
$fields['body'] = BaseFieldDefinition::create('text_long')
->setLabel(t('Body'))
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('form', [
'type' => 'text_textarea',
'weight' => -4,
])
->setDisplayConfigurable('form', TRUE);
$fields['date'] = BaseFieldDefinition::create('daterange')
->setLabel(t('Event Date'))
->setTranslatable(FALSE)
->setRevisionable(TRUE)
->setRequired(TRUE)
->setTargetBundle('eventinstance')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -4,
'settings' => [
'date_format' => 'F jS, Y h:iA',
'separator' => '-',
],
])
->setDisplayOptions('form', [
'type' => 'daterange_default',
'weight' => -4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['eventseries_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Event Series ID'))
->setDescription(t('The ID of the event series entity.'))
->setSetting('target_type', 'eventseries')
->setTranslatable(TRUE);
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The language code of event entity.'));
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the entity was created.'));
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setRevisionable(TRUE)
->setDescription(t('The time that the entity was last edited.'));
$fields['status']
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => TRUE,
],
'weight' => 12,
])
->setDisplayConfigurable('form', TRUE);
$fields['revision_translation_affected'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Revision translation affected'))
->setDescription(t('Indicates if the last edit of a translation belongs to current revision.'))
->setReadOnly(TRUE)
->setRevisionable(TRUE)
->setTranslatable(TRUE);
return $fields;
}
/**
* Get event series.
*
* @return \Drupal\recurring_events\EventInterface
* The event series.
*/
public function getEventSeries() {
$entity = $this->get('eventseries_id')->entity;
if ($entity->hasTranslation($this->language()->getId())) {
return $entity->getTranslation($this->language()->getId());
}
return $entity;
}
}
