Hook form alter for select list 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(&amp;$form, &amp;$form_state, $form_id) {

  if($form_id == 'views_exposed_form'){

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

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

         $dropdown_array[$key] = $value;        
      }

      // Alter form
      $form['title'] = array(     
         '#type' =&gt; 'select',
         '#default_value' =&gt; '1',        
         '#options' =&gt; $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.
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(&amp;$form, &amp;$form_state, $form_id) {

  if($form_id == 'views_exposed_form'){

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

      // Alter form
      $form['title'] = array(     
         '#type' =&gt; 'select',
         '#default_value' =&gt; '1',        
         '#options' =&gt; $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.