diff --git a/src/JsonApiResource/ResourceObject.php b/src/JsonApiResource/ResourceObject.php index eabc629..93dc25d 100644 --- a/src/JsonApiResource/ResourceObject.php +++ b/src/JsonApiResource/ResourceObject.php @@ -138,7 +138,15 @@ class ResourceObject implements CacheableDependencyInterface, ResourceIdentifier // The "label" field needs special treatment: some entity types have a label // field that is actually backed by a label callback. - static::ensureLabelFieldValue($entity, $fields); + $entity_type = $entity->getEntityType(); + if ($entity_type->hasLabelCallback()) { + $label_field_name = $entity_type->getKey('label'); + // @todo Remove this work-around after https://www.drupal.org/project/drupal/issues/2450793 lands. + if ($entity->getEntityTypeId() === 'user') { + $label_field_name = 'name'; + } + $fields[$label_field_name]->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)); @@ -178,25 +186,4 @@ class ResourceObject implements CacheableDependencyInterface, ResourceIdentifier return $enabled_public_fields; } - /** - * Ensures an entity's label field value is present. - * - * @param \Drupal\Core\Entity\ContentEntityInterface $entity - * The entity for which to ensure a label field value is present. - * @param \Drupal\Core\Field\FieldItemListInterface[] $fields - * The array of entity fields on which to ensure the label field value is - * present. - */ - protected static function ensureLabelFieldValue(ContentEntityInterface $entity, array &$fields) { - $entity_type = $entity->getEntityType(); - if ($entity_type->hasLabelCallback()) { - $label_field_name = $entity_type->getKey('label'); - // @todo Remove this work-around after https://www.drupal.org/project/drupal/issues/2450793 lands. - if ($entity->getEntityTypeId() === 'user') { - $label_field_name = 'name'; - } - $fields[$label_field_name]->value = $entity->label(); - } - } - } diff --git a/src/LabelOnlyEntity.php b/src/LabelOnlyEntity.php index 6762bed..a770eb4 100644 --- a/src/LabelOnlyEntity.php +++ b/src/LabelOnlyEntity.php @@ -2,9 +2,7 @@ namespace Drupal\jsonapi; -use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\TypedData\TypedDataInternalPropertiesHelper; use Drupal\jsonapi\JsonApiResource\ResourceObject; /** @@ -28,11 +26,7 @@ final class LabelOnlyEntity extends ResourceObject { * {@inheritdoc} */ protected function extractFields(EntityInterface $entity) { - if (!$entity instanceof ContentEntityInterface) { - return []; - } - $fields = TypedDataInternalPropertiesHelper::getNonInternalProperties($entity->getTypedData()); - static::ensureLabelFieldValue($entity, $fields); + $fields = parent::extractFields($entity); $label_field_name = $this->getLabelFieldName(); return $label_field_name && $this->resourceType->isFieldEnabled($label_field_name) ? [$this->resourceType->getPublicName($label_field_name) => $fields[$label_field_name]]