commands-1.x-dev/modules/tasks/src/Entity/Task.php
modules/tasks/src/Entity/Task.php
<?php
declare(strict_types=1);
namespace Drupal\tasks\Entity;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\RevisionableContentEntityBase;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\tasks\TaskInterface;
use Drupal\user\EntityOwnerInterface;
use Drupal\user\EntityOwnerTrait;
/**
* Defines the tasks entity class.
*
* @ContentEntityType(
* id = "task",
* label = @Translation("Tasks"),
* label_collection = @Translation("Taskss"),
* label_singular = @Translation("tasks"),
* label_plural = @Translation("tasks"),
* label_count = @PluralTranslation(
* singular = "@count tasks",
* plural = "@count tasks",
* ),
* bundle_label = @Translation("Task type"),
* handlers = {
* "list_builder" = "Drupal\tasks\TaskListBuilder",
* "views_data" = "Drupal\views\EntityViewsData",
* "form" = {
* "add" = "Drupal\tasks\Form\TaskForm",
* "edit" = "Drupal\tasks\Form\TaskForm",
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
* "delete-multiple-confirm" = "Drupal\Core\Entity\Form\DeleteMultipleForm",
* "revision-delete" = \Drupal\Core\Entity\Form\RevisionDeleteForm::class,
* "revision-revert" = \Drupal\Core\Entity\Form\RevisionRevertForm::class,
* },
* "route_provider" = {
* "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
* "revision" = \Drupal\Core\Entity\Routing\RevisionHtmlRouteProvider::class,
* },
* },
* base_table = "task",
* revision_table = "task_revision",
* show_revision_ui = TRUE,
* admin_permission = "administer task types",
* entity_keys = {
* "id" = "id",
* "revision" = "revision_id",
* "bundle" = "bundle",
* "label" = "id",
* "uuid" = "uuid",
* "owner" = "uid",
* },
* revision_metadata_keys = {
* "revision_user" = "revision_uid",
* "revision_created" = "revision_timestamp",
* "revision_log_message" = "revision_log",
* },
* links = {
* "collection" = "/admin/content/task",
* "add-form" = "/task/add/{task_type}",
* "add-page" = "/task/add",
* "canonical" = "/task/{task}",
* "edit-form" = "/task/{task}/edit",
* "delete-form" = "/task/{task}/delete",
* "delete-multiple-form" = "/admin/content/task/delete-multiple",
* "revision" = "/task/{task}/revision/{task_revision}/view",
* "revision-delete-form" = "/task/{task}/revision/{task_revision}/delete",
* "revision-revert-form" = "/task/{task}/revision/{task_revision}/revert",
* "version-history" = "/task/{task}/revisions",
* },
* bundle_entity_type = "task_type",
* field_ui_base_route = "entity.task_type.edit_form",
* constraints = {
* "tasks_commands_create" = {}
* }
* )
*/
class Task extends RevisionableContentEntityBase implements TaskInterface {
use EntityChangedTrait;
use EntityOwnerTrait;
/**
* Create a more user friendly label.
* @return array|\Drupal\Core\StringTranslation\TranslatableMarkup|mixed|string|void|null
*/
public function label() {
$label = t(':type | Task #:id', [
':type' => $this->getBundleEntity() ? $this->getBundleEntity()->label(): parent::label(),
':id' => parent::label(),
]);
return $label;
}
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage): void {
parent::preSave($storage);
if (!$this->getOwnerId()) {
// If no owner has been set explicitly, make the anonymous user the owner.
$this->setOwnerId(0);
}
// Create new command entities.
if ($this->isNew()) {
// For each task command, create a new command entity.
foreach ($this->commands as $command) {
$values = $command->getValue();
$entity = $this->createNewEntity('command', $values, t('New Command'), $this->getOwnerId());
$entity->save();
$command->target_id = $entity->id();
}
}
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type): array {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setRevisionable(TRUE)
->setLabel(t('Created by'))
->setSetting('target_type', 'user')
->setDefaultValueCallback(self::class . '::getDefaultEntityOwner')
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'author',
])
->setDisplayConfigurable('view', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created on'))
->setDescription(t('The time that the tasks was created.'))
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'timestamp',
])
->setDisplayConfigurable('view', TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Last update'))
->setDescription(t('The time the command entity was last updated.'));
$fields['commands'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Commands'))
->setDescription(t('The list of command entities for this task.'))
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setSettings([
'target_type' => 'command',
'handler_settings' => [
'auto_create' => TRUE,
],
])
->setDefaultValueCallback(self::class . '::getDefaultCommands')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
])
->setDisplayConfigurable('view', TRUE);
return $fields;
}
public static function getDefaultCommands(): array {
// Return list of commands that will be created on save.
return [];
}
/**
* Creates a new entity object.
*/
public function createNewEntity($entity_type_id, $values, $label, $uid) {
$entity = $this->entityTypeManager()->getStorage($entity_type_id)->create($values);
if ($entity instanceof EntityOwnerInterface) {
$entity->setOwnerId($uid);
}
return $entity;
}
/**
* Returns the bundle entity of the entity, or NULL if there is none.
*
* @return \Drupal\Core\Entity\EntityInterface|null
* The bundle entity.
*/
protected function getBundleEntity() {
if ($bundle_entity_type = $this->getEntityType()->getBundleEntityType()) {
return \Drupal::entityTypeManager()->getStorage($bundle_entity_type)->load($this->bundle());
}
return NULL;
}
}
