1

I am working on a website with facetapi + search_api, with some facets for taxonomy term references.

One of my facets is getting the machine name field_xyz_parents_all or field_xyz:parents_all, which in the URL this becomes field_xyz%253Aparents_all.

I want this facet to replace the older facet with machine name field_xyz, so that old URLs still work. So I would like to change the machine name or url query key of the new facet.

Any idea how to do this?

One idea I have would be to implement hook_entity_property_info_alter() and define my own entity property with the desired machine name, overwriting the existing entity property field_xyz. I imagine this would work, but it somehow seems wrong, because in other places I might want to keep the original meaning of this property. Ideally I would only change the facet query key, not the property.

EDIT: The original intent I had with this was to change the facet alias used in the url. However, changing the question to clarify this would make it a "chameleon question". Hence, I don't.

1

For completeness: If what you need is to change the facet alias, not the machine name, then the FacetAPI Alias module seems to do exactly this.

If you also consider using facetapi_pretty_paths, this issue might be interesting: Is facetapi_alias compatible with facetapi_pretty_paths ? (compatible, overlapping functionality, compliment one another?)

| improve this answer | |
0

Short version: Use hook_facetapi_facet_info_alter():

  • To only change the key used in the url query, just use the hook to change the 'field alias' of the field_xyz:parents_all facet.
  • To actually remap the machine name, as the original question implies, use the more complex implementation in "To remap the machine name".

To only change key used in url query

  1. On admin/config/search/search_api/index/INDEXNAME/fields, enable the field_xyz:parents_all field to be indexed.
  2. Maybe you need to disable the base field, 'field_xyz'. But maybe this is not necessary.
  3. Re-index the search index.
  4. Implement hook_facetapi_facet_info_alter() to change the 'field alias' of 'field_xyz:parents_all'.
  5. Flush all caches. Otherwise the change from the hook will not be picked up.
  6. On admin/config/search/search_api/index/INDEXNAME/fields, activate the 'field_xyz:parents_all' facet.
    Deactivate the base facet 'field_xyz' to prevent name clash.
  7. Optionally, on admin/config/search/facetapi/search_api%40INDEXNAME/block/field_xyz%3Aparents_all/edit, override the facet title (*).
  8. Activate the facet block.

Here is the code:

/**
 * Implements hook_facetapi_facet_info_alter().
 *
 * @param array $facet_info
 * @param array $searcher_info
 */
function MYMODULE_facetapi_facet_info_alter(array &$facet_info, array $searcher_info) {

  // Change the field alias.
  if (!empty($facet_info['field_xyz:parents_all'])) {
    $facet = $facet_info['field_xyz:parents_all']['field alias'] = 'field_xyz';
  }
}

To remap the machine name.

(This is the older version of this answer, which I am not deleting.)

  1. On admin/config/search/search_api/index/INDEXNAME/fields, enable the field_xyz:parents_all field to be indexed.
  2. Re-index the search index.
  3. (Optional: I also enabled the original field, field_xyz. I don't think this is necessary, but I did it anyway.)
  4. Implement hook_facetapi_facet_info_alter() to rewrite the facet machine name. See code below.
  5. Flush all caches. Otherwise the change from the hook will not be picked up.
  6. On admin/config/search/search_api/index/INDEXNAME/fields, activate the facet with the funny name (*).
  7. On admin/config/search/facetapi/search_api%40INDEXNAME/block/field_xyz/edit, override the facet title (*).
  8. Activate the facet block.

Here is the code:

/**
 * Implements hook_facetapi_facet_info_alter().
 *
 * @param array $facet_info
 * @param array $searcher_info
 */
function MYMODULE_facetapi_facet_info_alter(array &$facet_info, array $searcher_info) {

  // Re-map field_xyz.
  if (!empty($facet_info['field_xyz:parents_all'])) {
    $facet = $facet_info['field_xyz:parents_all'];
    // Optional: Remind the site builder that this facet is overridden in code (*).
    $facet['label'] .= ' (see MYMODULE.module)';
    $facet['name'] = 'field_xyz';
    // @todo Much better would be to JUST change the 'field alias'.
    $facet['field alias'] = 'field_xyz';
    $facet_info['field_xyz'] = $facet;
    // Optional: Unset the *:parents_all facet.
    unset($facet_info['field_xyz:parents_all']);
  }
}

(*) Why the funky title " (see MYMODULE.module)"?

Simply because it is too easy to forget that some custom module messed with the facets. Better to have the funky title which you then override in the UI.

| 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.