View source
<?php
namespace Drupal\gdpr_consent\Entity;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\RevisionableContentEntityBase;
use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\user\UserInterface;
use function array_keys;
class ConsentAgreement extends RevisionableContentEntityBase implements ConsentAgreementInterface {
use EntityChangedTrait;
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'user_id' => \Drupal::currentUser()
->id(),
];
}
protected function urlRouteParameters($rel) {
$uriRouteParameters = parent::urlRouteParameters($rel);
if ($this instanceof RevisionableInterface) {
if ($rel === 'revision_revert') {
$uriRouteParameters[$this
->getEntityTypeId() . '_revision'] = $this
->getRevisionId();
}
elseif ($rel === 'revision_delete') {
$uriRouteParameters[$this
->getEntityTypeId() . '_revision'] = $this
->getRevisionId();
}
}
return $uriRouteParameters;
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
foreach (array_keys($this
->getTranslationLanguages()) as $langcode) {
$translation = $this
->getTranslation($langcode);
if (!$translation
->getOwner()) {
$translation
->setOwnerId(0);
}
}
if (!$this
->getRevisionUser()) {
$this
->setRevisionUserId($this
->getOwnerId());
}
}
public function requiresExplicitAcceptance() {
return $this
->get('mode')->value == 'explicit';
}
public function getName() {
return $this
->get('name')->value;
}
public function setName($name) {
$this
->set('name', $name);
return $this;
}
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 setOwnerId($uid) {
$this
->set('user_id', $uid);
return $this;
}
public function setOwner(UserInterface $account) {
$this
->set('user_id', $account
->id());
return $this;
}
public function isPublished() {
return (bool) $this
->getEntityKey('status');
}
public function setPublished($published) {
$this
->set('status', $published ? TRUE : FALSE);
return $this;
}
public function getTitle() {
return (string) $this
->get('title')->value;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Title'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDescription(t('Agreement title.'))
->setRequired(TRUE)
->setDisplayOptions('view', [
'type' => 'textfield',
])
->setDisplayOptions('form', [
'type' => 'textfield',
'weight' => 0,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['mode'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Agreement type'))
->setRevisionable(TRUE)
->setDescription(t('Whether consent is implicit or explicit. Set to "Explicit" if the user needs to explicitly agree, otherwise "Implicit".'))
->setDefaultValue('explicit')
->setSetting('allowed_values_function', [
static::class,
'getModes',
])
->setRequired(TRUE)
->setDisplayOptions('view', [
'type' => 'select',
])
->setDisplayOptions('form', [
'type' => 'select',
'weight' => 1,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['description'] = BaseFieldDefinition::create('string')
->setLabel(t('Description'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDescription(t('Text displayed to the user on the form'))
->setRequired(TRUE)
->setDisplayOptions('view', [
'type' => 'textfield',
])
->setDisplayOptions('form', [
'type' => 'textfield',
'weight' => 2,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['long_description'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Long description'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDescription(t('Text shown when the user clicks for more details.'))
->setDisplayOptions('view', [
'type' => 'textarea',
])
->setDisplayOptions('form', [
'type' => 'textarea',
'weight' => 3,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['notes'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Notes'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDescription(t('This should contain the rationale behind the agreement.'))
->setDisplayOptions('view', [
'type' => 'textarea',
])
->setDisplayOptions('form', [
'type' => 'textarea',
'weight' => 4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Publishing status'))
->setDescription(t('A boolean indicating whether the Consent Agreement is published.'))
->setRevisionable(TRUE)
->setDefaultValue(TRUE);
$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.'));
$fields['revision_translation_affected'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Revision translation affected'))
->setDescription(t('Indicates if the last edit of a translation belongs to current revision.'))
->setReadOnly(TRUE)
->setRevisionable(TRUE)
->setTranslatable(TRUE);
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of author of the Consent Agreement entity.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'type' => 'author',
])
->setDisplayOptions('form', [
'type' => 'hidden',
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'region' => 'hidden',
])
->setDisplayOptions('form', [
'type' => 'language_select',
'weight' => 2,
]);
return $fields;
}
public static function getModes() {
return [
'implicit' => t('Implicit'),
'explicit' => t('Explicit'),
];
}
public function __toString() {
return $this
->getTitle();
}
}