CacheflushEntity.php in CacheFlush 8
File
modules/cacheflush_entity/src/Entity/CacheflushEntity.php
View source
<?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;
class CacheflushEntity extends ContentEntityBase implements CacheflushEntityInterface {
use EntityChangedTrait;
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'uid' => \Drupal::currentUser()
->id(),
];
}
public function getTitle() {
return $this
->get('title')->value;
}
public function setTitle($title) {
$this
->set('title', $title);
return $this;
}
public function getStatus() {
return $this
->get('status')->value;
}
public function setStatus($status) {
$this
->set('status', $status);
return $this;
}
public function getData() {
$data = $this
->get('data')
->getValue();
return isset($data[0]) ? $data[0] : $data;
}
public function setData(array $data) {
$this
->set('data', serialize($data));
return $this;
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function getOwner() {
return $this
->get('uid')->entity;
}
public function getOwnerId() {
return $this
->get('uid')->target_id;
}
public function setOwnerId($uid) {
$this
->set('uid', $uid);
return $this;
}
public function setOwner(UserInterface $account) {
$this
->set('uid', $account
->id());
return $this;
}
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;
}
}