You are here

protected function RelationshipFieldDefinitionNormalizer::normalizeRelationship in Schemata 8

Normalizes the relationship.

Parameters

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: The field definition.

Return value

array The normalized relationship.

1 call to RelationshipFieldDefinitionNormalizer::normalizeRelationship()
RelationshipFieldDefinitionNormalizer::normalize in schemata_json_schema/src/Normalizer/jsonapi/RelationshipFieldDefinitionNormalizer.php
Normalizes an object into a set of arrays/scalars.

File

schemata_json_schema/src/Normalizer/jsonapi/RelationshipFieldDefinitionNormalizer.php, line 97

Class

RelationshipFieldDefinitionNormalizer
Normalizer for RelationshipFieldDefinitionNormalizer objects.

Namespace

Drupal\schemata_json_schema\Normalizer\jsonapi

Code

protected function normalizeRelationship(FieldDefinitionInterface $field_definition) {

  /** @var \Drupal\jsonapi\ResourceType\ResourceTypeRepository $resource_type_repository */
  $resource_type_repository = \Drupal::service('jsonapi.resource_type.repository');

  // A relationship has very similar schema every time.
  $resource_identifier_object = [
    'type' => 'object',
    'required' => [
      'type',
      'id',
    ],
    'properties' => [
      'type' => [
        'type' => 'string',
        'title' => t('Referenced resource'),
      ],
      'id' => [
        'type' => 'string',
        'title' => t('Resource ID'),
        'format' => 'uuid',
        'maxLength' => 128,
      ],
    ],
  ];

  // Handle the multivalue variant.
  $cardinality = $field_definition
    ->getFieldStorageDefinition()
    ->getCardinality();

  /* @var $entity \Drupal\Core\TypedData\DataReferenceDefinitionInterface */
  if ($target_entity_type = $field_definition
    ->getSetting('target_type')) {
    $handler_settings = $field_definition
      ->getSetting('handler_settings');
    $target_bundles = empty($handler_settings['target_bundles']) ? [
      $target_entity_type,
    ] : array_values($handler_settings['target_bundles']);
    $target_resource_types = array_map(function ($bundle) use ($target_entity_type, $resource_type_repository) {
      return $resource_type_repository
        ->get($target_entity_type, $bundle ?: $target_entity_type);
      return $resource_type
        ->getTypeName();
    }, $target_bundles);
    $enum = array_map(function (ResourceType $resource_type) {
      return $resource_type
        ->getTypeName();
    }, array_filter($target_resource_types));
  }
  if ($cardinality == 1) {
    $data = $resource_identifier_object;
    if (!empty($enum)) {
      $data['properties']['type']['enum'] = $enum;
    }
  }
  else {
    $data = [
      'type' => 'array',
      'items' => $resource_identifier_object,
    ];
    if (!empty($enum)) {
      $data['items']['properties']['type']['enum'] = $enum;
    }
  }
  $normalized = [
    'type' => 'object',
    'properties' => [
      'data' => $data,
    ],
    'title' => $field_definition
      ->getLabel(),
  ];
  return $normalized;
}