You are here

protected static function ResourceObject::extractConfigEntityFields in Drupal 9

Same name and namespace in other branches
  1. 8 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 339

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;
}