Blog / Create a select box from a nodequeue

We had the need to make a select box from a nodequeue in Drupal 6. Don't ask me why, as selects aren't the best UI, but that's a different discussion. There's probably a way easier way to do this, and niftier with jQuery, etc., but project budgets are what they are.

In our helper module, created this function (change 'mymodule' to whatever you have named your module): 

drupal_add_js(drupal_get_path('module', 'agentic').'/agentic.js');

/**
 * Generates an html select box based on a nodequeue, auto-links to the node path alias onchange
 *
 * @param $nodequeue_name - the name of the nodequeue
 * @param $default - the optional default option for the select box
 *
 * @return html select box based on nid's in nodequeue
 *
 */
function agentic_nodequeue_select_box($nodequeue_name,$default=null) {
  $view = views_get_view($nodequeue_name);
  $view->execute();
  $return = '<form id="form_'.$nodequeue_name.'"><select id="select_'.$nodequeue_name.'" method="post" onchange="goto_URL(this.form.select_'.$nodequeue_name.');">';
  $return .= ($default) ? '<option value="">'.$default.'</option>' : '';
  foreach ($view->result as $v) {
    $node = node_load($v->nid);
    $return .= '<option value="/'.drupal_get_path_alias('node/'.$node->nid).'">'.$node->title.'</option>';
  }
  $return .= '</select></form>';
  return $return;
}

The js file just contains a javascript function that autosubmits the form to the selected index value (I believe I borrowed this function, but can't remember where - props to originator):

function goto_URL(object) {
  window.location.href =
  object.options[object.selectedIndex].value;
}
 

Then in a php block, you can place the following: 

<?php
print mymodule_nodequeue_select_box('nodequeue_1','My Default Value');
?>

Hope that helps someone . . .

D

 

Leave a comment

(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.

Sign up for updates