Change record status: 
Introduced in branch: 
8.x-7.x
Introduced in version: 
8.x-7.0-alpha6
Description: 

The hook_elasticsearch_connector_supported_data_types_alter() hook is used to modify the list of supported data types in the elasticsearch_connector:8.x-7.x release series.

hook_elasticsearch_connector_supported_data_types_alter() has been deprecated in elasticsearch_connector:8.x-7.0-alpha6, and has been removed from elasticsearch_connector:8.0.0.

To modify the list of supported data types, Elasticsearch Connector 8.0.0 instead uses the Search API module's data types.

Define a new data type

To define a new custom data type, create a class that implements \Drupal\search_api\Annotation\SearchApiDataType and is annotated with the \Drupal\search_api\Annotation\SearchApiDataType annotation. For example...

Elasticsearch Connector 8.x-7.x code Elasticsearch Connector 8.0.x code
Old code in mymodule.module:
function mymodule_elasticsearch_connector_supported_data_types_alter(&$data_types) {
  // Add a custom "my_data_type".
  $data_types[] = 'my_data_type';
}

Old code in mymodule.services.yml:

services:
  # Define how the custom my_data_type should behave.
  mymodule.my_event_subscriber:
    class: Drupal\mymodule\MyDataTypeEventSubscriber
    tags:
      - { name: event_subscriber }

Old code in src/MyDataTypeEventSubscriber.php:

namespace Drupal\mymodule;

class MyDataTypeEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface  {

  public static function getSubscribedEvents() {
    $events[\Drupal\elasticsearch_connector\Event\PrepareMappingEvent::PREPARE_MAPPING][] = 'onPrepareMappingEvent;
  }

  public function onPrepareMappingEvent(\Drupal\elasticsearch_connector\Event\PrepareMappingEvent $event) {
    ... // Define how the custom my_data_type should behave.
  }

}

New code in src/Plugin/search_api/data_type/MyDataType.php:

namespace Drupal\mymodule\Plugin\search_api\data_type;

/**
 * @SearchApiDataType(
 *   id = "my_data_type",
 *   label = @Translation("My data type"),
 *   description = @Translation("Lorem ipsum dolor sit amet."),
 * )
 */
class MyDataType extends \Drupal\search_api\DataType\DataTypePluginBase {
  ... // Define how the custom my_data_type should behave.
}

Delete an existing data type

To delete an existing data type, subscribe to Search API's \Drupal\search_api\Event\SearchApiEvents::GATHERING_DATA_TYPES event. For example...

Elasticsearch Connector 8.x-7.x code Elasticsearch Connector 8.0.x code
Old code in mymodule.module:
function mymodule_elasticsearch_connector_supported_data_types_alter(&$data_types) {
  // Delete the "location" data type
  $key = \array_search('location', $data_types, TRUE);
  if ($key !== FALSE) {
    unset($data_types[$key]);
  }
}

New code in mymodule.services.yml:

services:
  mymodule.search_api_event_subscriber:
    class: Drupal\mymodule\SearchApiEventSubscriber
    tags:
      - { name: event_subscriber }

New code in src/SearchApiEventSubscriber.php:

namespace Drupal\mymodule;

class SearchApiEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface  {

  public static function getSubscribedEvents() {
    $events[\Drupal\search_api\Event\SearchApiEvents::GATHERING_DATA_TYPES][] = 'onGatheringDataTypesEvent';
  }

  public function onGatheringDataTypesEvent(\Drupal\search_api\Event\GatheringPluginInfoEvent $event) {
    $definitions = &$event->getDefinitions();

    // Delete the "location" data type
    unset($definitions['location']);
  }

}
Impacts: 
Site builders, administrators, editors
Module developers