cacheflush-8.x-1.x-dev/modules/cacheflush_entity/src/Entity/CacheflushEntity.php
modules/cacheflush_entity/src/Entity/CacheflushEntity.php
<?php
namespace Drupal\cacheflush_entity\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\cacheflush_entity\CacheflushEntityInterface;
use Drupal\user\UserInterface;
/**
* Defines the Cacheflush entity.
*
* @ingroup cacheflush_entity
*
* @ContentEntityType(
* id = "cacheflush",
* label = @Translation("Cacheflush"),
* base_table = "cacheflush",
* entity_keys = {
* "id" = "id",
* "label" = "title",
* "uuid" = "uuid"
* }
* )
*/
class CacheflushEntity extends ContentEntityBase implements CacheflushEntityInterface {
use EntityChangedTrait;
/**
* {@inheritdoc}
*/
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'uid' => \Drupal::currentUser()->id(),
];
}
/**
* {@inheritdoc}
*/
public function getTitle() {
return $this->get('title')->value;
}
/**
* {@inheritdoc}
*/
public function setTitle($title) {
$this->set('title', $title);
return $this;
}
/**
* {@inheritdoc}
*/
public function getStatus() {
return $this->get('status')->value;
}
/**
* {@inheritdoc}
*/
public function setStatus($status) {
$this->set('status', $status);
return $this;
}
/**
* {@inheritdoc}
*/
public function getData() {
$data = $this->get('data')->getValue();
return isset($data[0]) ? $data[0] : $data;
}
/**
* {@inheritdoc}
*/
public function setData(array $data) {
$this->set('data', serialize($data));
return $this;
}
/**
* {@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 setOwnerId($uid) {
$this->set('uid', $uid);
return $this;
}
/**
* {@inheritdoc}
*/
public function setOwner(UserInterface $account) {
$this->set('uid', $account->id());
return $this;
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the Cacheflush entity.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE)
->setSetting('not null', TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the Cacheflush entity.'))
->setReadOnly(TRUE);
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Title'))
->setDescription(t('The title of the Cacheflush entity.'))
->setSettings([
'max_length' => 80,
'text_processing' => 0,
'not null' => TRUE,
]);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of author of the Cacheflush entity.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default');
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Status'))
->setDefaultValue(0)
->setDescription(t('The Status of the Cacheflush entity.'));
$fields['data'] = BaseFieldDefinition::create('map')
->setLabel(t('Status'))
->setDescription(t('The Status of the Cacheflush entity.'));
$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;
}
}
