Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / entityqueue / src / Entity / EntityQueue.php
1 <?php
2
3 namespace Drupal\entityqueue\Entity;
4
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;
12
13 /**
14  * Defines the EntityQueue entity class.
15  *
16  * @ConfigEntityType(
17  *   id = "entity_queue",
18  *   label = @Translation("Entity queue"),
19  *   handlers = {
20  *     "list_builder" = "Drupal\entityqueue\EntityQueueListBuilder",
21  *     "form" = {
22  *       "add" = "Drupal\entityqueue\Form\EntityQueueForm",
23  *       "edit" = "Drupal\entityqueue\Form\EntityQueueForm",
24  *       "delete" = "Drupal\Core\Entity\EntityDeleteForm"
25  *     },
26  *     "access" = "Drupal\entityqueue\EntityQueueAccessControlHandler",
27  *   },
28  *   admin_permission = "administer entityqueue",
29  *   config_prefix = "entity_queue",
30  *   bundle_of = "entity_subqueue",
31  *   entity_keys = {
32  *     "id" = "id",
33  *     "label" = "label",
34  *     "uuid" = "uuid",
35  *     "status" = "status"
36  *   },
37  *   links = {
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"
44  *   },
45  *   config_export = {
46  *     "id",
47  *     "label",
48  *     "handler",
49  *     "handler_configuration",
50  *     "entity_settings",
51  *     "queue_settings"
52  *   }
53  * )
54  */
55 class EntityQueue extends ConfigEntityBundleBase implements EntityQueueInterface, EntityWithPluginCollectionInterface {
56
57   /**
58    * The EntityQueue ID.
59    *
60    * @var string
61    */
62   protected $id;
63
64   /**
65    * The EntityQueue label.
66    *
67    * @var string
68    */
69   protected $label;
70
71   /**
72    * The entity selection settings used for the subqueue's 'items' field.
73    *
74    * @var array
75    */
76   protected $entity_settings = [
77     'target_type' => 'node',
78     'handler' => 'default',
79     'handler_settings' => [],
80   ];
81
82   /**
83    * The queue settings.
84    *
85    * @var array
86    */
87   protected $queue_settings = [
88     'min_size' => 0,
89     'max_size' => 0,
90     'act_as_queue' => FALSE,
91     'reverse_in_admin' => FALSE,
92   ];
93
94   /**
95    * The ID of the EntityQueueHandler.
96    *
97    * @var string
98    */
99   protected $handler = 'simple';
100
101   /**
102    * An array to store and load the EntityQueueHandler plugin configuration.
103    *
104    * @var array
105    */
106   protected $handler_configuration = [];
107
108   /**
109    * The EntityQueueHandler plugin.
110    *
111    * @var \Drupal\entityqueue\EntityQueueHandlerPluginCollection
112    */
113   protected $handlerPluginCollection;
114
115   /**
116    * {@inheritdoc}
117    */
118   public function getTargetEntityTypeId() {
119     return $this->entity_settings['target_type'];
120   }
121
122   /**
123    * {@inheritdoc}
124    */
125   public function getMinimumSize() {
126     return $this->queue_settings['min_size'];
127   }
128
129   /**
130    * {@inheritdoc}
131    */
132   public function getMaximumSize() {
133     return $this->queue_settings['max_size'];
134   }
135
136   /**
137    * {@inheritdoc}
138    */
139   public function getActAsQueue() {
140     return $this->queue_settings['act_as_queue'];
141   }
142
143   /**
144    * {@inheritdoc}
145    */
146   public function getReverseInAdmin() {
147     return isset($this->queue_settings['reverse_in_admin']) ? $this->queue_settings['reverse_in_admin'] : FALSE;
148   }
149
150   /**
151    * {@inheritdoc}
152    */
153   public function getEntitySettings() {
154     return $this->entity_settings;
155   }
156
157   /**
158    * {@inheritdoc}
159    */
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' => []
165     ];
166   }
167
168   /**
169    * {@inheritdoc}
170    */
171   public function getHandler() {
172     return $this->handler;
173   }
174
175   /**
176    * {@inheritdoc}
177    */
178   public function getHandlerConfiguration() {
179     return $this->handler_configuration;
180   }
181
182   /**
183    * {@inheritdoc}
184    */
185   public function setHandler($handler_id) {
186     $this->handler = $handler_id;
187     $this->getPluginCollection()->addInstanceID($handler_id, []);
188
189     return $this;
190   }
191
192   /**
193    * {@inheritdoc}
194    */
195   public function getHandlerPlugin() {
196     return $this->getPluginCollection()->get($this->handler);
197   }
198
199   /**
200    * {@inheritdoc}
201    */
202   public function setHandlerPlugin($handler) {
203     $this->getPluginCollection()->set($handler->getPluginId(), $handler);
204
205     return $this;
206   }
207
208   /**
209    * {@inheritdoc}
210    */
211   public function getPluginCollections() {
212     return ['handler_configuration' => $this->getPluginCollection()];
213   }
214
215   /**
216    * Encapsulates the creation of the EntityQueueHandlerPluginCollection.
217    *
218    * @return \Drupal\entityqueue\EntityQueueHandlerPluginCollection
219    *   The entity queue's plugin collection.
220    */
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);
226     }
227     return $this->handlerPluginCollection;
228   }
229
230   /**
231    * {@inheritdoc}
232    */
233   public function calculateDependencies() {
234     parent::calculateDependencies();
235
236     // Ensure that the queue depends on the module that provides the target
237     // entity type.
238     $target_entity_type = \Drupal::entityTypeManager()->getDefinition($this->getTargetEntityTypeId());
239     $this->addDependency('module', $target_entity_type->getProvider());
240
241     return $this;
242   }
243
244   /**
245    * {@inheritdoc}
246    */
247   public function preSave(EntityStorageInterface $storage) {
248     parent::preSave($storage);
249
250     $this->getHandlerPlugin()->onQueuePreSave($this, $storage);
251   }
252
253   /**
254    * {@inheritdoc}
255    */
256   public function postSave(EntityStorageInterface $storage, $update = TRUE) {
257     parent::postSave($storage, $update);
258
259     $this->getHandlerPlugin()->onQueuePostSave($this, $storage, $update);
260   }
261
262   /**
263    * {@inheritdoc}
264    */
265   public static function preDelete(EntityStorageInterface $storage, array $entities) {
266     parent::preDelete($storage, $entities);
267
268     foreach ($entities as $queue) {
269       $queue->getHandlerPlugin()->onQueuePreDelete($queue, $storage);
270     }
271   }
272
273   /**
274    * {@inheritdoc}
275    */
276   public static function postDelete(EntityStorageInterface $storage, array $entities) {
277     parent::postDelete($storage, $entities);
278
279     foreach ($entities as $queue) {
280       $queue->getHandlerPlugin()->onQueuePostDelete($queue, $storage);
281     }
282   }
283
284   /**
285    * {@inheritdoc}
286    */
287   public static function postLoad(EntityStorageInterface $storage, array &$entities) {
288     parent::postLoad($storage, $entities);
289
290     foreach ($entities as $queue) {
291       $queue->getHandlerPlugin()->onQueuePostLoad($queue, $storage);
292     }
293   }
294
295   /**
296    * {@inheritdoc}
297    */
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());
302
303     Cache::invalidateTags($tags);
304   }
305
306   /**
307    * {@inheritdoc}
308    *
309    * Override to never invalidate the individual entities' cache tags; the
310    * config system already invalidates them.
311    */
312   protected static function invalidateTagsOnDelete(EntityTypeInterface $entity_type, array $entities) {
313     $tags = $entity_type->getListCacheTags();
314
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());
319     }
320
321     Cache::invalidateTags($tags);
322   }
323
324   /**
325    * {@inheritdoc}
326    */
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']);
331   }
332
333   /**
334    * {@inheritdoc}
335    */
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)
339       ->execute();
340
341     return $ids ? static::loadMultiple($ids) : [];
342   }
343
344 }