Skip to content
Snippets Groups Projects
Commit 05ccfe10 authored by Pierre BONNEFOI's avatar Pierre BONNEFOI
Browse files

Issue #3441023 by tamaspresing, pbonnefoi: Auto add not working when views reference is used

parent 86d0be8a
No related branches found
No related tags found
1 merge request!9Issue #3441023 by tamaspresing, pbonnefoi: Auto add not working when views reference is used
Pipeline #272700 passed
entityqueue
entityqueues
subqueue
subqueues
......@@ -10,11 +10,12 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\entityqueue\Entity\EntityQueue;
use Drupal\entityqueue\Entity\EntitySubqueue;
use Drupal\entityqueue\EntityQueueInterface;
use Drupal\views\Views;
/**
* Implements hook_form_alter().
*
* Adds an additional option allowing entities to be added upon creation.
* Add option allowing entities to be added upon creation.
*/
function auto_entityqueue_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (preg_match('/^entity_queue_(add|edit)_form$/', $form_id)) {
......@@ -59,7 +60,7 @@ function auto_entityqueue_entity_insert(EntityInterface $entity) {
// Get queues for this entity type and bundle combo.
$queues = auto_entityqueue_get_queues_by_type_and_bundle($type, $bundle);
if (count($queues)) {
// Add the entity to each queues returned.
// Add the entity to each queue returned.
foreach ($queues as $queue) {
auto_entityqueue_add_entity_to_queue($entity, $queue);
}
......@@ -84,6 +85,15 @@ function auto_entityqueue_add_entity_to_queue(EntityInterface $entity, EntityQue
$entity_settings = $queue->getEntitySettings();
$queue_settings = $queue->getQueueSettings();
auto_entityqueue_check_if_handler_is_view($entity_settings);
// Check if entity is in view bundles.
if (isset($entity_settings['handler_settings']['target_bundles'])
&& !in_array($entity->bundle(), $entity_settings['handler_settings']['target_bundles'])
) {
return;
}
// Get the subqueues.
$query = \Drupal::entityQuery('entity_subqueue')->accessCheck(TRUE)->condition('queue', $queue->id());
$result = $query->execute();
......@@ -93,8 +103,8 @@ function auto_entityqueue_add_entity_to_queue(EntityInterface $entity, EntityQue
foreach ($subqueues as $subqueue) {
$items = $subqueue->get('items')->getValue();
// Deetermine if we should remove an item from the list to avoid exceeding
// the maximum number of items.
// Determines whether we should remove an item from the list to avoid
// exceeding the maximum limit on the number of items.
$remove_item = !empty($queue_settings['max_size']) && count($items) >= $queue_settings['max_size'];
if (isset($entity_settings['handler_settings']['auto_entityqueue']['insert_front']) && $entity_settings['handler_settings']['auto_entityqueue']['insert_front']) {
......@@ -128,13 +138,17 @@ function auto_entityqueue_get_queues_by_type_and_bundle(string $type, string $bu
$queues = EntityQueue::loadMultipleByTargetType($type);
foreach ($queues as $queue) {
$entity_settings = $queue->getEntitySettings();
auto_entityqueue_check_if_handler_is_view($entity_settings);
// Check bundle, if queue is enabled and if auto add is enabled.
// Check bundle.
// Check if auto add is enabled.
// Check if queue is enabled.
if (isset($entity_settings['handler_settings']['target_bundles'])
&& is_array($entity_settings['handler_settings']['target_bundles'])
&& in_array($bundle, $entity_settings['handler_settings']['target_bundles']) // Check bundle.
&& in_array($bundle, $entity_settings['handler_settings']['target_bundles'])
&& isset($entity_settings['handler_settings']['auto_entityqueue']['auto_add'])
&& $entity_settings['handler_settings']['auto_entityqueue']['auto_add'] // Check if auto add is enabled.
&& $queue->status() // Check if queue is enabled.
&& $entity_settings['handler_settings']['auto_entityqueue']['auto_add']
&& $queue->status()
) {
$target_queues[] = $queue;
}
......@@ -142,3 +156,22 @@ function auto_entityqueue_get_queues_by_type_and_bundle(string $type, string $bu
return $target_queues;
}
/**
* Check if handler is views.
*
* @param array $entity_settings
* The array of entity settings.
*/
function auto_entityqueue_check_if_handler_is_view(array &$entity_settings): void {
if ($entity_settings['handler'] == 'views') {
$view = Views::getView($entity_settings['handler_settings']['view']['view_name']);
if (!empty($view->getDependencies()['config'])) {
foreach ($view->getDependencies()['config'] as $config) {
if (str_starts_with($config, 'node.type')) {
$entity_settings['handler_settings']['target_bundles'][] = str_replace('node.type.', '', $config);
}
}
}
}
}
......@@ -6,6 +6,6 @@
"license": "GPL-2.0-or-later",
"minimum-stability": "dev",
"require": {
"drupal/entityqueue": "^1.0"
"drupal/entityqueue": "^1.8"
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment