3

I tried to convert a text field filter for title into a select list filtered by content type using hook form alter:

function myhook_form_alter(&$form, &$form_state, $form_id) {

  if($form_id == 'views_exposed_form'){

      // load nodes filtered by content type - news
      $nodes = node_load_multiple(array(), array('type' => 'news'));

      // Build an array    
      $dropdown_array = array('' => '--Any--');
      foreach ($nodes as $node) {
         $key = $node->title;
         $value = $node->title;

         $dropdown_array[$key] = $value;        
      }

      // Alter form
      $form['title'] = array(     
         '#type' => 'select',
         '#default_value' => '1',        
         '#options' => $dropdown_array,
      ); 

   }//endif

}

The code above works well. Now I wonder is there a better, shorter, more efficient or proper API to implement ? I'm a newbie in drupal programming. Learning to code in a more drupal way. Please advise. Thanks.

  • It's not going to work very well once you have a lot of news items, is it? How does this filter work with the view? Are you just showing the one item selected by title? If so, that doesn't seem very useful. – Alfred Armstrong Nov 5 '13 at 12:33
  • As @AlfredArmstrong just mentioned, I'm not sure if filtering by title is the best option, because if you have a lot of nodes your select list will end up being massive. Filters are normally used for data associated to content, like taxonomy terms, author, etc. If you expose a field as a filter in Views, the field will come up as a select list already. – Teknotica Nov 5 '13 at 12:44
  • Em.. right, it doesn't make much sense to filter by title in this way. Just so happened that the client asking for such feature. I'm exploring the possibility of coding in this way actually. many thanks! – Adamtan Nov 5 '13 at 15:22
  • How about Auto-complete? – Beebee Nov 18 '13 at 12:01
5

If you know that this list won't grow more than few dozens of nodes (you have to make some tests with more items) I'd just avoid loading complete nodes - it's unnecessary if you need only the title.
This call might become a significant database hit (=> page will load slower and will slow down other visitors of the site) when you add more nodes to the site.
When you call node_load(), Drupal builds the complete node object, meaning it looks for all additional fields you added, settings and calls corresponding node hooks from other modules. Avoid calling it for small details whenever you can.

Here's what you could use instead:

function myhook_form_alter(&$form, &$form_state, $form_id) {

  if($form_id == 'views_exposed_form'){

      $nodes = db_select('node', 'n')
        ->fields('n', array('title'))
        ->condition('type', 'news')
        ->orderBy('n.title', 'ASC')
        ->execute();
      $dropdown_array = array('' => t('-- Any --'));
      foreach ($nodes as $node) {
        $dropdown_array[$node->title] = $node->title;
      }

      // Alter form
      $form['title'] = array(     
         '#type' => 'select',
         '#default_value' => '1',        
         '#options' => $dropdown_array,
      ); 

   }//endif

}

Check out also Chosen module for Drupal - it will allow you to have a much nicer select form element than the default one. It's pretty straightforward to install.
You can see a demo here.

  • Great thanks! This is what I wanted to learn and pick up. – Adamtan Nov 5 '13 at 15:24

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.