View source
<?php
namespace Drupal\entityqueue\Entity;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
use Drupal\entityqueue\EntityQueueHandlerPluginCollection;
use Drupal\entityqueue\EntityQueueInterface;
class EntityQueue extends ConfigEntityBundleBase implements EntityQueueInterface, EntityWithPluginCollectionInterface {
protected $id;
protected $label;
protected $entity_settings = [
'target_type' => 'node',
'handler' => 'default',
'handler_settings' => [],
];
protected $queue_settings = [
'min_size' => 0,
'max_size' => 0,
'act_as_queue' => FALSE,
'reverse' => FALSE,
];
protected $handler = 'simple';
protected $handler_configuration = [];
protected $handlerPluginCollection;
public function getTargetEntityTypeId() {
return $this->entity_settings['target_type'];
}
public function getMinimumSize() {
return $this->queue_settings['min_size'];
}
public function getMaximumSize() {
return $this->queue_settings['max_size'];
}
public function getActAsQueue() {
return $this->queue_settings['act_as_queue'];
}
public function isReversed() {
return isset($this->queue_settings['reverse']) ? $this->queue_settings['reverse'] : FALSE;
}
public function getEntitySettings() {
return $this->entity_settings + [
'handler_settings' => [],
];
}
public function getQueueSettings() {
return $this->queue_settings;
}
public function getHandler() {
return $this->handler;
}
public function getHandlerConfiguration() {
return $this->handler_configuration;
}
public function setHandler($handler_id) {
$this->handler = $handler_id;
$this
->getPluginCollection()
->addInstanceID($handler_id, []);
return $this;
}
public function getHandlerPlugin() {
return $this
->getPluginCollection()
->get($this->handler);
}
public function setHandlerPlugin($handler) {
$this
->getPluginCollection()
->set($handler
->getPluginId(), $handler);
return $this;
}
public function getPluginCollections() {
return [
'handler_configuration' => $this
->getPluginCollection(),
];
}
protected function getPluginCollection() {
if (!$this->handlerPluginCollection) {
$this->handlerPluginCollection = new EntityQueueHandlerPluginCollection(\Drupal::service('plugin.manager.entityqueue.handler'), $this->handler, $this->handler_configuration, $this);
}
return $this->handlerPluginCollection;
}
public function calculateDependencies() {
parent::calculateDependencies();
$target_entity_type = \Drupal::entityTypeManager()
->getDefinition($this
->getTargetEntityTypeId());
$this
->addDependency('module', $target_entity_type
->getProvider());
return $this;
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
$this
->getHandlerPlugin()
->onQueuePreSave($this, $storage);
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
$this
->getHandlerPlugin()
->onQueuePostSave($this, $storage, $update);
}
public static function preDelete(EntityStorageInterface $storage, array $entities) {
parent::preDelete($storage, $entities);
foreach ($entities as $queue) {
$queue
->getHandlerPlugin()
->onQueuePreDelete($queue, $storage);
}
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
foreach ($entities as $queue) {
$queue
->getHandlerPlugin()
->onQueuePostDelete($queue, $storage);
}
}
public static function postLoad(EntityStorageInterface $storage, array &$entities) {
parent::postLoad($storage, $entities);
foreach ($entities as $queue) {
$queue
->getHandlerPlugin()
->onQueuePostLoad($queue, $storage);
}
}
protected function invalidateTagsOnSave($update) {
$tags = Cache::mergeTags($this
->getEntityType()
->getListCacheTags(), $this
->getCacheTagsToInvalidate());
Cache::invalidateTags($tags);
}
protected static function invalidateTagsOnDelete(EntityTypeInterface $entity_type, array $entities) {
$tags = $entity_type
->getListCacheTags();
foreach ($entities as $entity) {
$tags = Cache::mergeTags($tags, $entity
->getCacheTagsToInvalidate());
}
Cache::invalidateTags($tags);
}
public function getCacheTagsToInvalidate() {
return Cache::mergeTags(parent::getCacheTagsToInvalidate(), [
'views_data',
'entity_field_info',
]);
}
public static function loadMultipleByTargetType($target_entity_type_id) {
$ids = \Drupal::entityTypeManager()
->getStorage('entity_queue')
->getQuery()
->condition('entity_settings.target_type', $target_entity_type_id)
->execute();
return $ids ? static::loadMultiple($ids) : [];
}
}