View source
<?php
namespace Drupal\flag_lists\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\RevisionableContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface;
use Drupal\flag\Entity\Flag;
class FlaggingCollection extends RevisionableContentEntityBase implements FlaggingCollectionInterface {
use EntityChangedTrait;
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'user_id' => \Drupal::currentUser()
->id(),
];
}
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());
}
$flagListService = \Drupal::service('flaglists');
$flagService = \Drupal::service('flag');
$baseFlag = $flagListService
->getFlagForListById($this
->getBaseFlag()
->id());
$flag_to_use = $flagService
->getFlagById($baseFlag
->get('base_flag'));
$template['label'] = $this
->label();
if ($this
->isNew()) {
$template['id'] = strtolower($this
->label());
$template['id'] = preg_replace("/[^a-z0-9_]/", "_", $template['id']);
$this
->setRelatedFlag($template['id']);
}
else {
$template['id'] = $this
->getRelatedFlag()
->id();
}
$template['global'] = $flag_to_use
->isGlobal();
$template['flag_short'] = $flag_to_use
->getShortText('flag');
$template['flag_long'] = $flag_to_use
->getLongText('flag');
$template['flag_message'] = $flag_to_use
->getMessage('flag');
$template['unflag_short'] = $flag_to_use
->getShortText('unflag');
$template['unflag_long'] = $flag_to_use
->getLongText('unflag');
$template['unflag_message'] = $flag_to_use
->getMessage('unflag');
$template['access'] = [];
$template['access']['bundles'] = '';
$template['unflag_denied_text'] = '';
$template['link_type'] = $flag_to_use
->getLinkTypePlugin()
->getPluginId();
$template['flag_type'] = $flag_to_use
->getFlagTypePlugin()
->getPluginId();
$template['entity_type'] = $flag_to_use
->getFlaggableEntityTypeId();
$template['flagTypeConfig'] = $flag_to_use
->getFlagTypePlugin()
->getConfiguration();
$flag = new flag($template, 'flag');
$flag
->save();
}
public static function preDelete(EntityStorageInterface $storage, array $entities) {
$flagService = \Drupal::service('flag');
foreach ($entities as $entity) {
$flag = $flagService
->getFlagById($entity
->getRelatedFlag()
->id());
$flag
->delete();
}
parent::preDelete($storage, $entities);
}
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 getBaseFlag() {
return $this
->get('templateflag')->entity;
}
public function setBaseFlag($baseFlag) {
return $this
->set('templateflag', $baseFlag);
}
public function getRelatedFlag() {
return $this
->get('relatedflag')->entity;
}
public function setRelatedFlag($relatedflag) {
return $this
->set('relatedflag', $relatedflag);
}
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 static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$accountProxy = \Drupal::currentUser();
$account = $accountProxy
->getAccount();
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of author of the Flagging collection entity.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'hidden',
'weight' => 5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of the Flagging collection entity.'))
->setRevisionable(TRUE)
->setSettings([
'max_length' => 50,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -4,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', FALSE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Publishing status'))
->setDescription(t('A boolean indicating whether the Flagging collection 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['templateflag'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Base flag'))
->setDescription(t('The flag list used as base'))
->setTranslatable(TRUE)
->setSetting('target_type', 'flag')
->setSetting('handler', 'default')
->setRevisionable(TRUE)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => 1,
])
->setDisplayConfigurable('view', TRUE);
$fields['relatedflag'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Related flag'))
->setDescription(t('The related flag used for for this list'))
->setTranslatable(TRUE)
->setSetting('target_type', 'flag')
->setSetting('handler', 'default')
->setRevisionable(TRUE)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE);
return $fields;
}
protected function urlRouteParameters($rel) {
$uri_route_parameters = parent::urlRouteParameters($rel);
if ($rel === 'revision_revert' && $this instanceof RevisionableContentEntityBase) {
$uri_route_parameters[$this
->getEntityTypeId() . '_revision'] = $this
->getRevisionId();
}
elseif ($rel === 'revision_delete' && $this instanceof RevisionableContentEntityBase) {
$uri_route_parameters[$this
->getEntityTypeId() . '_revision'] = $this
->getRevisionId();
}
return $uri_route_parameters;
}
}