You are here

function _search_api_views_handler_adjustments in Search API 8

Makes necessary, field-specific adjustments to Views handler definitions.

Parameters

string $type: The type of field, as defined in _search_api_views_handler_mapping().

\Drupal\search_api\Item\FieldInterface $field: The field whose handler definitions are being created.

array $definitions: The handler definitions for the field, as a reference.

string|null $sub_type: (optional) If applicable, the field's sub-type.

Throws

\Drupal\search_api\SearchApiException Thrown if the field's data definition could not be retrieved.

1 call to _search_api_views_handler_adjustments()
_search_api_views_get_handlers in ./search_api.views.inc
Returns the Views handlers to use for a given field.

File

./search_api.views.inc, line 431
Views hook implementations for the Search API module.

Code

function _search_api_views_handler_adjustments($type, FieldInterface $field, array &$definitions, string $sub_type = NULL) {

  // By default, all fields can be empty (or at least have to be treated that
  // way by the Search API).
  if (!isset($definitions['filter']['allow empty'])) {
    $definitions['filter']['allow empty'] = TRUE;
  }

  // For taxonomy term references, set the referenced vocabulary.
  $data_definition = $field
    ->getDataDefinition();
  if ($type == 'entity:taxonomy_term') {
    if (isset($data_definition
      ->getSettings()['handler_settings']['target_bundles'])) {
      $target_bundles = $data_definition
        ->getSettings()['handler_settings']['target_bundles'];
      if (count($target_bundles) == 1) {
        $definitions['filter']['vocabulary'] = reset($target_bundles);
      }
    }
  }
  elseif ($type == 'options') {
    if ($data_definition instanceof FieldItemDataDefinition) {

      // If this is a normal Field API field, dynamically retrieve the options
      // list at query time.
      $field_definition = $data_definition
        ->getFieldDefinition();
      $bundle = $field_definition
        ->getTargetBundle();
      $field_name = $field_definition
        ->getName();
      $entity_type = $field_definition
        ->getTargetEntityTypeId();
      $definitions['filter']['options callback'] = '_search_api_views_get_allowed_values';
      $definitions['filter']['options arguments'] = [
        $entity_type,
        $bundle,
        $field_name,
      ];
    }
    else {

      // Otherwise, include the options list verbatim in the Views data, unless
      // it's too big (or doesn't look valid).
      $options = $data_definition
        ->getSetting('allowed_values');
      if (is_array($options) && count($options) <= 50) {

        // Since the Views InOperator filter plugin doesn't allow just including
        // the options in the definition, we use this workaround.
        $definitions['filter']['options callback'] = 'array_filter';
        $definitions['filter']['options arguments'] = [
          $options,
        ];
      }
    }
  }
  elseif ($type === 'bundle_of' && $sub_type) {
    $definitions['filter']['options callback'] = '_search_api_views_get_bundle_names';
    $definitions['filter']['options arguments'] = [
      $sub_type,
    ];
  }
}