function ResourceObject::extractContentEntityFields
Same name in other branches
- 8.9.x core/modules/jsonapi/src/JsonApiResource/ResourceObject.php \Drupal\jsonapi\JsonApiResource\ResourceObject::extractContentEntityFields()
- 10 core/modules/jsonapi/src/JsonApiResource/ResourceObject.php \Drupal\jsonapi\JsonApiResource\ResourceObject::extractContentEntityFields()
- 11.x core/modules/jsonapi/src/JsonApiResource/ResourceObject.php \Drupal\jsonapi\JsonApiResource\ResourceObject::extractContentEntityFields()
Extracts a content entity's fields.
Parameters
\Drupal\jsonapi\ResourceType\ResourceType $resource_type: The JSON:API resource type of the given entity.
\Drupal\Core\Entity\ContentEntityInterface $entity: The config entity from which fields should be extracted.
Return value
\Drupal\Core\Field\FieldItemListInterface[] The fields extracted from a content entity.
1 call to ResourceObject::extractContentEntityFields()
- ResourceObject::extractFieldsFromEntity in core/
modules/ jsonapi/ src/ JsonApiResource/ ResourceObject.php - Extracts the entity's fields.
File
-
core/
modules/ jsonapi/ src/ JsonApiResource/ ResourceObject.php, line 302
Class
- ResourceObject
- Represents a JSON:API resource object.
Namespace
Drupal\jsonapi\JsonApiResourceCode
protected static function extractContentEntityFields(ResourceType $resource_type, ContentEntityInterface $entity) {
$output = [];
$fields = TypedDataInternalPropertiesHelper::getNonInternalProperties($entity->getTypedData());
// Filter the array based on the field names.
$enabled_field_names = array_filter(array_keys($fields), [
$resource_type,
'isFieldEnabled',
]);
// Special handling for user entities that allows a JSON:API user agent to
// access the display name of a user. For example, this is useful when
// displaying the name of a node's author.
// @todo: eliminate this special casing in https://www.drupal.org/project/drupal/issues/3079254.
$entity_type = $entity->getEntityType();
if ($entity_type->id() == 'user' && $resource_type->isFieldEnabled('display_name')) {
assert($entity instanceof UserInterface);
$display_name = $resource_type->getPublicName('display_name');
$output[$display_name] = $entity->getDisplayName();
}
// Return a sub-array of $output containing the keys in $enabled_fields.
$input = array_intersect_key($fields, array_flip($enabled_field_names));
foreach ($input as $field_name => $field_value) {
$public_field_name = $resource_type->getPublicName($field_name);
$output[$public_field_name] = $field_value;
}
return $output;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.