diff --git a/config/schema/entityqueue.schema.yml b/config/schema/entityqueue.schema.yml index 8c6cef9..fd21d85 100644 --- a/config/schema/entityqueue.schema.yml +++ b/config/schema/entityqueue.schema.yml @@ -41,3 +41,6 @@ entityqueue.entity_queue.*: act_as_queue: type: boolean label: 'Act as queue' + reverse_in_admin: + type: boolean + label: 'Reverse order in admin view' diff --git a/src/Entity/EntityQueue.php b/src/Entity/EntityQueue.php index 54cfef3..706bd14 100644 --- a/src/Entity/EntityQueue.php +++ b/src/Entity/EntityQueue.php @@ -87,6 +87,7 @@ class EntityQueue extends ConfigEntityBundleBase implements EntityQueueInterface 'min_size' => 0, 'max_size' => 0, 'act_as_queue' => FALSE, + 'reverse_in_admin' => FALSE, ]; /** @@ -141,6 +142,13 @@ class EntityQueue extends ConfigEntityBundleBase implements EntityQueueInterface /** * {@inheritdoc} */ + public function getReverseInAdmin() { + return isset($this->queue_settings['reverse_in_admin']) ? $this->queue_settings['reverse_in_admin'] : FALSE; + } + + /** + * {@inheritdoc} + */ public function getEntitySettings() { return $this->entity_settings; } diff --git a/src/EntityQueueInterface.php b/src/EntityQueueInterface.php index 2af3e74..d861287 100644 --- a/src/EntityQueueInterface.php +++ b/src/EntityQueueInterface.php @@ -67,6 +67,20 @@ interface EntityQueueInterface extends ConfigEntityInterface { public function getActAsQueue(); /** + * Returns the behavior of editing the queue's items. + * + * Ordinarily, queues are arranged with the front of the queue (where items + * will be removed) on top, and the back (where items will be added) on the + * bottom. + * + * If TRUE, this will display the queue such that items will be added to the + * top and removed from the bottom. + * + * @return bool + */ + public function getReverseInAdmin(); + + /** * Gets the selection settings used by a subqueue's 'items' reference field. * * @return array @@ -88,6 +102,8 @@ interface EntityQueueInterface extends ConfigEntityInterface { * - max_size: The maximum number of items that this queue can hold. * - act_as_queue: The behavior of exceeding the maximum number of queue * items. + * - reverse_in_admin: Show the items in reverse order when editing a + * subqueue. */ public function getQueueSettings(); diff --git a/src/Form/EntityQueueForm.php b/src/Form/EntityQueueForm.php index 5a49679..2b5114a 100644 --- a/src/Form/EntityQueueForm.php +++ b/src/Form/EntityQueueForm.php @@ -160,6 +160,12 @@ class EntityQueueForm extends BundleEntityFormBase { ], ], ]; + $form['queue_settings']['reverse_in_admin'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Reverse order in admin view'), + '#default_value' => $queue->getReverseInAdmin(), + '#description' => $this->t('Ordinarily queues are arranged with the front of the queue (where items will be removed) on top and the back (where items will be added) on the bottom. If checked, this will display the queue such that items will be added to the top and removed from the bottom.'), + ]; // We have to duplicate all the code from // \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::fieldSettingsForm() diff --git a/src/Form/EntitySubqueueForm.php b/src/Form/EntitySubqueueForm.php index 68000f5..e6c60c9 100644 --- a/src/Form/EntitySubqueueForm.php +++ b/src/Form/EntitySubqueueForm.php @@ -52,6 +52,14 @@ class EntitySubqueueForm extends ContentEntityForm { * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { + // Reverse the items in the admin form if the queue uses the 'Reverse order + // in admin view' option. + if ($this->entity->getQueue()->getReverseInAdmin()) { + $subqueue_items = $this->entity->get('items'); + $items_values = $subqueue_items->getValue(); + $subqueue_items->setValue(array_reverse($items_values)); + } + $form = parent::form($form, $form_state); $form['#title'] = $this->t('Edit subqueue %label', ['%label' => $this->entity->label()]); @@ -155,6 +163,11 @@ class EntitySubqueueForm extends ContentEntityForm { $items_widget->extractFormValues($subqueue_items, $form, $form_state); $items_values = $subqueue_items->getValue(); + // Revert the effect of the 'Reverse order in admin view' option. + if ($entity->getQueue()->getReverseInAdmin()) { + $items_values = array_reverse($items_values); + } + switch ($op) { case 'reverse': $subqueue_items->setValue(array_reverse($items_values)); @@ -221,6 +234,14 @@ class EntitySubqueueForm extends ContentEntityForm { */ public function save(array $form, FormStateInterface $form_state) { $subqueue = $this->entity; + + // Revert the effect of the 'Reverse order in admin view' option. + if ($subqueue->getQueue()->getReverseInAdmin()) { + $subqueue_items = $subqueue->get('items'); + $items_values = $subqueue_items->getValue(); + $subqueue_items->setValue(array_reverse($items_values)); + } + $status = $subqueue->save(); $edit_link = $subqueue->toLink($this->t('Edit'), 'edit-form')->toString();