Problem/Motivation

I have created a ViewsSort plugin that works on a normal view (like taxonomy_term) but not on a view based on Search API : it is not present when I click on "Add sort" in BO.

Proposed resolution

This sort is used to create a custom order of content types, and I use hook_views_data_alter() to provide it to views :

/**
 * Implements hook_views_data_alter().
 */
function mymodule_views_data_alter(array &$data) {

  // works on classic view
  $data['node_field_data']['inspiration_first'] = array(
    'title' => t('Custom search sort in page tags with inspiration first'),
    'group' => t('Search'),
    'help' => t('Sort content types in this order : dossier_inspiration, dossier_conseil, produit.'),
    'sort' => array(
      'field' => 'type',
      'id' => 'inspiration_first',
    ),
  );

  // doesn't work on search api view
  $data['search_api_db_default_search_content_index_content_type']['inspiration_first'] = array(
    'title' => t('Place inspiration first for custom search'),
    'group' => t('Search'),
    'help' => t('Sort content types in this order : dossier_inspiration, dossier_conseil, produit.'),
    'sort' => array(
      'field' => 'value',
      'id' => 'inspiration_first',
    ),
  );

}

Any idea ? Some sorts like Random work also on search api and I've provided the good table, I think (search_api_db_default_search_content_index_content_type, see above).

Versions :

I use drupal 8.4.5 and the 8.x-1.7 module.

Comments

Salvor_Hardin created an issue. See original summary.

drunken monkey’s picture

Status: Active » Fixed

No, that's not the right table. Search API Views data definitions don't use database table names, but their own system of "virtual" tables exposed to Views. You likely want something like search_api_index_default_search_content_index as the "table". You can check which tables exist with something like this:

var_export(array_keys(\Drupal::service('views.views_data')->getAll()));

The second problem you'll then probably encounter is that Search API Views don't provide database-level access to the queries, so you can only add sorts for fields you have indexed in the Search API index. So, you'll either have to add a custom "Content type weight" field to the index and sort on that, or use hook_search_api_db_query_alter() to alter the DB query directly (but without any Views configuration).

Status: Fixed » Closed (fixed)

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