// @see http://jsonapi.org/format/#document-resource-object-fields
$reserved_field_names =
['id', 'type'
];
foreach (array_diff($field_names,
array_keys($fields)) as $field_name) { $alias =
$field_name;
// Alias the fields reserved by the JSON:API spec with `{entity_type}_`.
if (in_array($field_name,
$reserved_field_names, TRUE
)) { $alias =
$entity_type->
id() . '_' .
$field_name;
} // The default, which applies to most fields: expose as-is.
$field_definition =
$is_fieldable && !
empty($field_definitions[$field_name]) ?
$field_definitions[$field_name] : NULL;
$is_relationship_field =
$field_definition &&
static::
isReferenceFieldDefinition($field_definition);
$has_one = !
$field_definition ||
$field_definition->
getFieldStorageDefinition()->
getCardinality() === 1;
$fields[$field_name] =
$is_relationship_field ?
new ResourceTypeRelationship($field_name,
$alias, TRUE,
$has_one) :
new ResourceTypeAttribute($field_name,
$alias, TRUE,
$has_one);
} // With all fields now aliased, detect any conflicts caused by the
// automatically generated aliases above.
foreach (array_intersect($reserved_field_names,
array_keys($fields)) as $reserved_field_name) { /** @var \Drupal\jsonapi\ResourceType\ResourceTypeField $aliased_reserved_field */
$aliased_reserved_field =
$fields[$reserved_field_name];