Skip to content

Instantly share code, notes, and snippets.

@bmunslow
Last active July 18, 2024 08:31
Show Gist options
  • Save bmunslow/5d41d00c81313d485a148e960d7615fa to your computer and use it in GitHub Desktop.
Save bmunslow/5d41d00c81313d485a148e960d7615fa to your computer and use it in GitHub Desktop.
Drupal 7 - Programatically add items to EntityQueue
<?php
/**
* This snippet adds all nodes belonging to a specific node type to all
* entityqueues found in the system, defined by Entityqueue module
* Customize to your own needs by modifying relevant parts of the code.
* @see https://www.drupal.org/project/entityqueue
*/
// Get all available entityqueue queues
$all_queue_names = variable_get('entityqueue_queue_names', array());
foreach ($all_queue_names as $queue_name) {
$wrapper = NULL;
// Load subqueue
$subqueue = entityqueue_subqueue_load($queue_name);
// Load corresponding queue
$queue = entityqueue_queue_load($subqueue->queue);
// Get name of field which stores reference entities
$field_name = _entityqueue_get_target_field_name($queue->target_type);
try {
// Let's wrap it into EMW
$wrapper = entity_metadata_wrapper('entityqueue_subqueue', $subqueue);
}
catch (EntityMetadataWrapperException $exc) {
watchdog('default', 'Entityqueue error, not able to load: ' . $queue_name . ': ' . __FUNCTION__ . '() ' . $exc->getTraceAsString(), NULL, WATCHDOG_ERROR);
}
if (isset($wrapper)) {
// Ready to add entities
// Get desired entities from database via EFQ
$query = new EntityFieldQuery();
// Select specific bundles to add to each list
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', array('article', 'page'), 'IN')
->propertyCondition('status', NODE_PUBLISHED);
$result = $query->execute();
// Update queue
if (isset($result['node'])) {
foreach ($result['node'] as $nid => $node) {
// Add entities into queue
$wrapper->{$field_name}[] = $nid;
}
// Save entityqueue
$wrapper->save();
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment