0

I am trying to implement a feature of a fancy drop-down select box. I am going to use the Chosen module to get the functionality I want for the select box. I have a view that lists the data that I want to populate the select box. All I need to do is render a plain old HTML select box with the fields and rows from this view.

How can I do this? Is there a style plugin or anything out there? I personally haven't found one after searching.

  • Which Drupal version ? – yvan Aug 25 '16 at 9:36
  • Drupal 7 please : ) – user1359 Aug 25 '16 at 12:04
  • And you want to show this where? In a block? As a views header? In a form built using form API? Have you considered creating a Views jump menu? If not it should be fairly straightforward to create a custom module that includes such a select box in a form, using views_get_views_result and the form API. Chosen actually adds support for itself to Form API. – argiepiano Aug 25 '16 at 13:01
0

Going off of argiepiano's excellent suggestions, this is what I came up with: a module that provides a block with the select box populated by the view.

/**
 * Implements hook_block_info
 */
function my_module_block_info() {

  $blocks['my_module'] = array(
    'info' => t('Puts the values of My Taxonomy Term view into a select box')
  );

  return $blocks;
}

/**
 * Implements hook_block_view
 */
function my_module_block_view( $delta ) {

  $view_name = 'my_taxonomy_term_view';
  $display_name = '';

  $content = '<select>';

  $results = views_get_view_result($view_name, $display_name);

  foreach ( $results as $result ) {
    $content .= '<option value=\'' . $result->tid . '\'>' 
      . $result->taxonomy_term_data_name . '</option>';
  }

  $content .= '</select>';

  $block['content'] = $content;

  return $block;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.