While developing a site using search api, search api ranges in combination with entity translation we found that the module causes a fatal error expecting a valid class or name.

Because we are using entity translation the function search_api_ranges_minmax expects $index->type to be a valid entity type, when using entity translation the returned value is search_api_et_node and the entity id is prefixed EG en_2404 instead of 2404.

We solved this issue by changing the code around line 185 in search_api_ranges.module to:

  $result_ids = array();
  foreach ($results['results'] as $result) {
      if($index->item_type == 'search_api_et_node') {
        $result_ids[] = is_numeric($result['id']) ? $result['id'] : substr($result['id'], 3);
      }
      else {
        $result_ids[] = $result['id'];
      }
  }
    if($index->item_type == 'search_api_et_node') {
        $entities = entity_load('node', $result_ids);
    } else {
        $entities = entity_load($index->item_type, $result_ids);
    }

If the type is search_api_et_node we remove the prefix (substr starting from 3) and load the content by hardcoding the type to node to load the entities.
Beware: this solution works for this project but might not work if your index also contains language neutral content or other types of entities.

Comments

maciej.zgadzaj’s picture

Priority: Normal » Major
Status: Active » Needs review
FileSize
638 bytes

The module shouldn't assume that item_type is valid entity type. Instead, it should use relevant method provided for this exact purpose SearchApiDataSourceControllerInterface::getEntityType().

Patch attached.

Martin.’s picture

This patch only fixes the fatal error. The filter itself is not working. Did you actually got it working ? Please share the solution if you did.
Might be related with https://www.drupal.org/node/2321269

detroz’s picture

Hi,

I propose this alteration of the function search_api_ranges_minmax($variables, $order = 'ASC') on the file "search_api_ranges.module" :

  if (!$results['results']) {
    return NULL;
  }

  $result_ids = array();
  foreach ($results['results'] as $result) {
+     if ($index->item_type == 'search_api_et_node') {
+       $result['id'] = substr($result['id'], strrpos($result['id'], '_') + 1);
+     }
    $result_ids[] = $result['id'];
  }
-   $entities = entity_load($index->item_type, $result_ids);
+   $entities = entity_load($index->getEntityType(), $result_ids);

  $index->dataAlter($entities);

  $orphan_ids = array();
Aambro’s picture

I couldn't apply the patch as the code was slightlly different:

  foreach ($results['results'] as $result) {
    $entity = entity_load($variables['index']->item_type, array($result['id']));
    $variables['index']->dataAlter($entity);
    $wrapper = $variables['index']->entityWrapper($entity[$result['id']]);
  }

So I just changed the item_type to getEntityType()

Final code, that works for me:

  foreach ($results['results'] as $result) {
    $entity = entity_load($variables['index']->getEntityType(), array($result['id']));
    $variables['index']->dataAlter($entity);
    $wrapper = $variables['index']->entityWrapper($entity[$result['id']]);
  }
drclaw’s picture

Category: Bug report » Feature request
FileSize
1.05 KB

Here's a patch that adds support for any entity type that search_api_et may have multilingual indexes for.

Note that @Aambro's code in comment #4 is for the 7.x-1.5 version of the module, whereas the patch is for the development branch.

gonssal’s picture

Status: Needs review » Reviewed & tested by the community

I confirm the patch in #5 works. Thx drclaw and the rest.

Maybe time to commit and release as new version, so I can upgrade commerce kickstart without having to download the dev version and applying the patch everytime? ;)

Scott Robertson’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.