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