You are here

protected function EntityNormalizer::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.

2 calls to EntityNormalizer::getFields()
EntityNormalizer::getFieldNames in src/Normalizer/EntityNormalizer.php
Gets the field names for the given entity.
EntityNormalizer::normalize in src/Normalizer/EntityNormalizer.php
Normalizes an object into a set of arrays/scalars.
1 method overrides EntityNormalizer::getFields()
ConfigEntityNormalizer::getFields in src/Normalizer/ConfigEntityNormalizer.php
Gets the field names for the given entity.

File

src/Normalizer/EntityNormalizer.php, line 182

Class

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

Namespace

Drupal\jsonapi\Normalizer

Code

protected function getFields($entity, $bundle, ResourceType $resource_type) {
  $output = [];

  // @todo Remove this when JSON API requires Drupal 8.5 or newer.
  if (floatval(\Drupal::VERSION) >= 8.5) {
    $fields = TypedDataInternalPropertiesHelper::getNonInternalProperties($entity
      ->getTypedData());
  }
  else {
    $fields = $entity
      ->getFields();
  }

  // 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\Entity\ContentEntityInterface $entity */
  foreach ($input as $field_name => $field_value) {
    $public_field_name = $resource_type
      ->getPublicName($field_name);
    $output[$public_field_name] = $field_value;
  }
  return $output;
}