filebrowser-8.x-2.x-dev/src/Entity/FilebrowserMetadataEntity.php
src/Entity/FilebrowserMetadataEntity.php
<?php
namespace Drupal\filebrowser\Entity;
use Drupal;
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\node\Entity\Node;
use Drupal\user\UserInterface;
/**
* Defines the Filebrowser metadata entity.
*
* @ingroup filebrowser
*
* @ContentEntityType(
* id = "filebrowser_metadata_entity",
* label = @Translation("Filebrowser metadata entity"),
* base_table = "filebrowser_metadata_entity",
* admin_permission = "administer filebrowser metadata entity entities",
* entity_keys = {
* "id" = "id",
* "name" = "name",
* },
* )
*/
class FilebrowserMetadataEntity extends ContentEntityBase implements FilebrowserMetadataEntityInterface {
use EntityChangedTrait;
use Drupal\user\EntityOwnerTrait;
/**
* {@inheritdoc}
*/
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'uid' => Drupal::currentUser()->id(),
];
}
/**
* {@inheritdoc}
*/
public function getName() {
return $this->get('name')->value;
}
/**
* {@inheritdoc}
*/
public function setName($name) {
$this->set('name', $name);
return $this;
}
/**
* {@inheritdoc}
*/
public function getCreatedTime() {
return $this->get('created')->value;
}
/**
* {@inheritdoc}
*/
public function setCreatedTime($timestamp) {
$this->set('created', $timestamp);
return $this;
}
/**
* {@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 function isPublished() {
return (bool) $this->getEntityKey('status');
}
/**
* {@inheritdoc}
*/
public function setPublished($published) {
$this->set('status', $published ? Node::PUBLISHED: Node::NOT_PUBLISHED);
return $this;
}
/**
* @inheritDoc
*/
public function getContent() {
return $this->get('content')->getValue();
}
/**
* @inheritDoc
*/
public function setContent($content) {
$this->get('content')->setValue($content);
}
/**
* @inheritDoc
*/
public function getFid() {
return $this->get('fid')->getValue();
}
/**
* @inheritDoc
*/
public function setFid($fid) {
$this->get('fid')->setValue($fid);
}
/**
* @inheritDoc
*/
public function getModule() {
return $this->get('module')->getValue();
}
/**
* @inheritDoc
*/
public function setModule($fid) {
$this->get('fid')->setValue($fid);
}
/**
* @inheritDoc
*/
public function getTheme() {
return $this->get('theme')->getValue();
}
/**
* @inheritDoc
*/
public function setTheme($fid) {
$this->get('theme')->setValue($fid);
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the Metadata entity entity.'))
->setReadOnly(TRUE);
$fields['fid'] = BaseFieldDefinition::create('integer')
->setRequired(true);
$fields['nid'] = BaseFieldDefinition::create('integer')
->setRequired(true);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Author'))
->setDescription(t('The user ID of the author of this metadata entity.'))
->setSetting('target_type', 'user')
->setDefaultValueCallback('Drupal\node\Entity\Node::getCurrentUserId') // or use preCreate()
->setTranslatable(FALSE)
->setRevisionable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 0,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Machine readable name'))
->setDescription(t('The machine name of the Filebrowser Metadata entity.'))
->setSettings([
'max_length' => 50,
'text_processing' => 0,
]);
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The Name of the Filebrowser Metadata entity.'))
->setSettings([
'max_length' => 50,
'text_processing' => 0,
]);
$fields['module'] = BaseFieldDefinition::create('string')
->setRequired(true);
$fields['theme'] = BaseFieldDefinition::create('string');
$fields['content'] = BaseFieldDefinition::create('string_long')
->setSetting('case_sensitive',TRUE);
return $fields;
}
}
