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

 

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.