6 * Defines a content_multigroup filter object in views.
8 * This filter allows you to associate the deltas in a multigroup so that
9 * views doesn't do a cross-join across all values in multivalue fields.
11 class content_multigroup_handler_filter
extends views_handler_filter
{
12 var
$content_multigroup_fields;
14 function can_expose() {
18 function admin_summary() {
23 * Get information about the multigroup.
25 function _get_multigroup() {
26 $groups = fieldgroup_groups($this->definition
['content_type_name']);
27 return $groups[$this->definition
['content_group_name']];
31 * Get information about the fields in this multigroup.
33 function _get_multigroup_fields() {
34 if (!isset($this->content_multigroup_fields
)) {
35 $group = $this->_get_multigroup();
36 $this->content_multigroup_fields
= array();
37 foreach (array_keys($group['fields']) as
$field_name) {
38 $field = content_fields($field_name, $this->definition
['content_type_name']);
39 $table_alias = content_views_tablename($field);
40 $this->content_multigroup_fields
[$table_alias] = $field;
43 return $this->content_multigroup_fields
;
47 * Define default value for master field.
49 function options(&$options) {
50 $multigroup_fields = $this->_get_multigroup_fields();
51 // Find the first required field.
52 foreach ($multigroup_fields as
$table_alias => $field) {
53 if ($field['required']) {
54 $options['content_multigroup_master_field'] = $table_alias;
58 // Default to first field in the multigroup.
59 if (empty($options['content_multigroup_master_field'])) {
60 $options['content_multigroup_master_field'] = current(array_keys($multigroup_fields));
65 * Options from to ask the user for a master field.
67 function options_form(&$form, &$form_state) {
68 $group = $this->_get_multigroup();
70 foreach ($this->_get_multigroup_fields() as
$table_alias => $field) {
71 $options[$table_alias] = t($field['widget']['label']);
73 $form['content_multigroup_master_field'] = array(
74 '#title' => t('Available fields in @group_label multigroup', array('@group_label' => t($group['label']))),
76 '#options' => $options,
77 '#default_value' => $this->options
['content_multigroup_master_field'],
78 '#description' => t('Select the field in this multigroup that will be used to build the primary join with the content table.'),
83 * Add joins to the query to synchronize the fields in this multigroup.
86 // Create the join between the master field table and the node table.
87 $base_alias = $this->query
->ensure_table($this->options
['content_multigroup_master_field'], $this->relationship
);
89 // Now we want to join the master field table with all other tables
90 // related to fields in the same multigroup, but adding the delta
91 // key to the join condition. This is what allows us to keep delta
92 // values in sync for all fields in the same multigroup.
93 foreach ($this->_get_multigroup_fields() as
$table_alias => $field) {
94 if ($table_alias != $this->options
['content_multigroup_master_field']) {
95 $alias = $this->query
->ensure_table($table_alias, $this->relationship
);
96 $this->query
->table_queue
[$table_alias]['join']->extra
= $base_alias .
'.delta = '.
$alias .
'.delta';