You are here

function search_api_grouping_generate_keys in Search API Grouping 7.2

Recursive callback to generate the elements of our pseudo keys.

Parameters

string $entity_type: The entity type.

object $entity: The entity object.

array $fields: Associative array of field names to use when computing the cartesian product. The array key is the field name, the value the permutation limit. To disable limit use 0.

array $keys: Recursive data holder stores array of field indexes to use in generating pseudo document.

See also

search_api_grouping_generate_pseudo_keys()

1 call to search_api_grouping_generate_keys()
search_api_grouping_generate_pseudo_keys in ./search_api_grouping.module
Generate our custom pseudo keys based on the entity data.

File

./search_api_grouping.module, line 262
Module search_api_grouping.

Code

function search_api_grouping_generate_keys($entity_type, $entity, $fields, &$keys) {
  if (!empty($fields)) {
    $field_name = key($fields);
    $permutation_limit = array_shift($fields);
    if (!empty($field_name)) {
      $values = field_get_items($entity_type, $entity, $field_name);

      // Limit values if the permutations are limited.
      if (is_array($values) && $permutation_limit) {
        $values = array_slice($values, 0, $permutation_limit);
      }
      if (!empty($keys)) {
        $newkeys = array();

        // Develop the cross-product of existing keys and our new values.
        if (!empty($values)) {
          foreach ($values as $id => $value) {
            foreach ($keys as $key) {
              $key[] = $id;
              $newkeys[] = $key;
            }
          }
        }
        else {

          // This field doesn't have any values, but we need a placeholder.
          $key[] = 0;
          $newkeys[] = $key;
        }
        $keys = $newkeys;
      }
      else {

        // Fresh key stack, add all of our values.
        if (!empty($values)) {
          foreach ($values as $id => $value) {
            $keys[] = array(
              $id,
            );
          }
        }
        else {

          // This field doesn't have any values, but we need a placeholder.
          $keys[] = array(
            0,
          );
        }
      }
    }
    search_api_grouping_generate_keys($entity_type, $entity, $fields, $keys);
  }
}