AuditLog.php in Audit Log 8.2
File
src/Entity/AuditLog.php
View source
<?php
namespace Drupal\audit_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\user\UserInterface;
class AuditLog extends ContentEntityBase implements AuditLogInterface {
use EntityChangedTrait;
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'user_id' => \Drupal::currentUser()
->id(),
];
}
public function label() {
return $this
->getMessage();
}
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 getType() {
return $this
->get('entity_type')->value;
}
public function getEvent() {
return $this
->get('event')->value;
}
public function getMessage() {
return $this
->get('message')->value;
}
public function getDate() {
return $this
->get('created')->value;
}
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 static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of author of the Audit log entity.'))
->setSetting('target_type', 'user');
$fields['entity_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Target entity'))
->setDescription(t('The entity id of the entity that was created, modified or deleted.'));
$fields['entity_type'] = BaseFieldDefinition::create('string')
->setLabel(t('Target entity type'))
->setDescription(t('The target entity type.'));
$fields['event'] = BaseFieldDefinition::create('string')
->setLabel(t('Event'))
->setDescription(t('The event type, usually insert, update or delete.'));
$fields['previous_state'] = BaseFieldDefinition::create('string')
->setLabel(t('Previous state'))
->setDescription(t('The previous state of the entity if available.'));
$fields['current_state'] = BaseFieldDefinition::create('string')
->setLabel(t('Current state'))
->setDescription(t('The current state of the entity if available.'));
$fields['message'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Text of log message'))
->setDescription(t('Text of log message to be passed into the t() function.'));
$fields['variables'] = BaseFieldDefinition::create('map')
->setLabel(t('Variables of log message'))
->setDescription(t('Serialized array of variables that match the message string and that is passed into the t() function.'));
$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;
}
}