You are here

function _search_api_views_get_handlers in Search API 8

Returns the Views handlers to use for a given field.

Parameters

\Drupal\search_api\Item\FieldInterface $field: The field to add to the definition.

Return value

array The Views definition to add for the given field.

1 call to _search_api_views_get_handlers()
search_api_views_data in ./search_api.views.inc
Implements hook_views_data().

File

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

Code

function _search_api_views_get_handlers(FieldInterface $field) {
  $mapping = _search_api_views_handler_mapping();
  try {
    $types = [];
    $definition = $field
      ->getDataDefinition();
    $entity_type_id = $definition
      ->getSetting('target_type');
    if ($entity_type_id) {
      $entity_type = \Drupal::entityTypeManager()
        ->getDefinition($entity_type_id);
      $bundle_of = $entity_type
        ->getBundleOf();
      if ($bundle_of) {
        $types[] = "bundle_of:{$bundle_of}";
        $types[] = [
          'bundle_of',
          $bundle_of,
        ];
      }
      $types[] = "entity:{$entity_type_id}";
      $types[] = [
        'entity',
        $entity_type_id,
      ];
    }

    // Special treatment for languages (as we have no specific Search API data
    // type for those).
    if ($definition
      ->getSetting('views_type') === 'language') {
      $types[] = 'language';
    }
    if ($definition
      ->getSetting('allowed_values')) {
      $types[] = 'options';
    }
    $types[] = $field
      ->getType();

    /** @var \Drupal\search_api\DataType\DataTypeInterface $data_type */
    $data_type = \Drupal::service('plugin.manager.search_api.data_type')
      ->createInstance($field
      ->getType());
    if (!$data_type
      ->isDefault()) {
      $types[] = $data_type
        ->getFallbackType();
    }
    foreach ($types as $type) {
      $sub_type = NULL;
      if (is_array($type)) {
        list($type, $sub_type) = $type;
      }
      if (isset($mapping[$type])) {
        _search_api_views_handler_adjustments($type, $field, $mapping[$type], $sub_type);
        return $mapping[$type];
      }
    }
  } catch (SearchApiException $e) {
    $vars['%index'] = $field
      ->getIndex()
      ->label();
    $vars['%field'] = $field
      ->getPrefixedLabel();
    watchdog_exception('search_api', $e, '%type while adding Views handlers for field %field on index %index: @message in %function (line %line of %file).', $vars);
  } catch (PluginNotFoundException $e) {
    $vars['%index'] = $field
      ->getIndex()
      ->label();
    $vars['%field'] = $field
      ->getPrefixedLabel();
    watchdog_exception('search_api', $e, '%type while adding Views handlers for field %field on index %index: @message in %function (line %line of %file).', $vars);
  }
  return [];
}