TypeGroup.php in Booking and Availability Management Tools for Drupal 8
Contains \Drupal\bat\Entity\TypeGroup.
Namespace
Drupal\bat\EntityFile
src/Entity/TypeGroup.phpView source
<?php
/**
* @file
* Contains \Drupal\bat\Entity\TypeGroup.
*/
namespace Drupal\bat\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\bat\TypeGroupInterface;
use Drupal\user\UserInterface;
use Drupal\user\EntityOwnerTrait;
/**
* Defines the Type Group entity.
*
* @ingroup bat
*
* @ContentEntityType(
* id = "bat_type_group",
* label = @Translation("Type Group"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\bat\TypeGroupListBuilder",
* "views_data" = "Drupal\bat\Entity\TypeGroupViewsData",
* "form" = {
* "default" = "Drupal\bat\Entity\Form\TypeGroupForm",
* "add" = "Drupal\bat\Entity\Form\TypeGroupForm",
* "edit" = "Drupal\bat\Entity\Form\TypeGroupForm",
* "delete" = "Drupal\bat\Entity\Form\TypeGroupDeleteForm",
* },
* "access" = "Drupal\bat\TypeGroupAccessControlHandler",
* },
* base_table = "type_group",
* admin_permission = "administer type_group entity",
* entity_keys = {
* "id" = "id",
* "bundle" = "type",
* "label" = "name",
* "uuid" = "uuid",
* "uid" = "uid",
* "owner" = "uid",
* "langcode" = "langcode",
* },
* bundle_entity_type = "bat_type_group_bundle",
* field_ui_base_route = "entity.bat_type_group_bundle.edit_form",
* permission_granularity = "bundle",
* links = {
* "canonical" = "/admin/type_group/{bat_type_group}",
* "edit-form" = "/admin/type_group/{bat_type_group}/edit",
* "delete-form" = "/admin/type_group/{bat_type_group}/delete"
* },
* )
*/
class TypeGroup extends ContentEntityBase implements TypeGroupInterface {
use EntityChangedTrait, EntityOwnerTrait;
/**
* {@inheritdoc}
*/
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'uid' => \Drupal::currentUser()
->id(),
];
}
/**
* {@inheritdoc}
*/
public function getCreatedTime() {
return $this
->get('created')->value;
}
/**
* {@inheritdoc}
*/
public function getOwner() {
return $this
->get('uid')->entity;
}
/**
* {@inheritdoc}
*/
public function getOwnerId() {
return $this
->get('uid')->target_id;
}
/**
* {@inheritdoc}
*/
public function getStatus() {
return $this
->get('status')->value;
}
/**
* {@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 setStatus($status) {
$this
->set('status', $status);
return $this;
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields += static::ownerBaseFieldDefinitions($entity_type);
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the Type Group entity.'))
->setReadOnly(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the Type Group entity.'))
->setReadOnly(TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of author of the Type Group entity.'))
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDefaultValueCallback(static::class . '::getCurrentUserId')
->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 Type Group 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)
->setRequired(TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Authored on'))
->setDescription(t('The time that the entity was created.'))
->setDisplayOptions('form', [
'type' => 'datetime_timestamp',
'weight' => 10,
])
->setDisplayConfigurable('form', TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the entity was last edited.'));
$fields['type'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Type'))
->setDescription(t('The type group bundle.'))
->setSetting('target_type', 'bat_type_group_bundle');
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Published'))
->setDefaultValue(TRUE);
return $fields;
}
/**
* Default value callback for 'uid' base field definition.
*
* @see ::baseFieldDefinitions()
*
* @return array
* An array of default values.
*/
public static function getCurrentUserId() {
return [
\Drupal::currentUser()
->id(),
];
}
}