-
Notifications
You must be signed in to change notification settings - Fork 2
/
entityqueue.module
136 lines (122 loc) · 4.51 KB
/
entityqueue.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?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;
}
// Allow to disable the contextual links.
if (!$view->display_handler->getOption('show_admin_links')) {
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('settings_tray') && isset($element['#links']['entityentity-subqueueedit-form'])) {
$element['#links']['entityentity-subqueueedit-form']['attributes'] = [
'class' => ['use-ajax'],
'data-dialog-type' => 'dialog',
'data-dialog-renderer' => 'off_canvas',
'data-settings-tray-edit' => TRUE,
];
$element['#attached']['library'][] = 'settings_tray/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) {
// Get all the subqueues which are referencing the deleted entity.
$query = \Drupal::entityQuery('entity_subqueue')
->condition('queue', $queue->id())
->condition('items', $entity->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();
}
}
}
}