views_dependent_filters.module 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * @file views_dependent_filters.module
  4. * Provides a Views exposed filter which makes other filters depend on values
  5. * in yet further filters for their visiblity and processing.
  6. * For example: if the 'node type' filter is set to 'article', show a filter for
  7. * a field that is only present on articles.
  8. */
  9. /**
  10. * Implements hook_views_api().
  11. */
  12. function views_dependent_filters_views_api() {
  13. return array(
  14. 'api' => '3.0-alpha1',
  15. 'path' => drupal_get_path('module', 'views_dependent_filters'),
  16. );
  17. }
  18. /**
  19. * After build form processor for the views exposed form.
  20. *
  21. * This is added by the exposed filter handler so that we can add a CTools
  22. * visiblity dependency.
  23. * We don't use Drupal core #states because as far as I can tell they don't
  24. * work here.
  25. * See also merlinofchaos's comment here: http://drupal.org/node/1406470
  26. */
  27. function views_dependent_filters_exposed_form_after_build($form, $form_state) {
  28. // We may have multiple dependency info arrays from more than one copies
  29. // of the views_dependent_filters_handler_filter_dependent handler.
  30. foreach ($form_state['dependent_exposed_filters'] as $dependency_info) {
  31. // Build up the CTools #dependency item to put onto each dependent element.
  32. $form_dependency = array();
  33. foreach ($dependency_info['controllers'] as $filter_id => $controller_values) {
  34. // Regular form.
  35. $form_dependency['edit-' . $filter_id] = $controller_values;
  36. // better_exposed_filters form.
  37. foreach ($controller_values as $value) {
  38. $value = strtr(drupal_strtolower($value), array(' ' => '-', '_' => '-', '[' => '-', ']' => ''));
  39. $key = 'edit-' . $filter_id . '-' . $value;
  40. $form_dependency[$key] = array(TRUE);
  41. }
  42. }
  43. // Set the dependency on each form element as required.
  44. foreach ($dependency_info['dependents'] as $dependent) {
  45. if(isset($form_state['groups'])){
  46. foreach ($form_state['groups'] as $g_key => $g_value) {
  47. if(isset($form['filters'][$g_key][$dependent])){
  48. dsm($form['filters'][$g_key][$dependent], '$form["filters"][$g_key][$dependent]');
  49. $form['filters'][$g_key][$dependent]['#process'][] = 'ctools_dependent_process';
  50. if (!isset($form['filters'][$g_key][$dependent]['#dependency'])) {
  51. $form['filters'][$g_key][$dependent]['#dependency'] = array();
  52. }
  53. $form['filters'][$g_key][$dependent]['#dependency'] += $form_dependency;
  54. break;
  55. }
  56. }
  57. }else{
  58. dsm($form[$dependent], '$form[$dependent]');
  59. $form[$dependent]['#process'][] = 'ctools_dependent_process';
  60. if (!isset($form[$dependent]['#dependency'])) {
  61. $form[$dependent]['#dependency'] = array();
  62. }
  63. $form[$dependent]['#dependency'] += $form_dependency;
  64. }
  65. }
  66. }
  67. return $form;
  68. }