3 namespace Drupal\entityqueue\Entity;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
7 use Drupal\Core\Entity\EntityStorageInterface;
8 use Drupal\Core\Entity\EntityTypeInterface;
9 use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
10 use Drupal\entityqueue\EntityQueueHandlerPluginCollection;
11 use Drupal\entityqueue\EntityQueueInterface;
14 * Defines the EntityQueue entity class.
17 * id = "entity_queue",
18 * label = @Translation("Entity queue"),
20 * "list_builder" = "Drupal\entityqueue\EntityQueueListBuilder",
22 * "add" = "Drupal\entityqueue\Form\EntityQueueForm",
23 * "edit" = "Drupal\entityqueue\Form\EntityQueueForm",
24 * "delete" = "Drupal\Core\Entity\EntityDeleteForm"
26 * "access" = "Drupal\entityqueue\EntityQueueAccessControlHandler",
28 * admin_permission = "administer entityqueue",
29 * config_prefix = "entity_queue",
30 * bundle_of = "entity_subqueue",
38 * "edit-form" = "/admin/structure/entityqueue/{entity_queue}",
39 * "delete-form" = "/admin/structure/entityqueue/{entity_queue}/delete",
40 * "collection" = "/admin/structure/entityqueue",
41 * "enable" = "/admin/structure/entityqueue/{entity_queue}/enable",
42 * "disable" = "/admin/structure/entityqueue/{entity_queue}/disable",
43 * "subqueue-list" = "/admin/structure/entityqueue/{entity_queue}/list"
49 * "handler_configuration",
55 class EntityQueue extends ConfigEntityBundleBase implements EntityQueueInterface, EntityWithPluginCollectionInterface {
65 * The EntityQueue label.
72 * The entity selection settings used for the subqueue's 'items' field.
76 protected $entity_settings = [
77 'target_type' => 'node',
78 'handler' => 'default',
79 'handler_settings' => [],
87 protected $queue_settings = [
90 'act_as_queue' => FALSE,
91 'reverse_in_admin' => FALSE,
95 * The ID of the EntityQueueHandler.
99 protected $handler = 'simple';
102 * An array to store and load the EntityQueueHandler plugin configuration.
106 protected $handler_configuration = [];
109 * The EntityQueueHandler plugin.
111 * @var \Drupal\entityqueue\EntityQueueHandlerPluginCollection
113 protected $handlerPluginCollection;
118 public function getTargetEntityTypeId() {
119 return $this->entity_settings['target_type'];
125 public function getMinimumSize() {
126 return $this->queue_settings['min_size'];
132 public function getMaximumSize() {
133 return $this->queue_settings['max_size'];
139 public function getActAsQueue() {
140 return $this->queue_settings['act_as_queue'];
146 public function getReverseInAdmin() {
147 return isset($this->queue_settings['reverse_in_admin']) ? $this->queue_settings['reverse_in_admin'] : FALSE;
153 public function getEntitySettings() {
154 return $this->entity_settings;
160 public function getQueueSettings() {
161 return $this->queue_settings + [
162 // Ensure that we always have an empty array by default for the
163 // 'handler_settings', regardless of the incoming form values.
164 'handler_settings' => []
171 public function getHandler() {
172 return $this->handler;
178 public function getHandlerConfiguration() {
179 return $this->handler_configuration;
185 public function setHandler($handler_id) {
186 $this->handler = $handler_id;
187 $this->getPluginCollection()->addInstanceID($handler_id, []);
195 public function getHandlerPlugin() {
196 return $this->getPluginCollection()->get($this->handler);
202 public function setHandlerPlugin($handler) {
203 $this->getPluginCollection()->set($handler->getPluginId(), $handler);
211 public function getPluginCollections() {
212 return ['handler_configuration' => $this->getPluginCollection()];
216 * Encapsulates the creation of the EntityQueueHandlerPluginCollection.
218 * @return \Drupal\entityqueue\EntityQueueHandlerPluginCollection
219 * The entity queue's plugin collection.
221 protected function getPluginCollection() {
222 if (!$this->handlerPluginCollection) {
223 $this->handlerPluginCollection = new EntityQueueHandlerPluginCollection(
224 \Drupal::service('plugin.manager.entityqueue.handler'),
225 $this->handler, $this->handler_configuration, $this);
227 return $this->handlerPluginCollection;
233 public function calculateDependencies() {
234 parent::calculateDependencies();
236 // Ensure that the queue depends on the module that provides the target
238 $target_entity_type = \Drupal::entityTypeManager()->getDefinition($this->getTargetEntityTypeId());
239 $this->addDependency('module', $target_entity_type->getProvider());
247 public function preSave(EntityStorageInterface $storage) {
248 parent::preSave($storage);
250 $this->getHandlerPlugin()->onQueuePreSave($this, $storage);
256 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
257 parent::postSave($storage, $update);
259 $this->getHandlerPlugin()->onQueuePostSave($this, $storage, $update);
265 public static function preDelete(EntityStorageInterface $storage, array $entities) {
266 parent::preDelete($storage, $entities);
268 foreach ($entities as $queue) {
269 $queue->getHandlerPlugin()->onQueuePreDelete($queue, $storage);
276 public static function postDelete(EntityStorageInterface $storage, array $entities) {
277 parent::postDelete($storage, $entities);
279 foreach ($entities as $queue) {
280 $queue->getHandlerPlugin()->onQueuePostDelete($queue, $storage);
287 public static function postLoad(EntityStorageInterface $storage, array &$entities) {
288 parent::postLoad($storage, $entities);
290 foreach ($entities as $queue) {
291 $queue->getHandlerPlugin()->onQueuePostLoad($queue, $storage);
298 protected function invalidateTagsOnSave($update) {
299 // In addition to the parent implementation, we also need to invalidate
300 // queue-specific cache tags.
301 $tags = Cache::mergeTags($this->getEntityType()->getListCacheTags(), $this->getCacheTagsToInvalidate());
303 Cache::invalidateTags($tags);
309 * Override to never invalidate the individual entities' cache tags; the
310 * config system already invalidates them.
312 protected static function invalidateTagsOnDelete(EntityTypeInterface $entity_type, array $entities) {
313 $tags = $entity_type->getListCacheTags();
315 // In addition to the parent implementation, we also need to invalidate
316 // queue-specific cache tags.
317 foreach ($entities as $entity) {
318 $tags = Cache::mergeTags($tags, $entity->getCacheTagsToInvalidate());
321 Cache::invalidateTags($tags);
327 public function getCacheTagsToInvalidate() {
328 // A newly created or deleted queue could alter views data relationships, so
329 // we must invalidate the associated 'views_data' cache tag.
330 return Cache::mergeTags(parent::getCacheTagsToInvalidate(), ['views_data', 'entity_field_info']);
336 public static function loadMultipleByTargetType($target_entity_type_id) {
337 $ids = \Drupal::entityTypeManager()->getStorage('entity_queue')->getQuery()
338 ->condition('entity_settings.target_type', $target_entity_type_id)
341 return $ids ? static::loadMultiple($ids) : [];