View source
<?php
namespace Drupal\entityqueue\Entity;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EditorialContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\entityqueue\EntityQueueInterface;
use Drupal\entityqueue\EntitySubqueueInterface;
use Drupal\entityqueue\EntitySubqueueItemsFieldItemList;
use Drupal\user\EntityOwnerTrait;
class EntitySubqueue extends EditorialContentEntityBase implements EntitySubqueueInterface {
use EntityChangedTrait;
use EntityOwnerTrait;
public function access($operation = 'view', AccountInterface $account = NULL, $return_as_object = FALSE) {
if ($operation == 'create') {
return parent::access($operation, $account, $return_as_object);
}
return \Drupal::entityTypeManager()
->getAccessControlHandler($this->entityTypeId)
->access($this, $operation, $account, $return_as_object);
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
$queue = $this
->getQueue();
$max_size = $queue
->getMaximumSize();
$items = $this
->get('items')
->getValue();
if ($queue
->getActAsQueue() && count($items) > $max_size) {
if ($queue
->isReversed()) {
$items = array_slice($items, 0, $max_size);
}
else {
$items = array_slice($items, -$max_size);
}
$this
->set('items', $items);
}
if (!$this
->getRevisionUser()) {
$this
->setRevisionUserId($this
->getOwnerId());
}
}
public function getQueue() {
return $this
->get('queue')->entity;
}
public function setQueue(EntityQueueInterface $queue) {
$this
->set('queue', $queue
->id());
return $this;
}
public function getTitle() {
return $this
->get('title')->value;
}
public function setTitle($title) {
$this
->set('title', $title);
return $this;
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields += static::ownerBaseFieldDefinitions($entity_type);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The ID (machine name) of the subqueue.'))
->setReadOnly(TRUE)
->setSetting('is_ascii', TRUE);
$fields['queue']
->setDescription(t('The queue (bundle) of this subqueue.'));
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Title'))
->setRequired(TRUE)
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setSetting('max_length', 191)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -10,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -10,
])
->setDisplayConfigurable('form', TRUE);
$fields['items'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Items'))
->setClass(EntitySubqueueItemsFieldItemList::class)
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setSetting('target_type', 'entity_subqueue')
->setRevisionable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'entity_reference_label',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'entityqueue_dragtable',
'weight' => 5,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['uid']
->setRevisionable(TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Authored on'))
->setDescription(t('The time that the subqueue was created.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the subqueue was last edited.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE);
$fields['revision_log_message']
->setDisplayOptions('form', [
'region' => 'hidden',
]);
return $fields;
}
public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
if ($queue = EntityQueue::load($bundle)) {
$fields['items'] = clone $base_field_definitions['items'];
$fields['items']
->setSettings($queue
->getEntitySettings());
return $fields;
}
return [];
}
public function addItem(EntityInterface $entity) {
$this
->get('items')
->appendItem($entity
->id());
return $this;
}
public function removeItem(EntityInterface $entity) {
$index = $this
->getItemPosition($entity);
if ($index !== FALSE) {
$this
->get('items')
->offsetUnset($index);
}
return $this;
}
public function hasItem(EntityInterface $entity) {
return $this
->getItemPosition($entity) !== FALSE;
}
public function getItemPosition(EntityInterface $entity) {
$subqueue_items = $this
->get('items')
->getValue();
$subqueue_items_ids = array_map(function ($item) {
return $item['target_id'];
}, $subqueue_items);
return array_search($entity
->id(), $subqueue_items_ids);
}
public function reverseItems() {
$subqueue_items = $this
->get('items')
->getValue();
$this
->get('items')
->setValue(array_reverse($subqueue_items));
return $this;
}
public function shuffleItems() {
$subqueue_items = $this
->get('items')
->getValue();
shuffle($subqueue_items);
$this
->get('items')
->setValue($subqueue_items);
return $this;
}
public function clearItems() {
$this
->get('items')
->setValue(NULL);
return $this;
}
public function toUrl($rel = 'canonical', array $options = []) {
$url = parent::toUrl($rel, $options);
$url
->setRouteParameter('entity_queue', $this
->bundle());
return $url;
}
public function getCacheTagsToInvalidate() {
$tags = [];
if ($queue = $this
->getQueue()) {
$tags = Cache::mergeTags(parent::getCacheTagsToInvalidate(), $queue
->getCacheTags());
$target_entity_type = $this
->entityTypeManager()
->getDefinition($this
->getQueue()
->getTargetEntityTypeId());
$tags = Cache::mergeTags($tags, $target_entity_type
->getListCacheTags());
}
return $tags;
}
}