Currently the only available sort methods are:
- facet active
- count
- display value
- display value (natural)
- indexed value

I'd like to order the list using any indexed entity field.

I've wrote a custom module which implements hook_facetapi_sort_info(), it lets me to sort a facet display block by an entity field but it should be better to have a configuration ui to select the fields.

What do you think about?

Comments

cpliakas’s picture

I'm not exactly sure what you mean, but I'd love to see a patch with your code (or even just your code) to get a better handle. Great job using the API to accomplish what you needed to do.

Thanks for the post,
Chris

finex’s picture

The module is quite simple and it works only if with a field called "order" is present. Moreover the performance are not good because of two "node_load" calls, but the code can be improved in the future. I needed to implement the feature in less than 20 minute so the code is a bit dirty.

I've called the module "facetapi_field_sort" and this is the facetapi_field_sort.module code:

/**
 * Implements hook_facetapi_sort_info().
 */
function facetapi_field_sort_facetapi_sort_info() {
  $sorts = array();

  $sorts['taxonomy_weight'] = array(
    'label' => t('Order by field_order'),
    'callback' => 'facetapi_field_sort_field_order',
    'description' => t('Sort by field_order. WARNING: the field field_order must exists).'),
    'weight' => -40,
  );

  return $sorts;
}

/**
 * Sorts facets by field_order.
 */
function facetapi_field_sort_field_order(array $a, array $b) {
  $nid_a = $a['#indexed_value'];
  $nid_b = $b['#indexed_value'];

  $object_a = entity_metadata_wrapper('node', node_load($nid_a));
  $value_a = $object_a->field_order->value();

  $object_b = entity_metadata_wrapper('node', node_load($nid_b));
  $value_b = $object_b->field_order->value();

  $a_count = (isset($value_a)) ? $value_a : 0;
  $b_count = (isset($value_b)) ? $value_b : 0;
  if ($a_count == $b_count) {
    return 0;
  }
  return ($a_count < $b_count) ? -1 : 1;
}

What do you think about? Would be interesting to extend facetapi module in order to allow to select any indexed field on the sort settings?

summit’s picture

Hi,
What is the status of this module please?
Would love to be able to sort the results on price. With view from Commerce Kickstart I am not able to get that working...
greetings, Martijn