function ResourceType::updateDeprecatedFieldMapping

Takes a deprecated field mapping and converts it to ResourceTypeFields.

Parameters

array $field_mapping: The deprecated field mapping.

string $entity_type_id: The entity type ID of the field mapping.

string $bundle: The bundle ID of the field mapping or the entity type ID if the entity type does not have bundles.

Return value

\Drupal\jsonapi\ResourceType\ResourceTypeField[] The updated field mapping objects.

Deprecated

in drupal:8.8.0 and is removed from drupal:9.0.0. Use self::getFields() instead.

See also

https://www.drupal.org/project/drupal/issues/3014277

1 call to ResourceType::updateDeprecatedFieldMapping()
ResourceType::__construct in core/modules/jsonapi/src/ResourceType/ResourceType.php
Instantiates a ResourceType object.

File

core/modules/jsonapi/src/ResourceType/ResourceType.php, line 482

Class

ResourceType
Value object containing all metadata for a JSON:API resource type.

Namespace

Drupal\jsonapi\ResourceType

Code

private function updateDeprecatedFieldMapping(array $field_mapping, $entity_type_id, $bundle) {
    $class_name = self::class;
    @trigger_error("Passing an array with strings or booleans as a field mapping to {$class_name}::__construct() is deprecated in Drupal 8.8.0 and will not be allowed in Drupal 9.0.0. See \\Drupal\\jsonapi\\ResourceTypeRepository::getFields(). See https://www.drupal.org/node/3084746.", E_USER_DEPRECATED);
    // See \Drupal\jsonapi\ResourceType\ResourceTypeRepository::isReferenceFieldDefinition().
    $is_reference_field_definition = function (FieldDefinitionInterface $field_definition) {
        static $field_type_is_reference = [];
        if (isset($field_type_is_reference[$field_definition->getType()])) {
            return $field_type_is_reference[$field_definition->getType()];
        }
        
        /* @var \Drupal\Core\Field\TypedData\FieldItemDataDefinition $item_definition */
        $item_definition = $field_definition->getItemDefinition();
        $main_property = $item_definition->getMainPropertyName();
        $property_definition = $item_definition->getPropertyDefinition($main_property);
        return $field_type_is_reference[$field_definition->getType()] = $property_definition instanceof DataReferenceTargetDefinition;
    };
    
    /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */
    $entity_type_manager = \Drupal::service('entity_type.manager');
    $is_fieldable = $entity_type_manager->getDefinition($entity_type_id)
        ->entityClassImplements(FieldableEntityInterface::class);
    $field_definitions = $is_fieldable ? \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type_id, $bundle) : [];
    $fields = [];
    foreach ($field_mapping as $internal_field_name => $public_field_name) {
        assert(is_bool($public_field_name) || is_string($public_field_name));
        $field_definition = $is_fieldable && !empty($field_definitions[$internal_field_name]) ? $field_definitions[$internal_field_name] : NULL;
        $is_relationship_field = $field_definition && $is_reference_field_definition($field_definition);
        $has_one = !$field_definition || $field_definition->getFieldStorageDefinition()
            ->getCardinality() === 1;
        $alias = is_string($public_field_name) ? $public_field_name : NULL;
        $fields[$internal_field_name] = $is_relationship_field ? new ResourceTypeRelationship($internal_field_name, $alias, $public_field_name !== FALSE, $has_one) : new ResourceTypeAttribute($internal_field_name, $alias, $public_field_name !== FALSE, $has_one);
    }
    return $fields;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.