You are here

protected function ConfigEntityNormalizer::getFields in JSON:API 8

Gets the field names for the given entity.

Parameters

mixed $entity: The entity.

string $bundle: The bundle id.

\Drupal\jsonapi\ResourceType\ResourceType $resource_type: The resource type.

Return value

array The fields.

Overrides EntityNormalizer::getFields

File

src/Normalizer/ConfigEntityNormalizer.php, line 28

Class

ConfigEntityNormalizer
Converts the Drupal config entity object to a JSON API array structure.

Namespace

Drupal\jsonapi\Normalizer

Code

protected function getFields($entity, $bundle, ResourceType $resource_type) {
  $enabled_public_fields = [];
  $fields = static::getDataWithoutInternals($entity
    ->toArray());

  // Filter the array based on the field names.
  $enabled_field_names = array_filter(array_keys($fields), [
    $resource_type,
    'isFieldEnabled',
  ]);

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