5 * Allows users to collect entities in arbitrarily ordered lists.
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;
18 * Implements hook_entity_field_access().
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());
28 return AccessResult::neutral();
32 * Implements hook_views_pre_render().
34 * Add contexual links to views before rendering.
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()) {
42 // Proceed only if there is entityqueue sort criteria available.
43 if (!$sort_key = entityqueue_get_entityqueue_sort($view)) {
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'];
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)))) {
57 'entity_queue' => $subqueue->getQueue()->id(),
58 'entity_subqueue' => $subqueue->id(),
60 $view->element['#contextual_links']['entityqueue'] = [
61 'route_parameters' => $route_parameters,
69 * Implements hook_contextual_links_view_alter().
71 * Change Entityqueue on views into offcanvas links if available.
73 function entityqueue_contextual_links_view_alter(&$element, $items) {
74 if (\Drupal::moduleHandler()->moduleExists('outside_in') && 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' => 'offcanvas',
79 'data-outside-in-edit' => TRUE,
82 $element['#attached']['library'][] = 'outside_in/drupal.off_canvas';
87 * Get the entityqueue position sort of a view if there is one and return its
88 * ID. If there are multiple of these sorts the first is returned.
94 * The ID of the sort or FALSE if there isn't one.
96 function entityqueue_get_entityqueue_sort($view) {
97 foreach ($view->sort as $id => $sort) {
98 if ($sort->definition['id'] == 'entity_queue_position') {
106 * Implements hook_entity_delete().
108 * @todo Remove this when https://www.drupal.org/node/2723323 is fixed.
110 function entityqueue_entity_delete(EntityInterface $entity) {
111 // Get all the entity queues referencing the targets entity type.
112 $queues = EntityQueue::loadMultipleByTargetType($entity->getEntityTypeId());
113 foreach ($queues as $queue) {
114 $entity_settings = $queue->getEntitySettings();
116 // Check if the queue's bundle also matches that of the target entity.
117 if (is_array($entity_settings['handler_settings']['target_bundles']) && in_array($entity->bundle(), $entity_settings['handler_settings']['target_bundles'])) {
119 $query = \Drupal::entityQuery('entity_subqueue')->condition('queue', $queue->id());
120 $result = $query->execute();
121 $subqueues = EntitySubqueue::loadMultiple($result);
123 // Check if the entity is referenced in a subqueue.
124 foreach ($subqueues as $subqueue) {
125 $items = $subqueue->get('items')->getValue();
126 if (($item_key = array_search($entity->id(), array_column($items, 'target_id'))) !== FALSE) {
127 unset($items[$item_key]);
128 $subqueue->set('items', $items);