commit 2bafd2f18b183d08276e6e1e25e0998dc3a83b7a Author: Matthew Radcliffe Date: Sat Sep 14 15:27:14 2013 -0400 Issue #2046147 by mradcliffe: Refactor exposed form to work with input required and basic exposed form plugins. diff --git a/views/cck_select_other_handler_filter.inc b/views/cck_select_other_handler_filter.inc index fab3fa8..5854820 100644 --- a/views/cck_select_other_handler_filter.inc +++ b/views/cck_select_other_handler_filter.inc @@ -26,14 +26,28 @@ class cck_select_other_handler_filter extends views_handler_filter_in_operator { $identifier = $this->options['expose']['identifier']; - // Populate other default value if any. - $otherdef = ''; - $other_values = array_diff($this->value, $this->value_options); - if (!empty($other_values)) { - $otherdef = array_pop($other_values); - $form[$identifier]['select_other_list']['#default_value'][] = 'other'; + if (!is_array($form_state['input'][$identifier])) { + $form_state['input'][$identifier] = array( + 'select_other_list' => $this->value, + 'select_other_text_input' => '', + ); } + $settings = array( + $identifier => array( + 'list_element' => str_replace('_', '-', 'edit-' . $identifier . '-select-other-list'), + 'input_element' => str_replace('_', '-', 'edit-' . $identifier . '-select-other-text-input'), + ), + ); + $form[$identifier]['#parents'] = array($identifier); + $form[$identifier]['#attached']['js'] = array( + array( + 'data' => array('CCKSelectOther' => $settings), + 'type' => 'setting', + ), + drupal_get_path('module', 'cck_select_other') . '/cck_select_other.js', + ); + $form[$identifier]['select_other_list']['#parents'] = array($identifier, 'select_other_list'); $form[$identifier]['select_other_text_input'] = array( '#type' => 'textfield', '#title' => t('Other'), @@ -41,52 +55,40 @@ class cck_select_other_handler_filter extends views_handler_filter_in_operator { '#attributes' => array( 'class' => array('form-text select_other_text_input'), ), - '#default_value' => $otherdef, - '#parents' => array('options', 'value', 'select_other_text_input'), + '#parents' => array($identifier, 'select_other_text_input'), '#size' => 10, ); - $form[$identifier]['#parents'] = array('options', $identifier); - $form[$identifier]['select_other_list']['#type'] = 'select'; $form[$identifier]['select_other_list']['#multiple'] = $this->options['expose']['multiple']; - $form[$identifier]['select_other_list']['#parents'] = array('options', $identifier, 'select_other_list'); - $form[$identifier]['select_other_text_input']['#parents'] = array('options', $identifier, 'select_other_text_input'); if (empty($form_state['exposed']) || empty($this->options['expose']['required'])) { // If we're configuring an exposed filter, add an option. $any_label = variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? '' : t('- Any -'); $form[$identifier]['select_other_list']['#options'] = array('All' => $any_label) + $form[$identifier]['select_other_list']['#options']; } - - $settings = array( - 'list_element' => str_replace('_', '-', 'edit-options-' . $identifier . '-select-other-list'), - 'input_element' => str_replace('_', '-', 'edit-options-' . $identifier . '-select-other-text-input'), - ); - - drupal_add_js(array('CCKSelectOther' => array($settings)), array('type' => 'setting')); - drupal_add_js(drupal_get_path('module', 'cck_select_other') . '/cck_select_other.js'); } function exposed_submit(&$form, &$form_state) { $identifier = $this->options['expose']['identifier']; - $values = $form_state['values']['options'][$identifier]; + $values = $form_state['values'][$identifier]; $form_state['values'][$identifier] = array(); + $text_input = $values['select_other_text_input']; if (is_array($values['select_other_list'])) { - // Multiple values will be stored in an array. - foreach ($values['select_other_list'] as $key => $value) { - if ($value && $key == $value) { - if ($value <> 'other') { - $form_state['values'][$identifier][] = $value; - } - else { - // Set the other value instead of 'other' - $form_state['values'][$identifier][] = $values['select_other_text_input']; - } + // Reduce the values into a simple array. + $form_state['values'][$identifier] = array_reduce($values['select_other_list'], function(&$result, $item) use ($text_input) { + if ($item <> 'other') { + $result[] = $item; } - } + else { + $result[] = $text_input; + } + + return $result; + }); } else { + // Reduce into a single value. if ($values['select_other_list'] == 'other') { $form_state['values'][$identifier] = $values['select_other_text_input']; } @@ -94,15 +96,12 @@ class cck_select_other_handler_filter extends views_handler_filter_in_operator { $form_state['values'][$identifier] = $values['select_other_list']; } } - } /** * Value form */ function value_form(&$form, &$form_state) { - static $js; - parent::value_form($form, $form_state); $default = array(); @@ -118,35 +117,38 @@ class cck_select_other_handler_filter extends views_handler_filter_in_operator { } } + $settings = array( + 'list_element' => 'edit-value-select-other-list', + 'input_element' => 'edit-value-select-other-text-input', + ); + $options = $form['value']['#options']; $form['value'] = array( '#type' => 'container', '#tree' => TRUE, - '#parents' => array('options', 'value'), + '#parents' => array('value'), + '#attached' => array( + 'js' => array( + array( + 'data' => array('CCKSelectOther' => $settings), + 'type' => 'setting', + ), + drupal_get_path('module', 'cck_select_other') . '/cck_select_other.js', + ), + ), 'select_other_list' => array( '#type' => 'select', '#options' => $options, '#multiple' => TRUE, '#default_value' => $default, - '#parents' => array('options', 'value', 'select_other_list'), + '#parents' => array('value', 'select_other_list'), ), ); - - if (!$js) { - drupal_add_js(drupal_get_path('module', 'cck_select_other') . '/cck_select_other.js'); - $js = TRUE; - } - $settings = array( - 'list_element' => 'edit-options-value-select-other-list', - 'input_element' => 'edit-options-value-select-other-text-input', - ); - - drupal_add_js(array('CCKSelectOther' => array($settings)), array('type' => 'setting')); } function value_submit($form, &$form_state) { // Capture the values into a separate array and reset the value array. - $values = $form_state['values']['options']['value']; + $values = $form_state['values']['value']; $form_state['values']['options']['value'] = array(); foreach ($values['select_other_list'] as $key => $value) { @@ -163,7 +165,7 @@ class cck_select_other_handler_filter extends views_handler_filter_in_operator { unset($this->value_options['']); unset($this->value_options['_none']); } - + function accept_exposed_input($input) { $ret = parent::accept_exposed_input($input); return $ret; @@ -212,7 +214,7 @@ class cck_select_other_handler_filter extends views_handler_filter_in_operator { return; } - if (in_array('other', $this->value)) { + if (!$this->is_exposed() && in_array('other', $this->value)) { // We need to reverse the operator and find the inverse. $this->ensure_my_table(); @@ -223,6 +225,14 @@ class cck_select_other_handler_filter extends views_handler_filter_in_operator { $operator = ($this->operator == 'in') ? 'not in' : 'in'; $this->query->add_where($this->options['group'], "$this->table_alias.$this->real_field", array_values($values), $operator); } + elseif ($this->is_exposed() && empty($this->value[0][0])) { + // Need to do something a bit different for exposed forms. + $this->ensure_my_table(); + + $value_options = cck_select_other_options($this->instance); + $operator = ($this->operator == 'in') ? 'not in' : 'in'; + $this->query->add_Where($this->options['group'], "$this->table_alias.$this->real_field", array_values($value_options), $operator); + } else { // Query as normal. parent::op_simple();