You are here

public function ContentEntityCdfNormalizer::getFieldTypeMapping in Acquia Content Hub 8

Retrieves the mapping for known data types to Content Hub's internal types.

Inspired by the getFieldTypeMapping in search_api.

Search API uses the complex data format to normalize the data into a document-structure suitable for search engines. However, since content hub for Drupal 8 just got started, it focusses on the field types for now instead of on the complex data types. Changing this architecture would mean that we have to adopt a very similar structure as can be seen in the Utility class of Search API. That would also mean we no longer have to explicitly support certain field types as they map back to the known complex data types such as string, uri that are known in Drupal Core.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: An entity to map fields to.

Return value

string[] An array mapping all known (and supported) Drupal field types to their corresponding Content Hub data types. Empty values mean that fields of that type should be ignored by the Content Hub.

See also

hook_acquia_contenthub_field_type_mapping_alter()

1 call to ContentEntityCdfNormalizer::getFieldTypeMapping()
ContentEntityCdfNormalizer::addFieldsToContentHubEntity in src/Normalizer/ContentEntityCdfNormalizer.php
Get fields from given entity.

File

src/Normalizer/ContentEntityCdfNormalizer.php, line 1018

Class

ContentEntityCdfNormalizer
Converts the Drupal entity object to a Acquia Content Hub CDF array.

Namespace

Drupal\acquia_contenthub\Normalizer

Code

public function getFieldTypeMapping(ContentEntityInterface $entity) {

  // Getting the bundle key.
  $bundle_key = $this->entityTypeManager
    ->getDefinition($entity
    ->getEntityTypeId())
    ->getKey('bundle');
  $langcode_key = $this->entityTypeManager
    ->getDefinition($entity
    ->getEntityTypeId())
    ->getKey('langcode');
  $mapping = [];

  // It's easier to write and understand this array in the form of
  // $default_mapping => [$data_types] and flip it below.
  $default_mapping = [
    'string' => [
      // These are special field names that we do not want to parse as
      // arrays.
      'title',
      $bundle_key,
      $langcode_key,
      // This is a special field that we will want to parse as string for now.
      // @todo Replace this to work with taxonomy_vocabulary entities.
      'vid',
    ],
    'array<string>' => [
      'fallback',
      'text_with_summary',
      'image',
      'file',
      'video',
    ],
    'array<reference>' => [
      'entity_reference',
      'entity_reference_revisions',
    ],
    'array<integer>' => [
      'integer',
      'timespan',
      'timestamp',
    ],
    'array<number>' => [
      'decimal',
      'float',
    ],
    // Types we know about but want/have to ignore.
    NULL => [
      'password',
    ],
    'array<boolean>' => [
      'boolean',
    ],
  ];
  foreach ($default_mapping as $contenthub_type => $data_types) {
    foreach ($data_types as $data_type) {
      $mapping[$data_type] = $contenthub_type;
    }
  }

  // Allow other modules to intercept and define what default type they want
  // to use for their data type.
  $this->moduleHandler
    ->alter('acquia_contenthub_field_type_mapping', $mapping);
  return $mapping;
}