diff --git a/contrib/search_api_views/includes/handler_field.inc b/contrib/search_api_views/includes/handler_field.inc index 9627ed2..620583d 100644 --- a/contrib/search_api_views/includes/handler_field.inc +++ b/contrib/search_api_views/includes/handler_field.inc @@ -62,6 +62,9 @@ class SearchApiViewsHandlerField extends views_handler_field { 'collapse' => t('Concatenate values using the list seperator'), 'first' => t('Show first (if present)'), 'count' => t('Show item count'), + 'sum' => t('Show the sum of the values'), + 'min' => t('Show the minimum of the values'), + 'max' => t('Show the maximum of the values'), ), '#default_value' => $this->options['list']['mode'], ); @@ -70,6 +73,7 @@ class SearchApiViewsHandlerField extends views_handler_field { '#title' => t('List seperator'), '#default_value' => $this->options['list']['glue'], ); + $form['list']['glue']['#states']['visible'][':input[name="options[list][mode]"]']['value'] = 'collapse'; } $form['link_to_entity'] = array( '#type' => 'checkbox', @@ -138,27 +142,61 @@ class SearchApiViewsHandlerField extends views_handler_field { * Render a list of values. */ protected function renderArray($value, $values) { - if (isset($this->options['list']['mode'])) { - if ($this->options['list']['mode'] == 'first') { - $value = count($value) ? array_shift($value) : NULL; - if (is_array($value)) { - return $this->renderLink($value, $values); - } - elseif (isset($value)) { - return $this->renderLink($value, $values); - } - return NULL; + $mode = $this->options['list']['mode']; + if ($mode == 'first') { + while (is_array($value)) { + $value = reset($value); } - if ($this->options['list']['mode'] == 'count') { - return count($value); + return isset($value) ? $this->renderLink($value, $values) : NULL; + } + if ($mode == 'collapse') { + $vs = array(); + foreach ($value as $v) { + $vs[] = is_array($v) ? $this->renderArray($v, $values) : $this->renderLink($v, $values); } + return implode($this->options['list']['glue'], $vs); } - $vs = array(); + return $this->reduceArray($value, $mode); + } + + /** + * Helper method for only reducing an array, without linking. + * + * Used for the "count", "sum", "min" and "max" list handling options. + */ + protected function reduceArray($value, $mode) { + // "min" and "max" return NULL for empty arrays, the others return 0. + $ret = $mode[0] == 'm' ? NULL : 0; foreach ($value as $v) { - $vs[] = is_array($v) ? $this->renderArray($v, $values) : $this->renderLink($v, $values); + if (is_array($v)) { + if (!$v) { + continue; + } + $v = $this->reduceArray($v, $mode); + } + if (!isset($v)) { + continue; + } + switch ($mode) { + case 'count': + ++$ret; + break; + case 'sum': + $ret += $v; + break; + case 'min': + if (!isset($ret) || $v < $ret) { + $ret = $v; + } + break; + case 'max': + if (!isset($ret) || $v > $ret) { + $ret = $v; + } + break; + } } - $glue = isset($this->options['list']['glue']) ? $this->options['list']['glue'] : ', '; - return implode($glue, $vs); + return $ret; } /** diff --git a/contrib/search_api_views/includes/handler_field_entity.inc b/contrib/search_api_views/includes/handler_field_entity.inc index 6cc11f4..439929b 100644 --- a/contrib/search_api_views/includes/handler_field_entity.inc +++ b/contrib/search_api_views/includes/handler_field_entity.inc @@ -70,7 +70,7 @@ class SearchApiViewsHandlerFieldEntity extends SearchApiViewsHandlerField { * Helper function for rendering a single value. */ protected function renderValue($value) { - if ($this->options['format_name']) { + if (is_numeric($value) && $this->options['format_name']) { $value = (int) $value; $entity = entity_load($this->entity_type, array($value)); if (isset($entity[$value])) {