function ResourceObject::extractConfigEntityFields

Same name in other branches
  1. 9 core/modules/jsonapi/src/JsonApiResource/ResourceObject.php \Drupal\jsonapi\JsonApiResource\ResourceObject::extractConfigEntityFields()
  2. 8.9.x core/modules/jsonapi/src/JsonApiResource/ResourceObject.php \Drupal\jsonapi\JsonApiResource\ResourceObject::extractConfigEntityFields()
  3. 10 core/modules/jsonapi/src/JsonApiResource/ResourceObject.php \Drupal\jsonapi\JsonApiResource\ResourceObject::extractConfigEntityFields()

Extracts a config entity's fields.

Parameters

\Drupal\jsonapi\ResourceType\ResourceType $resource_type: The JSON:API resource type of the given entity.

\Drupal\Core\Config\Entity\ConfigEntityInterface $entity: The config entity from which fields should be extracted.

Return value

array The fields extracted from a config entity.

1 call to ResourceObject::extractConfigEntityFields()
ResourceObject::extractFieldsFromEntity in core/modules/jsonapi/src/JsonApiResource/ResourceObject.php
Extracts the entity's fields.

File

core/modules/jsonapi/src/JsonApiResource/ResourceObject.php, line 365

Class

ResourceObject
Represents a JSON:API resource object.

Namespace

Drupal\jsonapi\JsonApiResource

Code

protected static function extractConfigEntityFields(ResourceType $resource_type, ConfigEntityInterface $entity) {
    $enabled_public_fields = [];
    $fields = $entity->toArray();
    // Filter the array based on the field names.
    $enabled_field_names = array_filter(array_keys($fields), function ($internal_field_name) use ($resource_type) {
        // Config entities have "fields" which aren't known to the resource type,
        // these fields should not be excluded because they cannot be enabled or
        // disabled.
        return !$resource_type->hasField($internal_field_name) || $resource_type->isFieldEnabled($internal_field_name);
    });
    // Return a sub-array of $output containing the keys in $enabled_fields.
    $input = array_intersect_key($fields, array_flip($enabled_field_names));
    
    /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity */
    foreach ($input as $field_name => $field_value) {
        $public_field_name = $resource_type->getPublicName($field_name);
        $enabled_public_fields[$public_field_name] = $field_value;
    }
    return $enabled_public_fields;
}

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