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"
27 * admin_permission = "administer entityqueue",
28 * config_prefix = "entity_queue",
29 * bundle_of = "entity_subqueue",
37 * "edit-form" = "/admin/structure/entityqueue/{entity_queue}",
38 * "delete-form" = "/admin/structure/entityqueue/{entity_queue}/delete",
39 * "collection" = "/admin/structure/entityqueue",
40 * "enable" = "/admin/structure/entityqueue/{entity_queue}/enable",
41 * "disable" = "/admin/structure/entityqueue/{entity_queue}/disable",
42 * "subqueue-list" = "/admin/structure/entityqueue/{entity_queue}/list"
48 * "handler_configuration",
54 class EntityQueue extends ConfigEntityBundleBase implements EntityQueueInterface, EntityWithPluginCollectionInterface {
64 * The EntityQueue label.
71 * The entity selection settings used for the subqueue's 'items' field.
75 protected $entity_settings = [
76 'target_type' => 'node',
77 'handler' => 'default',
78 'handler_settings' => [],
86 protected $queue_settings = [
89 'act_as_queue' => FALSE,
90 'reverse_in_admin' => FALSE,
94 * The ID of the EntityQueueHandler.
98 protected $handler = 'simple';
101 * An array to store and load the EntityQueueHandler plugin configuration.
105 protected $handler_configuration = [];
108 * The EntityQueueHandler plugin.
110 * @var \Drupal\entityqueue\EntityQueueHandlerPluginCollection
112 protected $handlerPluginCollection;
117 public function getTargetEntityTypeId() {
118 return $this->entity_settings['target_type'];
124 public function getMinimumSize() {
125 return $this->queue_settings['min_size'];
131 public function getMaximumSize() {
132 return $this->queue_settings['max_size'];
138 public function getActAsQueue() {
139 return $this->queue_settings['act_as_queue'];
145 public function getReverseInAdmin() {
146 return isset($this->queue_settings['reverse_in_admin']) ? $this->queue_settings['reverse_in_admin'] : FALSE;
152 public function getEntitySettings() {
153 return $this->entity_settings;
159 public function getQueueSettings() {
160 return $this->queue_settings + [
161 // Ensure that we always have an empty array by default for the
162 // 'handler_settings', regardless of the incoming form values.
163 'handler_settings' => []
170 public function getHandler() {
171 return $this->handler;
177 public function setHandler($handler) {
178 $this->handler = $handler;
179 $this->getPluginCollection()->addInstanceID($handler, []);
187 public function getHandlerPlugin() {
188 return $this->getPluginCollection()->get($this->handler);
194 public function getPluginCollections() {
195 return ['handler_configuration' => $this->getPluginCollection()];
199 * Encapsulates the creation of the EntityQueueHandlerPluginCollection.
201 * @return \Drupal\entityqueue\EntityQueueHandlerPluginCollection
202 * The entity queue's plugin collection.
204 protected function getPluginCollection() {
205 if (!$this->handlerPluginCollection) {
206 $this->handlerPluginCollection = new EntityQueueHandlerPluginCollection(
207 \Drupal::service('plugin.manager.entityqueue.handler'),
208 $this->handler, $this->handler_configuration, $this);
210 return $this->handlerPluginCollection;
216 public function calculateDependencies() {
217 parent::calculateDependencies();
219 // Ensure that the queue depends on the module that provides the target
221 $target_entity_type = \Drupal::entityTypeManager()->getDefinition($this->getTargetEntityTypeId());
222 $this->addDependency('module', $target_entity_type->getProvider());
230 public function preSave(EntityStorageInterface $storage) {
231 parent::preSave($storage);
233 $this->getHandlerPlugin()->onQueuePreSave($this, $storage);
239 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
240 parent::postSave($storage, $update);
242 $this->getHandlerPlugin()->onQueuePostSave($this, $storage, $update);
248 public static function preDelete(EntityStorageInterface $storage, array $entities) {
249 parent::preDelete($storage, $entities);
251 foreach ($entities as $queue) {
252 $queue->getHandlerPlugin()->onQueuePreDelete($queue, $storage);
259 public static function postDelete(EntityStorageInterface $storage, array $entities) {
260 parent::postDelete($storage, $entities);
262 foreach ($entities as $queue) {
263 $queue->getHandlerPlugin()->onQueuePostDelete($queue, $storage);
270 public static function postLoad(EntityStorageInterface $storage, array &$entities) {
271 parent::postLoad($storage, $entities);
273 foreach ($entities as $queue) {
274 $queue->getHandlerPlugin()->onQueuePostLoad($queue, $storage);
281 protected function invalidateTagsOnSave($update) {
282 // In addition to the parent implementation, we also need to invalidate
283 // queue-specific cache tags.
284 $tags = Cache::mergeTags($this->getEntityType()->getListCacheTags(), $this->getCacheTagsToInvalidate());
286 Cache::invalidateTags($tags);
292 * Override to never invalidate the individual entities' cache tags; the
293 * config system already invalidates them.
295 protected static function invalidateTagsOnDelete(EntityTypeInterface $entity_type, array $entities) {
296 $tags = $entity_type->getListCacheTags();
298 // In addition to the parent implementation, we also need to invalidate
299 // queue-specific cache tags.
300 foreach ($entities as $entity) {
301 $tags = Cache::mergeTags($tags, $entity->getCacheTagsToInvalidate());
304 Cache::invalidateTags($tags);
310 public function getCacheTagsToInvalidate() {
311 // A newly created or deleted queue could alter views data relationships, so
312 // we must invalidate the associated 'views_data' cache tag.
313 return Cache::mergeTags(parent::getCacheTagsToInvalidate(), ['views_data', 'entity_field_info']);
319 public static function loadMultipleByTargetType($target_entity_type_id) {
320 $ids = \Drupal::entityTypeManager()->getStorage('entity_queue')->getQuery()
321 ->condition('entity_settings.target_type', $target_entity_type_id)
324 return $ids ? static::loadMultiple($ids) : [];