Updated to Drupal 8.5. Core Media not yet in use.
[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   // Proceed only if there is entityqueue sort criteria available.
43   if (!$sort_key = entityqueue_get_entityqueue_sort($view)) {
44     return;
45   }
46
47   // Allow to disable the contextual links.
48   if (!$view->display_handler->getOption('show_admin_links')) {
49     return;
50   }
51
52   // Get view display relationships.
53   $relationships = $view->relationship;
54   foreach ($relationships as $relationship) {
55     if ($relationship->field == 'entityqueue_relationship') {
56       $referenced_subqueues = (array) $relationship->options['limit_queue'];
57
58       // Contextual links can handle only one set of links coming from a module,
59       // so we'll have to settle for the first referenced queue.
60       if (!empty($referenced_subqueues) && ($subqueue = EntitySubqueue::load(reset($referenced_subqueues)))) {
61         $route_parameters = [
62           'entity_queue' => $subqueue->getQueue()->id(),
63           'entity_subqueue' => $subqueue->id(),
64         ];
65         $view->element['#contextual_links']['entityqueue'] = [
66           'route_parameters' => $route_parameters,
67         ];
68       }
69     }
70   }
71 }
72
73 /**
74  * Implements hook_contextual_links_view_alter().
75  *
76  * Change Entityqueue on views into offcanvas links if available.
77  */
78 function entityqueue_contextual_links_view_alter(&$element, $items) {
79   if (\Drupal::moduleHandler()->moduleExists('outside_in') && isset($element['#links']['entityentity-subqueueedit-form'])) {
80     $element['#links']['entityentity-subqueueedit-form']['attributes'] = [
81       'class' => ['use-ajax'],
82       'data-dialog-type' => 'dialog',
83       'data-dialog-renderer' => 'offcanvas',
84       'data-outside-in-edit' => TRUE,
85     ];
86
87     $element['#attached']['library'][] = 'outside_in/drupal.off_canvas';
88   }
89 }
90
91 /**
92  * Get the entityqueue position sort of a view if there is one and return its
93  * ID. If there are multiple of these sorts the first is returned.
94  *
95  * @param $view
96  *   The view object.
97  *
98  * @return
99  *   The ID of the sort or FALSE if there isn't one.
100  */
101 function entityqueue_get_entityqueue_sort($view) {
102   foreach ($view->sort as $id => $sort) {
103     if ($sort->definition['id'] == 'entity_queue_position') {
104       return $id;
105     }
106   }
107   return FALSE;
108 }
109
110 /**
111  * Implements hook_entity_delete().
112  *
113  * @todo Remove this when https://www.drupal.org/node/2723323 is fixed.
114  */
115 function entityqueue_entity_delete(EntityInterface $entity) {
116   // Get all the entity queues referencing the targets entity type.
117   $queues = EntityQueue::loadMultipleByTargetType($entity->getEntityTypeId());
118   foreach ($queues as $queue) {
119     $entity_settings = $queue->getEntitySettings();
120
121     // Check if the queue's bundle also matches that of the target entity.
122     if (is_array($entity_settings['handler_settings']['target_bundles']) && in_array($entity->bundle(), $entity_settings['handler_settings']['target_bundles'])) {
123       // Get subqueues.
124       $query = \Drupal::entityQuery('entity_subqueue')->condition('queue', $queue->id());
125       $result = $query->execute();
126       $subqueues = EntitySubqueue::loadMultiple($result);
127
128       // Check if the entity is referenced in a subqueue.
129       foreach ($subqueues as $subqueue) {
130         $items = $subqueue->get('items')->getValue();
131         if (($item_key = array_search($entity->id(), array_column($items, 'target_id'))) !== FALSE) {
132           unset($items[$item_key]);
133           $subqueue->set('items', $items);
134           $subqueue->save();
135         }
136       }
137     }
138   }
139 }