diff --git a/contrib/search_api_views/includes/handler_filter_user.inc b/contrib/search_api_views/includes/handler_filter_user.inc index 563c3eb..0cfba71 100644 --- a/contrib/search_api_views/includes/handler_filter_user.inc +++ b/contrib/search_api_views/includes/handler_filter_user.inc @@ -53,34 +53,52 @@ class SearchApiViewsHandlerFilterUser extends SearchApiViewsHandlerFilter { $path = $this->isMultiValued() ? 'admin/views/ajax/autocomplete/user' : 'user/autocomplete'; $form['value']['#autocomplete_path'] = $path; - // Set the correct default value in case the admin-set value is used. - if ($this->value && empty($form_state['input'])) { - $values = array(); - $args[':uids'] = array_filter($this->value); - $result = db_query("SELECT uid, name FROM {users} u WHERE uid IN (:uids)", $args); - $result = $result->fetchAllKeyed(); - foreach ($this->value as $uid) { - if (!$uid) { - $values[] = 'Anonymous'; - } - elseif (isset($result[$uid])) { - $values[] = $result[$uid]; - } + // Set the correct default value in case the admin-set value is used (and a + // value is present). The value is used if the form is either not exposed, + // or the exposed form wasn't submitted yet (there is + if ($this->value && (empty($form_state['input']) || !empty($form_state['input']['live_preview']))) { + $form['value']['#default_value'] = $this->uids_to_names($this->value); + } + } + + /** + * Transforms an array of UIDs into a comma-separated list of names. + * + * @param array $uids + * The UIDs to transform. + * + * @return string + * A string containing the names corresponding to the UIDs, separated by + * commas. + */ + protected function uids_to_names(array $uids) { + $names = array(); + $args[':uids'] = array_filter($uids); + $result = db_query("SELECT uid, name FROM {users} u WHERE uid IN (:uids)", $args); + $result = $result->fetchAllKeyed(); + foreach ($uids as $uid) { + if (!$uid) { + $names[] = 'Anonymous'; + } + elseif (isset($result[$uid])) { + $names[] = $result[$uid]; } - $form['value']['#default_value'] = implode(', ', $values); } + return implode(', ', $names); } /** * {@inheritdoc} */ public function value_validate($form, &$form_state) { - $value = &$form_state['values']['options']['value']; - $values = $this->isMultiValued() ? drupal_explode_tags($value) : array($value); - $uids = $this->validate_user_strings($form['value'], $values); + if (!empty($form['value'])) { + $value = &$form_state['values']['options']['value']; + $values = $this->isMultiValued($form_state['values']['options']) ? drupal_explode_tags($value) : array($value); + $uids = $this->validate_user_strings($form['value'], $values); - if ($uids) { - $value = $uids; + if ($uids) { + $value = $uids; + } } } @@ -175,11 +193,16 @@ class SearchApiViewsHandlerFilterUser extends SearchApiViewsHandlerFilter { * This is either the case if the form isn't exposed, or if the " Allow * multiple selections" option is enabled. * + * @param array $options + * (optional) The options array to use. If not supplied, the options set on + * this filter will be used. + * * @return bool * TRUE if multiple values can be entered for this filter, FALSE otherwise. */ - protected function isMultiValued() { - return empty($this->options['exposed']) || !empty($this->options['expose']['multiple']); + protected function isMultiValued(array $options = array()) { + $options = $options ? $options : $this->options; + return empty($options['exposed']) || !empty($options['expose']['multiple']); } /** @@ -187,7 +210,7 @@ class SearchApiViewsHandlerFilterUser extends SearchApiViewsHandlerFilter { */ public function admin_summary() { $value = $this->value; - $this->value = implode(', ', $value); + $this->value = empty($value) ? '' : $this->uids_to_names($value); $ret = parent::admin_summary(); $this->value = $value; return $ret;