Аватар пользователя Dima

How to programmatically change settings for facets [Drupal 7]

Lets say you have a long list of facets for specific search index.
And you need to set all facet widgets to "facetapi_checkbox_links".

With manual input it will takes you a long time.

So here is a way to programmatically change the settings for bunch of facets on the site.
I am using apachesolr_search module, but it should works also with search_api because
facetapi is not depend on search backend, the search modules only uses it to provide facets.

  1. // I have two indexes on my site.
  2. // $searcher = 'apachesolr @ indexdepot'; // remove spaces.
  3. // You can extract $searcher name from the path when you editing specific facet display.
  4. // for example this path admin/config/search/facetapi/apachesolr%40indexdepot/block/author/edit.
  5. // $searcher = apachesolr%40indexdepot, and "%40" is urlencoded "@" sign.
  6.  
  7. $searcher = 'apachesolr @ solr'; // I have added extra spaces before and after @, remove spaces after copy-past.
  8. $realm = 'block'; // mostly it is block, see facetapi
  9. $adapter = facetapi_adapter_load($searcher);
  10. $facet_info = facetapi_get_facet_info($searcher);
  11.  
  12. foreach (array_keys($facet_info) as $item) {
  13. $facet = facetapi_facet_load($item, $searcher) ;
  14. $global_settings = $adapter->getFacet($facet)->getSettings();
  15.  
  16. // Change the operator.
  17. $global_settings->settings['operator'] = 'or';
  18.  
  19. $facet_settings = $adapter->getFacet($facet)->getSettings($realm);
  20.  
  21. // Change the widget.
  22. $facet_settings->settings['widget'] = 'facetapi_checkbox_links';
  23.  
  24. // Save the setting.
  25. ctools_export_crud_save('facetapi', $facet_settings);
  26. ctools_export_crud_save('facetapi', $global_settings);
  27. }

Tags: