0

I'm trying to build my own sorting function implementing hook_facetapi_sort() and sorting the following data by weight.

name, weight
dog, -2
cat, 1
zebra, -5
dolphin, 3
bee, 5

The code I wrote so far is the following one.

function zoo_facetapi_sort_animal(array $a, array$b) {
  $a = db_query("select weight from {animal} where name = :a", array(':a' => $a['#markup']))->fetchAssoc();
  $b = db_query("select weight from {animal} where name = :b", array(':b' => $b['#markup']))->fetchAssoc();

  $a_value = (isset($a['weight'])) ? $a['weight'] : 0;
  $b_value = (isset($b['weight'])) ? $b['weight'] : 0;

  return strnatcmp($b_value, $a_value);
}

The result is what I expected.

zebra
dog
cat
dolphin
bee

I get the following error message.

Warning: uasort(): Array was modified by the user comparison function in FacetapiWidget->applySorts() (line 230 of /var/www/zoo.dev/sites/all/modules/facetapi/plugins/facetapi/widget.inc)

How do I avoid getting that message?

0

Register your own function as FacetAPI sort callback. Then select it in the FacetAPI sort order.

http://example.com/admin/config/search/facetapi/search_api%40default_node_index/block/field_animal_type/edit

http://drupalcontrib.org/api/drupal/contributions%21facetapi%21facetapi.api.php/function/hook_facetapi_sort_info/7

NB: You also accidentally collapsed the space between array and $b

function zoo_facetapi_sort_animal(array $a, array$b) {...}

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

  $sorts['active'] = array(
    'label' => t('Animal ...'), 
    'callback' => 'zoo_facetapi_sort_animal', 
    'description' => t('Sort by whether the animal is ...'), 
    'weight' => -50,
  );

  return $sorts;
}


function zoo_facetapi_sort_animal(array $a, array $b) {
  ...
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.