Form alter to force exposed views filters to fllow filter options


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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

You need to login to post a comment.