pm-4.1.x-dev/modules/pm_priority/src/Entity/PmPriority.php
modules/pm_priority/src/Entity/PmPriority.php
<?php
namespace Drupal\pm_priority\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\pm_priority\PmPriorityInterface;
use Drupal\user\EntityOwnerTrait;
/**
* Defines the Priority entity class.
*
* @ContentEntityType(
* id = "pm_priority",
* label = @Translation("Priority"),
* label_collection = @Translation("Priorities"),
* label_singular = @Translation("Priority"),
* label_plural = @Translation("Priorities"),
* label_count = @PluralTranslation(
* singular = "@count Priorities",
* plural = "@count Priorities",
* ),
* handlers = {
* "list_builder" = "Drupal\pm_priority\PmPriorityListBuilder",
* "views_data" = "Drupal\views\EntityViewsData",
* "access" = "Drupal\pm_priority\PmPriorityAccessControlHandler",
* "form" = {
* "add" = "Drupal\pm_priority\Form\PmPriorityForm",
* "edit" = "Drupal\pm_priority\Form\PmPriorityForm",
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
* },
* "route_provider" = {
* "html" = "Drupal\pm_priority\Routing\PmPriorityHtmlRouteProvider",
* }
* },
* base_table = "pm_priority",
* admin_permission = "administer pm priority",
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid",
* "owner" = "uid",
* },
* links = {
* "collection" = "/admin/pm/priority",
* "add-form" = "/pm/priority/add",
* "canonical" = "/pm/priority/{pm_priority}",
* "edit-form" = "/pm/priority/{pm_priority}/edit",
* "delete-form" = "/pm/priority/{pm_priority}/delete",
* },
* field_ui_base_route = "entity.pm_priority.settings",
* )
*/
class PmPriority extends ContentEntityBase implements PmPriorityInterface {
use EntityChangedTrait;
use EntityOwnerTrait;
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
if (!$this->getOwnerId()) {
// If no owner has been set explicitly, make the anonymous user the owner.
$this->setOwnerId(0);
}
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['label'] = BaseFieldDefinition::create('string')
->setLabel(t('Label'))
->setRequired(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
])
->setDisplayConfigurable('view', TRUE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Status'))
->setDefaultValue(TRUE)
->setSetting('on_label', 'Enabled')
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => FALSE,
],
'weight' => 0,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'type' => 'boolean',
'label' => 'above',
'weight' => 0,
'settings' => [
'format' => 'enabled-disabled',
],
])
->setDisplayConfigurable('view', TRUE);
$fields['color'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Color'))
->setDescription(t('Color associated with the priority.'))
->setDefaultValue('blue')
->setSettings([
'allowed_values' => [
'blue' => 'Blue',
'gray' => 'Gray',
'red' => 'Red',
'green' => 'Green',
'yellow' => 'Yellow',
'indigo' => 'Indigo',
'purple' => 'Purple',
'pink' => 'Pink',
],
])
->setDisplayOptions('view', [
'label' => 'visible',
'type' => 'list_default',
'weight' => 6,
])
->setDisplayOptions('form', [
'type' => 'options_select',
'weight' => 6,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
$fields['weight'] = BaseFieldDefinition::create('integer')
->setLabel(t('Weight'))
->setDescription(t('The weight of this Priority in relation to other Priorities.'))
->setDefaultValue(0)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'number_integer',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'number',
'weight' => 20,
]);
$fields['description'] = BaseFieldDefinition::create('text_long')
->setLabel(t('Description'))
->setDisplayOptions('form', [
'type' => 'text_textarea',
'weight' => 10,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'type' => 'text_default',
'label' => 'above',
'weight' => 10,
])
->setDisplayConfigurable('view', TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Author'))
->setSetting('target_type', 'user')
->setDefaultValueCallback(static::class . '::getDefaultEntityOwner')
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'settings' => [
'match_operator' => 'CONTAINS',
'size' => 60,
'placeholder' => '',
],
'weight' => 15,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'author',
'weight' => 15,
])
->setDisplayConfigurable('view', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Authored on'))
->setDescription(t('The time that the priority was created.'))
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'timestamp',
'weight' => 20,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('form', [
'type' => 'datetime_timestamp',
'weight' => 20,
])
->setDisplayConfigurable('view', TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the priority was last edited.'));
return $fields;
}
}
