View source
<?php
namespace Drupal\gdpr_tasks\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\Core\StringTranslation\StringTranslationTrait;
use Drupal\user\UserInterface;
class Task extends ContentEntityBase implements TaskInterface {
use EntityChangedTrait;
use StringTranslationTrait;
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'requested_by' => \Drupal::currentUser()
->id(),
];
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
public function getOwner() {
return $this
->get('user_id')->entity;
}
public function getOwnerId() {
return $this
->get('user_id')->target_id;
}
public function setOwnerId($uid) {
$this
->set('user_id', $uid);
return $this;
}
public function setOwner(UserInterface $account) {
$this
->set('user_id', $account
->id());
return $this;
}
public function setProcessedBy(UserInterface $account) {
$this
->set('processed_by', $account
->id());
return $this;
}
public function setProcessedById($uid) {
$this
->set('processed_by', $uid);
return $this;
}
public function getProcessedBy() {
return $this
->get('processed_by')->entity;
}
public function getProcessedById() {
return $this
->get('processed_by')->target_id;
}
public function label() {
return $this
->t('Task @id', [
'@id' => $this
->id(),
]);
}
public function getStatus() {
return $this->status->value;
}
public function getStatusLabel() {
$statuses = self::getStatuses();
$value = $this
->getStatus();
if (isset($statuses[$value])) {
$value = $statuses[$value];
}
return $value;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('User'))
->setDescription(t('The user whose data should be processed.'))
->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['status'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Status'))
->setRequired(TRUE)
->setDefaultValue('requested')
->setSetting('allowed_values_function', [
static::class,
'getStatuses',
])
->setDisplayOptions('form', [
'label' => 'hidden',
'type' => 'select',
])
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', 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.'));
$fields['complete'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the task was completed.'));
$fields['processed_by'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Processed by'))
->setDescription(t('The user who processed this task.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'weight' => 5,
])
->setDisplayOptions('form', [
'label' => 'hidden',
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => 60,
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['requested_by'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Requested by'))
->setDescription(t('The user who requested this task.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'weight' => 6,
])
->setDisplayOptions('form', [
'label' => 'hidden',
'type' => 'entity_reference_autocomplete',
'weight' => 6,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => 60,
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['notes'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Notes'))
->setDescription(t('Notes with this request'))
->setRequired(FALSE)
->setDisplayOptions('view', [
'type' => 'textfield',
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
return $fields;
}
public static function getStatuses() {
return [
'requested' => t('Requested'),
'building' => t('Building data'),
'reviewing' => t('Needs review'),
'processed' => t('Processed'),
'closed' => t('Closed'),
];
}
}