ticket-8.x-1.x-dev/src/Entity/Registration.php
src/Entity/Registration.php
<?php
namespace Drupal\ticket\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface;
/**
* Defines the Registration entity.
*
* @ingroup ticket
*
* @ContentEntityType(
* id = "registration",
* label = @Translation("Registration"),
* bundle_label = @Translation("Registration type"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\ticket\RegistrationListBuilder",
* "views_data" = "Drupal\ticket\Entity\RegistrationViewsData",
* "translation" = "Drupal\ticket\RegistrationTranslationHandler",
*
* "form" = {
* "default" = "Drupal\ticket\Form\RegistrationForm",
* "add" = "Drupal\ticket\Form\RegistrationForm",
* "edit" = "Drupal\ticket\Form\RegistrationForm",
* "delete" = "Drupal\ticket\Form\RegistrationDeleteForm",
* },
* "access" = "Drupal\ticket\RegistrationAccessControlHandler",
* "route_provider" = {
* "html" = "Drupal\ticket\RegistrationHtmlRouteProvider",
* },
* },
* base_table = "registration",
* data_table = "registration_field_data",
* translatable = TRUE,
* admin_permission = "administer registration entities",
* entity_keys = {
* "id" = "registrationId",
* "label" = "name",
* "uuid" = "uuid",
* "uid" = "uid",
* "langcode" = "langcode",
* "status" = "status",
* "bundle" = "registration_type",
* },
* bundle_entity_type = "registration_type",
* links = {
* "canonical" = "/ticket/registrations/{registration}",
* "edit-form" = "/ticket/registrations/{registration}/edit",
* "delete-form" = "/ticket/registrations/{registration}/delete",
* "collection" = "/ticket/registrations",
* },
* field_ui_base_route = "entity.registration_type.edit_form"
* )
*/
class Registration extends ContentEntityBase implements RegistrationInterface {
use EntityChangedTrait;
protected $uid;
protected $registrationId;
protected $registrationType;
/**
* {@inheritdoc}
*/
public function getRegistrationType() {
return $this->registrationType;
}
/**
* {@inheritdoc}
*/
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'uid' => \Drupal::currentUser()->id(),
];
}
/**
* {@inheritdoc}
*/
public function getType() {
return $this->bundle();
}
/**
* {@inheritdoc}
*/
public function getCreatedTime() {
return $this->get('created')->value;
}
/**
* {@inheritdoc}
*/
public function setCreatedTime($timestamp) {
$this->set('created', $timestamp);
return $this;
}
/**
* {@inheritdoc}
*/
public function getOwner() {
return $this->get('uid')->entity;
}
/**
* {@inheritdoc}
*/
public function getOwnerId() {
return $this->get('uid')->target_id;
}
/**
* {@inheritdoc}
*/
public function setOwnerId($uid) {
$this->set('uid', $uid);
return $this;
}
/**
* {@inheritdoc}
*/
public function setOwner(UserInterface $account) {
$this->set('uid', $account->id());
return $this;
}
/**
* {@inheritdoc}
*/
public function getCreator() {
return $this->get('uid')->entity;
}
/**
* {@inheritdoc}
*/
public function getCreatorId() {
return $this->get('uid')->target_id;
}
/**
* {@inheritdoc}
*/
public function setCreatorId($uid) {
$this->set('uid', $uid);
return $this;
}
/**
* {@inheritdoc}
*/
public function setCreator(UserInterface $account) {
$this->set('uid', $account->id());
return $this;
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of author of the Registration entity.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of the Registration entity.'))
->setSettings([
'max_length' => 50,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -4,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Publishing status'))
->setDescription(t('A boolean indicating whether the Registration is published.'))
->setDefaultValue(TRUE);
$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'))
->setDescription(t('The time that the entity was last edited.'));
return $fields;
}
}
