Programmatically create a nodequeue with subqueue in Drupal

Last updated on 18/02/2016

Drupal 8

The Nodequeue module is not going to be ported to Drupal 8.

You should use the Entityqueue module.

Create a nodequeue with Nodequeue module.

Drupal 7

if (!nodequeue_load_queue_by_name('homepage_featured')) {
    $nodequeue = new stdClass();
    $nodequeue->api_version = 2;
    $nodequeue->name = 'homepage_featured';
    $nodequeue->title = 'Homepage featured';
    $nodequeue->subqueue_title = '';
    $nodequeue->size = 4;
    $nodequeue->link = 'Add to the Homepage featured queue';
    $nodequeue->link_remove = 'Remove from Homepage featured queue';
    $nodequeue->owner = 'nodequeue';
    $nodequeue->show_in_ui = 1;
    $nodequeue->show_in_tab = 1;
    $nodequeue->show_in_links = 1;
    $nodequeue->reference = '0';
    $nodequeue->reverse = 1;
    $nodequeue->i18n = 0;
    $nodequeue->roles = array(
      0 => '3',
      1 => '5',
      2 => '6',
    );
    $nodequeue->types = array(
      0 => 'article',
      2 => 'review',
    );
    nodequeue_save($nodequeue);
 
    // Add subqueue, using qid.
    $nodequeue->add_subqueue = array(
      $nodequeue->qid => 'Homepage featured',
    );
    nodequeue_save($nodequeue);
  }

And to remove:

  if ($nodequeue = nodequeue_load_queue_by_name('promoted_content_carousel')) {
    nodequeue_delete($nodequeue->qid);
  }

Drupal 6

$qid = db_result(db_query("SELECT qid FROM {nodequeue_queue} WHERE title = 'My nodequeue title'"));
if (!$qid) {
  $queue = (object) array(
    'title' => 'My nodequeue title',
    'size' => '15',
    'types' => array('article'),
    'link'           => '',
    'link_remove'    => '',
    'owner'          => 'nodequeue',
    'show_in_ui'     => 1,
    'show_in_tabs'   => 1,
    'show_in_links'  => 0,
    'reference'      => 0,
    'reverse'        => 0,
  );
  $qid = nodequeue_save($queue);
  $queue = nodequeue_load($qid);
  nodequeue_add_subqueue($queue, 'My nodequeue title');
}