Log.php in Log entity 8
Same filename and directory in other branches
Contains \Drupal\log\Entity\Log.
Namespace
Drupal\log\EntityFile
src/Entity/Log.phpView source
<?php
/**
* @file
* Contains \Drupal\log\Entity\Log.
*/
namespace Drupal\log\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\Render\BubbleableMetadata;
use Drupal\log\LogInterface;
use Drupal\user\UserInterface;
/**
* Defines the Log entity.
*
* @ingroup log
*
* @ContentEntityType(
* id = "log",
* label = @Translation("Log"),
* bundle_label = @Translation("Log type"),
* handlers = {
* "storage" = "Drupal\log\LogStorage",
* "storage_schema" = "Drupal\log\LogStorageSchema",
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "access" = "Drupal\log\LogAccessControlHandler",
* "views_data" = "Drupal\log\LogViewsData",
* "form" = {
* "default" = "Drupal\log\Form\LogForm",
* "edit" = "Drupal\log\Form\LogForm",
* "delete" = "Drupal\log\Form\LogDeleteForm",
* },
* "route_provider" = {
* "html" = "Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider",
* },
* "list_builder" = "Drupal\log\LogListBuilder",
* },
* base_table = "log",
* revision_table = "log_revision",
* admin_permission = "administer log",
* entity_keys = {
* "id" = "id",
* "revision" = "vid",
* "bundle" = "type",
* "label" = "name",
* "uid" = "uid",
* "uuid" = "uuid"
* },
* bundle_entity_type = "log_type",
* field_ui_base_route = "entity.log_type.edit_form",
* common_reference_target = TRUE,
* permission_granularity = "bundle",
* links = {
* "canonical" = "/log/{log}",
* "edit-form" = "/log/{log}/edit",
* "delete-form" = "/log/{log}/delete"
* }
* )
*/
class Log extends ContentEntityBase implements LogInterface {
use EntityChangedTrait;
protected $tokenService;
public function __construct(array $values, $entity_type, $bundle, array $translations = []) {
parent::__construct($values, $entity_type, $bundle, $translations);
$this->tokenService = \Drupal::token();
}
/**
* {@inheritdoc}
*/
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
// Set default value for name and done properties.
if (!empty($values['type'])) {
$type = \Drupal::entityManager()
->getStorage('log_type')
->load($values['type']);
$values += [
'name' => $type
->getNamePattern(),
'done' => $type
->isAutomaticallyDone(),
];
}
}
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
$type = \Drupal::entityManager()
->getStorage('log_type')
->load($this
->getType());
if (!$type
->isNameEditable() && $this
->isNew()) {
// Pass in an empty bubblable metadata object, so we can avoid starting a
// renderer, for example if this happens in a REST resource creating
// context.
$bubbleable_metadata = new BubbleableMetadata();
$this
->set('name', $this->tokenService
->replace($type
->getNamePattern(), [
'log' => $this,
], [], $bubbleable_metadata));
}
}
/**
* {@inheritdoc}
*/
public function getName() {
return $this
->get('name')->value;
}
/**
* {@inheritdoc}
*/
public function setName($name) {
$this
->set('name', $name);
return $this;
}
/**
* {@inheritdoc}
*/
public function getType() {
return $this
->bundle();
}
/**
* {@inheritdoc}
*/
public function getTypeName() {
return $this
->get('type')->entity
->label();
}
/**
* {@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('user_id')->entity;
}
/**
* {@inheritdoc}
*/
public function getOwnerId() {
return $this
->get('user_id')->target_id;
}
/**
* {@inheritdoc}
*/
public function setOwnerId($uid) {
$this
->set('user_id', $uid);
return $this;
}
/**
* {@inheritdoc}
*/
public function setOwner(UserInterface $account) {
$this
->set('user_id', $account
->id());
return $this;
}
/**
* {@inheritdoc}
*/
public function getRevisionCreationTime() {
return $this
->get('revision_timestamp')->value;
}
/**
* {@inheritdoc}
*/
public function setRevisionCreationTime($timestamp) {
$this
->set('revision_timestamp', $timestamp);
return $this;
}
/**
* {@inheritdoc}
*/
public function getRevisionAuthor() {
return $this
->get('revision_uid')->entity;
}
/**
* {@inheritdoc}
*/
public function setRevisionAuthorId($uid) {
$this
->set('revision_uid', $uid);
return $this;
}
/**
* @return array
*/
public static function getCurrentUserId() {
return array(
\Drupal::currentUser()
->id(),
);
}
/**
* @return array
*/
public static function getCurrentTimestamp() {
return array(
REQUEST_TIME,
);
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the Log entity.'))
->setReadOnly(TRUE);
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of author of the Log entity.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDefaultValueCallback('Drupal\\log\\Entity\\Log::getCurrentUserId')
->setTranslatable(TRUE)
->setDisplayOptions('view', array(
'label' => 'hidden',
'type' => 'author',
'weight' => 99,
))
->setDisplayOptions('form', array(
'type' => 'entity_reference_autocomplete',
'weight' => 99,
'settings' => array(
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
),
))
->setDisplayConfigurable('view', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Authored on'))
->setDescription(t('The time that the log was created.'))
->setRevisionable(TRUE)
->setDisplayOptions('view', array(
'label' => 'hidden',
'type' => 'timestamp',
'weight' => 0,
))
->setDisplayOptions('form', array(
'type' => 'datetime_timestamp',
'weight' => 99,
))
->setDisplayConfigurable('form', TRUE);
$fields['vid'] = BaseFieldDefinition::create('integer')
->setLabel(t('Revision ID'))
->setDescription(t('The log revision ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['type'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Type'))
->setDescription(t('The log type.'))
->setSetting('target_type', 'log_type')
->setReadOnly(TRUE);
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language'))
->setDescription(t('The log language code.'))
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setDisplayOptions('view', array(
'type' => 'hidden',
))
->setDisplayOptions('form', array(
'type' => 'language_select',
'weight' => 2,
));
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setRevisionable(TRUE)
->setDefaultValue('')
->setSetting('max_length', 255)
->setSetting('text_processing', 0)
->setDisplayOptions('view', array(
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
))
->setDisplayOptions('form', array(
'type' => 'string_textfield',
'weight' => -5,
))
->setDisplayConfigurable('form', TRUE);
$fields['timestamp'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Date'))
->setDescription(t('Timestamp of the event being logged.'))
->setDefaultValueCallback('Drupal\\log\\Entity\\Log::getCurrentTimestamp')
->setRevisionable(TRUE)
->setDisplayOptions('view', array(
'label' => 'above',
'type' => 'timestamp',
'weight' => 10,
))
->setDisplayOptions('form', array(
'type' => 'datetime_timestamp',
'weight' => 80,
))
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
$fields['done'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Done'))
->setDescription(t('Boolean indicating whether the log is done (the event happened).'))
->setRevisionable(TRUE)
->setDefaultValue(TRUE)
->setDisplayOptions('view', array(
'label' => 'above',
'type' => 'boolean',
'weight' => 10,
))
->setDisplayOptions('form', array(
'settings' => array(
'display_label' => TRUE,
),
'weight' => 90,
))
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
// Read only.
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the log was last edited.'));
$fields['revision_timestamp'] = BaseFieldDefinition::create('created')
->setLabel(t('Revision timestamp'))
->setDescription(t('The time that the current revision was created.'))
->setQueryable(FALSE)
->setRevisionable(TRUE);
$fields['revision_uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Revision user ID'))
->setDescription(t('The user ID of the author of the current revision.'))
->setSetting('target_type', 'user')
->setQueryable(FALSE)
->setRevisionable(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the Log entity.'))
->setReadOnly(TRUE);
return $fields;
}
}