--- /dev/null
+<?php
+
+/**
+ * @file
+ * Allows users to collect entities in arbitrarily ordered lists.
+ */
+
+use Drupal\Core\Access\AccessResult;
+use Drupal\Core\Entity\EntityInterface;
+use Drupal\Core\Field\FieldDefinitionInterface;
+use Drupal\Core\Field\FieldItemListInterface;
+use Drupal\Core\Session\AccountInterface;
+use Drupal\views\ViewExecutable;
+use Drupal\entityqueue\Entity\EntityQueue;
+use Drupal\entityqueue\Entity\EntitySubqueue;
+
+/**
+ * Implements hook_entity_field_access().
+ */
+function entityqueue_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
+ // Only allow edit access on a subqueue title field if the queue doesn't have
+ // automated subqueues.
+ if ($operation == 'edit' && $field_definition->getName() == 'title' && $items && $items->getEntity()->getEntityTypeId() === 'entity_subqueue') {
+ $queue = $items->getEntity()->getQueue();
+ return AccessResult::forbiddenIf($queue->getHandlerPlugin()->hasAutomatedSubqueues());
+ }
+
+ return AccessResult::neutral();
+}
+
+/**
+ * Implements hook_views_pre_render().
+ *
+ * Add contexual links to views before rendering.
+ */
+function entityqueue_views_pre_render(ViewExecutable $view) {
+ // Do not add contextual link on view preview.
+ if (\Drupal::moduleHandler()->moduleExists('views_ui') && views_ui_contextual_links_suppress()) {
+ return;
+ }
+
+ // Proceed only if there is entityqueue sort criteria available.
+ if (!$sort_key = entityqueue_get_entityqueue_sort($view)) {
+ return;
+ }
+
+ // Get view display relationships.
+ $relationships = $view->relationship;
+ foreach ($relationships as $relationship) {
+ if ($relationship->field == 'entityqueue_relationship') {
+ $referenced_subqueues = (array) $relationship->options['limit_queue'];
+
+ // Contextual links can handle only one set of links coming from a module,
+ // so we'll have to settle for the first referenced queue.
+ if (!empty($referenced_subqueues) && ($subqueue = EntitySubqueue::load(reset($referenced_subqueues)))) {
+ $route_parameters = [
+ 'entity_queue' => $subqueue->getQueue()->id(),
+ 'entity_subqueue' => $subqueue->id(),
+ ];
+ $view->element['#contextual_links']['entityqueue'] = [
+ 'route_parameters' => $route_parameters,
+ ];
+ }
+ }
+ }
+}
+
+/**
+ * Implements hook_contextual_links_view_alter().
+ *
+ * Change Entityqueue on views into offcanvas links if available.
+ */
+function entityqueue_contextual_links_view_alter(&$element, $items) {
+ if (\Drupal::moduleHandler()->moduleExists('outside_in') && isset($element['#links']['entityentity-subqueueedit-form'])) {
+ $element['#links']['entityentity-subqueueedit-form']['attributes'] = [
+ 'class' => ['use-ajax'],
+ 'data-dialog-type' => 'dialog',
+ 'data-dialog-renderer' => 'offcanvas',
+ 'data-outside-in-edit' => TRUE,
+ ];
+
+ $element['#attached']['library'][] = 'outside_in/drupal.off_canvas';
+ }
+}
+
+/**
+ * Get the entityqueue position sort of a view if there is one and return its
+ * ID. If there are multiple of these sorts the first is returned.
+ *
+ * @param $view
+ * The view object.
+ *
+ * @return
+ * The ID of the sort or FALSE if there isn't one.
+ */
+function entityqueue_get_entityqueue_sort($view) {
+ foreach ($view->sort as $id => $sort) {
+ if ($sort->definition['id'] == 'entity_queue_position') {
+ return $id;
+ }
+ }
+ return FALSE;
+}
+
+/**
+ * Implements hook_entity_delete().
+ *
+ * @todo Remove this when https://www.drupal.org/node/2723323 is fixed.
+ */
+function entityqueue_entity_delete(EntityInterface $entity) {
+ // Get all the entity queues referencing the targets entity type.
+ $queues = EntityQueue::loadMultipleByTargetType($entity->getEntityTypeId());
+ foreach ($queues as $queue) {
+ $entity_settings = $queue->getEntitySettings();
+
+ // Check if the queue's bundle also matches that of the target entity.
+ if (is_array($entity_settings['handler_settings']['target_bundles']) && in_array($entity->bundle(), $entity_settings['handler_settings']['target_bundles'])) {
+ // Get subqueues.
+ $query = \Drupal::entityQuery('entity_subqueue')->condition('queue', $queue->id());
+ $result = $query->execute();
+ $subqueues = EntitySubqueue::loadMultiple($result);
+
+ // Check if the entity is referenced in a subqueue.
+ foreach ($subqueues as $subqueue) {
+ $items = $subqueue->get('items')->getValue();
+ if (($item_key = array_search($entity->id(), array_column($items, 'target_id'))) !== FALSE) {
+ unset($items[$item_key]);
+ $subqueue->set('items', $items);
+ $subqueue->save();
+ }
+ }
+ }
+ }
+}