Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / entityqueue / entityqueue.module
1 <?php
2
3 /**
4  * @file
5  * Allows users to collect entities in arbitrarily ordered lists.
6  */
7
8 use Drupal\Core\Access\AccessResult;
9 use Drupal\Core\Entity\EntityInterface;
10 use Drupal\Core\Field\FieldDefinitionInterface;
11 use Drupal\Core\Field\FieldItemListInterface;
12 use Drupal\Core\Session\AccountInterface;
13 use Drupal\views\ViewExecutable;
14 use Drupal\entityqueue\Entity\EntityQueue;
15 use Drupal\entityqueue\Entity\EntitySubqueue;
16
17 /**
18  * Implements hook_entity_field_access().
19  */
20 function entityqueue_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
21   // Only allow edit access on a subqueue title field if the queue doesn't have
22   // automated subqueues.
23   if ($operation == 'edit' && $field_definition->getName() == 'title' && $items && $items->getEntity()->getEntityTypeId() === 'entity_subqueue') {
24     $queue = $items->getEntity()->getQueue();
25     return AccessResult::forbiddenIf($queue->getHandlerPlugin()->hasAutomatedSubqueues());
26   }
27
28   return AccessResult::neutral();
29 }
30
31 /**
32  * Implements hook_views_pre_render().
33  *
34  * Add contexual links to views before rendering.
35  */
36 function entityqueue_views_pre_render(ViewExecutable $view) {
37   // Do not add contextual link on view preview.
38   if (\Drupal::moduleHandler()->moduleExists('views_ui') && views_ui_contextual_links_suppress()) {
39     return;
40   }
41
42   // Allow to disable the contextual links.
43   if (!$view->display_handler->getOption('show_admin_links')) {
44     return;
45   }
46
47   // Get view display relationships.
48   $relationships = $view->relationship;
49   foreach ($relationships as $relationship) {
50     if ($relationship->field == 'entityqueue_relationship') {
51       $referenced_subqueues = (array) $relationship->options['limit_queue'];
52
53       // Contextual links can handle only one set of links coming from a module,
54       // so we'll have to settle for the first referenced queue.
55       if (!empty($referenced_subqueues) && ($subqueue = EntitySubqueue::load(reset($referenced_subqueues)))) {
56         $route_parameters = [
57           'entity_queue' => $subqueue->getQueue()->id(),
58           'entity_subqueue' => $subqueue->id(),
59         ];
60         $view->element['#contextual_links']['entityqueue'] = [
61           'route_parameters' => $route_parameters,
62         ];
63       }
64     }
65   }
66 }
67
68 /**
69  * Implements hook_contextual_links_view_alter().
70  *
71  * Change Entityqueue on views into offcanvas links if available.
72  */
73 function entityqueue_contextual_links_view_alter(&$element, $items) {
74   if (\Drupal::moduleHandler()->moduleExists('settings_tray') && isset($element['#links']['entityentity-subqueueedit-form'])) {
75     $element['#links']['entityentity-subqueueedit-form']['attributes'] = [
76       'class' => ['use-ajax'],
77       'data-dialog-type' => 'dialog',
78       'data-dialog-renderer' => 'off_canvas',
79       'data-settings-tray-edit' => TRUE,
80     ];
81   }
82 }
83
84 /**
85  * Implements hook_entity_delete().
86  *
87  * @todo Remove this when https://www.drupal.org/node/2723323 is fixed.
88  */
89 function entityqueue_entity_delete(EntityInterface $entity) {
90   // Get all the entity queues referencing the targets entity type.
91   $queues = EntityQueue::loadMultipleByTargetType($entity->getEntityTypeId());
92   foreach ($queues as $queue) {
93     // Get all the subqueues which are referencing the deleted entity.
94     $query = \Drupal::entityQuery('entity_subqueue')
95       ->condition('queue', $queue->id())
96       ->condition('items', $entity->id());
97     $result = $query->execute();
98     $subqueues = EntitySubqueue::loadMultiple($result);
99
100     // Check if the entity is referenced in a subqueue.
101     foreach ($subqueues as $subqueue) {
102       $items = $subqueue->get('items')->getValue();
103       if (($item_key = array_search($entity->id(), array_column($items, 'target_id'))) !== FALSE) {
104         unset($items[$item_key]);
105         $subqueue->set('items', $items);
106         $subqueue->save();
107       }
108     }
109   }
110 }