protected static function ResourceObject::extractContentEntityFields in JSON:API 8.2
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 src/
JsonApiResource/ ResourceObject.php - Extracts the entity's fields.
File
- src/
JsonApiResource/ ResourceObject.php, line 275
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',
]);
// The "label" field needs special treatment: some entity types have a label
// field that is actually backed by a label callback.
$entity_type = $entity
->getEntityType();
if ($entity_type
->hasLabelCallback()) {
$fields[static::getLabelFieldName($entity)]->value = $entity
->label();
}
// 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;
}