class DataTypeHelper in Search API 8
Provides helper methods for dealing with Search API data types.
Hierarchy
- class \Drupal\search_api\Utility\DataTypeHelper implements DataTypeHelperInterface
Expanded class hierarchy of DataTypeHelper
2 files declare their use of DataTypeHelper
- Database.php in modules/
search_api_db/ src/ Plugin/ search_api/ backend/ Database.php - TestItemsTrait.php in tests/
src/ Unit/ Processor/ TestItemsTrait.php
1 string reference to 'DataTypeHelper'
1 service uses DataTypeHelper
File
- src/
Utility/ DataTypeHelper.php, line 16
Namespace
Drupal\search_api\UtilityView source
class DataTypeHelper implements DataTypeHelperInterface {
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The data type plugin manager.
*
* @var \Drupal\search_api\DataType\DataTypePluginManager
*/
protected $dataTypeManager;
/**
* The event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* Cache for the field type mapping.
*
* @var array|null
*
* @see getFieldTypeMapping()
*/
protected $fieldTypeMapping;
/**
* Cache for the fallback data type mapping per index.
*
* @var array
*
* @see getDataTypeFallbackMapping()
*/
protected $dataTypeFallbackMapping = [];
/**
* Constructs a DataTypeHelper object.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* The module handler.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
* The event dispatcher.
* @param \Drupal\search_api\DataType\DataTypePluginManager $dataTypeManager
* The data type plugin manager.
*/
public function __construct(ModuleHandlerInterface $moduleHandler, EventDispatcherInterface $eventDispatcher, DataTypePluginManager $dataTypeManager) {
$this->moduleHandler = $moduleHandler;
$this->eventDispatcher = $eventDispatcher;
$this->dataTypeManager = $dataTypeManager;
}
/**
* {@inheritdoc}
*/
public function isTextType($type, array $textTypes = [
'text',
]) {
if (in_array($type, $textTypes)) {
return TRUE;
}
$dataType = $this->dataTypeManager
->createInstance($type);
if ($dataType && !$dataType
->isDefault()) {
return in_array($dataType
->getFallbackType(), $textTypes);
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function getFieldTypeMapping() {
// Check the cache first.
if (!isset($this->fieldTypeMapping)) {
// It's easier to write and understand this array in the form of
// $searchApiFieldType => [$dataTypes] and flip it below.
$defaultMapping = [
'text' => [
'field_item:string_long.string',
'field_item:text_long.string',
'field_item:text_with_summary.string',
'search_api_html',
'search_api_text',
],
'string' => [
'string',
'email',
'uri',
'filter_format',
'duration_iso8601',
'field_item:path',
],
'integer' => [
'integer',
'timespan',
],
'decimal' => [
'decimal',
'float',
],
'date' => [
'date',
'datetime_iso8601',
'timestamp',
],
'boolean' => [
'boolean',
],
// Types we know about but want/have to ignore.
NULL => [
'language',
],
];
foreach ($defaultMapping as $searchApiType => $dataTypes) {
foreach ($dataTypes as $dataType) {
$mapping[$dataType] = $searchApiType;
}
}
// Allow other modules to intercept and define what default type they want
// to use for their data type.
$description = 'This hook is deprecated in search_api:8.x-1.14 and is removed from search_api:2.0.0. Please use the "search_api.mapping_field_types" event instead. See https://www.drupal.org/node/3059866';
$hook = 'search_api_field_type_mapping';
$this->moduleHandler
->alterDeprecated($description, $hook, $mapping);
$eventName = SearchApiEvents::MAPPING_FIELD_TYPES;
$event = new MappingFieldTypesEvent($mapping);
$this->eventDispatcher
->dispatch($eventName, $event);
$this->fieldTypeMapping = $mapping;
}
return $this->fieldTypeMapping;
}
/**
* {@inheritdoc}
*/
public function getDataTypeFallbackMapping(IndexInterface $index) {
// Check the cache first.
$indexId = $index
->id();
if (empty($this->dataTypeFallbackMapping[$indexId])) {
$server = NULL;
try {
$server = $index
->getServerInstance();
} catch (SearchApiException $e) {
// If the server isn't available, just ignore it here and return all
// custom types.
}
$this->dataTypeFallbackMapping[$indexId] = [];
$dataTypes = $this->dataTypeManager
->getInstances();
foreach ($dataTypes as $typeId => $dataType) {
// We know for sure that we do not need to fall back for the default
// data types as they are always present and are required to be
// supported by all backends.
if (!$dataType
->isDefault() && (!$server || !$server
->supportsDataType($typeId))) {
$this->dataTypeFallbackMapping[$indexId][$typeId] = $dataType
->getFallbackType();
}
}
}
return $this->dataTypeFallbackMapping[$indexId];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DataTypeHelper:: |
protected | property | Cache for the fallback data type mapping per index. | |
DataTypeHelper:: |
protected | property | The data type plugin manager. | |
DataTypeHelper:: |
protected | property | The event dispatcher. | |
DataTypeHelper:: |
protected | property | Cache for the field type mapping. | |
DataTypeHelper:: |
protected | property | The module handler. | |
DataTypeHelper:: |
public | function |
Retrieves the necessary type fallbacks for an index. Overrides DataTypeHelperInterface:: |
|
DataTypeHelper:: |
public | function |
Retrieves the mapping for known data types to Search API's internal types. Overrides DataTypeHelperInterface:: |
|
DataTypeHelper:: |
public | function |
Determines whether fields of the given type contain fulltext data. Overrides DataTypeHelperInterface:: |
|
DataTypeHelper:: |
public | function | Constructs a DataTypeHelper object. |