1 | <?php |
---|
2 | /** |
---|
3 | * @file |
---|
4 | * Registers a views filter. |
---|
5 | * |
---|
6 | * The view hooks allow for registration of a new views filter that allows |
---|
7 | * the user to choose the fields to be excluded from a displayed instance of a view. |
---|
8 | */ |
---|
9 | |
---|
10 | /** |
---|
11 | * Implementation of hook_views_handlers(). |
---|
12 | */ |
---|
13 | function views_dynamic_fields_views_handlers() { |
---|
14 | return array( |
---|
15 | 'info' => array( |
---|
16 | 'path' => drupal_get_path('module', 'views_dynamic_fields') . '/handlers', |
---|
17 | ), |
---|
18 | 'handlers' => array( |
---|
19 | 'views_handler_filter_dynamic_fields' => array( |
---|
20 | 'parent' => 'views_handler_filter', |
---|
21 | ), |
---|
22 | ), |
---|
23 | ); |
---|
24 | } |
---|
25 | |
---|
26 | /** |
---|
27 | * Implementation of hook_views_data_alter() |
---|
28 | */ |
---|
29 | function views_dynamic_fields_views_data_alter(&$data) { |
---|
30 | // Register a dummy field to attach the dynamic_fields filter |
---|
31 | $data['node']['dyfield'] = array( |
---|
32 | 'title' => t('Dynamic Fields'), |
---|
33 | 'help' => t("List of fields displayed in a view"), |
---|
34 | 'filter' => array( |
---|
35 | 'handler' => 'views_handler_filter_dynamic_fields', |
---|
36 | ), |
---|
37 | ); |
---|
38 | } |
---|
39 | |
---|
40 | /** |
---|
41 | * Implementation of hook_theme() |
---|
42 | */ |
---|
43 | function views_dynamic_fields_theme() { |
---|
44 | $theme = array( |
---|
45 | 'views_dynamic_fields_sort_filter_fields' => array( |
---|
46 | 'arguments' => array('form' => NULL), |
---|
47 | ), |
---|
48 | ); |
---|
49 | return $theme; |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * Theme a drag-sortable table of fields in the exposed filter |
---|
54 | * @param unknown_type $form |
---|
55 | */ |
---|
56 | function theme_views_dynamic_fields_sort_filter_fields($form) { |
---|
57 | drupal_add_tabledrag('views-dynamic-fields-filters-table-sort', 'order', 'self', 'sort'); |
---|
58 | $header = array('', 'Select', 'Field', 'Weight'); |
---|
59 | foreach (element_children($form) as $key) { |
---|
60 | $row = array(''); // This is important. We need to start with an empty element for the drag handle. |
---|
61 | // Add class to group weight fields for drag and drop. |
---|
62 | $form[$key]['sort']['#attributes']['class'] = 'sort'; |
---|
63 | $row[] = drupal_render($form[$key]['check']); |
---|
64 | $row[] = drupal_render($form[$key]['title']); |
---|
65 | $row[] = drupal_render($form[$key]['sort']); |
---|
66 | $rows[] = array('data' => $row, 'class' => 'draggable'); // note the difference between $row and $rows |
---|
67 | } |
---|
68 | $output = theme('table', $header, $rows, array('id' => 'views-dynamic-fields-filters-table-sort')); |
---|
69 | $output .= drupal_render($form); |
---|
70 | return $output; |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | * Comparison function based on weights on fields in the exposed filter |
---|
75 | * @param unknown_type $a |
---|
76 | * @param unknown_type $b |
---|
77 | */ |
---|
78 | function _views_handler_filter_sort_form_field_weights($a, $b) { |
---|
79 | if ($a['sort']['#default_value'] == $b['sort']['#default_value']) { |
---|
80 | return 0; |
---|
81 | } |
---|
82 | return ($a['sort']['#default_value'] < $b['sort']['#default_value']) ? -1 : 1; |
---|
83 | } |
---|