You are here

protected static function ResourceTypeRepository::getFieldMapping in JSON:API 8.2

Gets the field mapping for the given field names and entity type + bundle.

Parameters

string[] $field_names: All field names on a bundle of the given entity type.

\Drupal\Core\Entity\EntityTypeInterface $entity_type: The entity type for which to get the field mapping.

string $bundle: The bundle to assess.

Return value

array An array with:

  • keys are (real/internal) field names
  • values are either FALSE (indicating the field is not exposed despite not being internal), TRUE (indicating the field should be exposed under its internal name) or a string (indicating the field should not be exposed using its internal name, but the name specified in the string)
3 calls to ResourceTypeRepository::getFieldMapping()
AliasingResourceTypeRepository::getFieldMapping in tests/modules/jsonapi_test_field_aliasing/src/ResourceType/AliasingResourceTypeRepository.php
Gets the field mapping for the given field names and entity type + bundle.
CountableResourceTypeRepository::createResourceType in tests/modules/jsonapi_test_collection_count/src/ResourceType/CountableResourceTypeRepository.php
Creates a ResourceType value object for the given entity type + bundle.
ResourceTypeRepository::createResourceType in src/ResourceType/ResourceTypeRepository.php
Creates a ResourceType value object for the given entity type + bundle.
1 method overrides ResourceTypeRepository::getFieldMapping()
AliasingResourceTypeRepository::getFieldMapping in tests/modules/jsonapi_test_field_aliasing/src/ResourceType/AliasingResourceTypeRepository.php
Gets the field mapping for the given field names and entity type + bundle.

File

src/ResourceType/ResourceTypeRepository.php, line 196

Class

ResourceTypeRepository
Provides a repository of all JSON:API resource types.

Namespace

Drupal\jsonapi\ResourceType

Code

protected static function getFieldMapping(array $field_names, EntityTypeInterface $entity_type, $bundle) {
  assert(Inspector::assertAllStrings($field_names));
  assert($entity_type instanceof ContentEntityTypeInterface || $entity_type instanceof ConfigEntityTypeInterface);
  assert(is_string($bundle) && !empty($bundle), 'A bundle ID is required. Bundleless entity types should pass the entity type ID again.');
  $mapping = [];

  // JSON:API resource identifier objects are sufficient to identify
  // entities. By exposing all fields as attributes, we expose unwanted,
  // confusing or duplicate information:
  // - exposing an entity's ID (which is not a UUID) is bad, but it's
  //   necessary for certain Drupal-coupled clients, so we alias it by
  //   prefixing it with `drupal_internal__`.
  // - exposing an entity's UUID as an attribute is useless (it's already part
  //   of the mandatory "id" attribute in JSON:API), so we disable it in most
  //   cases.
  // - exposing its revision ID as an attribute will compete with any profile
  //   defined meta members used for resource object versioning.
  // @see http://jsonapi.org/format/#document-resource-identifier-objects
  $id_field_name = $entity_type
    ->getKey('id');
  $uuid_field_name = $entity_type
    ->getKey('uuid');
  if ($uuid_field_name !== 'id') {
    $mapping[$uuid_field_name] = FALSE;
  }
  $mapping[$id_field_name] = "drupal_internal__{$id_field_name}";
  if ($entity_type
    ->isRevisionable() && ($revision_id_field_name = $entity_type
    ->getKey('revision'))) {
    $mapping[$revision_id_field_name] = "drupal_internal__{$revision_id_field_name}";
  }
  if ($entity_type instanceof ConfigEntityTypeInterface) {

    // The '_core' key is reserved by Drupal core to handle complex edge cases
    // correctly. Data in the '_core' key is irrelevant to clients reading
    // configuration, and is not allowed to be set by clients writing
    // configuration: it is for Drupal core only, and managed by Drupal core.
    // @see https://www.drupal.org/node/2653358
    $mapping['_core'] = FALSE;
  }

  // For all other fields,  use their internal field name also as their public
  // field name.  Unless they're called "id" or "type": those names are
  // reserved by the JSON:API spec.
  // @see http://jsonapi.org/format/#document-resource-object-fields
  foreach (array_diff($field_names, array_keys($mapping)) as $field_name) {
    if ($field_name === 'id' || $field_name === 'type') {
      $alias = $entity_type
        ->id() . '_' . $field_name;
      if (isset($field_name[$alias])) {
        throw new \LogicException('The generated alias conflicts with an existing field. Please report this in the JSON:API issue queue!');
      }
      $mapping[$field_name] = $alias;
      continue;
    }

    // The default, which applies to most fields: expose as-is.
    $mapping[$field_name] = TRUE;
  }
  return $mapping;
}