Posted By

berkes on 05/19/07


Tagged

php drupal formalter views


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

berkes
vali29


Form alter to force exposed views filters to fllow filter options


 / Published in: PHP
 

callback in a form alter to enforce an exposed filter to use only the options set in filter. Works for selectlists only.

  1. /**
  2.  * Form alter hook Callbacks and helpers
  3.  */
  4. function _mymodule_formalter_exported_filter(&$form) {
  5. $filter_id = 'workflow_node.sid'; //set this to the id of the filter. use print_r($form['view'][#value']->filter) to find the ID
  6.  
  7. foreach($form['view']['#value']->filter as $filter) {
  8. if ($filter['field'] == $filter_id) {
  9. $only_these = $filter['value'];
  10. break;
  11. }
  12. }
  13.  
  14. foreach ($form['filter0']['#options'] as $key => $option) {
  15. if (in_array($key, $only_these)) {
  16. $options[$key] = $option;
  17. }
  18. }
  19. $form['filter0']['#options'] = $options;
  20.  
  21. if(is_array($form['filter0']['#default_value'])) {
  22. $form['filter0']['#default_value'] = 40; //or any other value.
  23. }
  24. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: thededalus on October 7, 2007

where does this snippet go?

Posted By: dduane on July 15, 2009

This is the way I ended up solving this problem:

I put this snippet into my hookformalter() function

  $form['field_field_name_value'] = array(
     '#type' => 'select',
        '#default_value' => 'All',
        '#options' => field_name_option_values(),
  );

// Then here is the function that returns the options that came out of the drupal table

function fieldnameoptionvalues() { // contentfields function retrieves the possible values out of the drupal database table $field = contentfields('fieldfieldname',NULL); $options = explode(chr(10), $field['allowedvalues']);

 $new_options['All']='';
 // this cleans it up so that the field names are indexes to the array.  In other cases you may want to put number codes here.  
 foreach ($options AS $option)
 {  $new_options[trim($option)]=trim($option); }

 return $new_options;

}

/////////////////Here is another possible example of this function if you have key/ value pairs specified in your CCK type delinted with pipes.

function fieldname2optionvalues() {

$field = content_fields('field_field_name',NULL);
 $options = explode(chr(10), $field['allowed_values']); 

 $new_options['All']='';

 foreach ($options AS $option) {
    $op = explode('|',$option);
    $key = trim($op[0]);
    if($key != '') 
      $new_options[$key] = $op[1];
 }

 return $new_options;

}

You need to login to post a comment.