diff --git a/src/JsonApiResource/ResourceObject.php b/src/JsonApiResource/ResourceObject.php index 93dc25d..eabc629 100644 --- a/src/JsonApiResource/ResourceObject.php +++ b/src/JsonApiResource/ResourceObject.php @@ -138,15 +138,7 @@ 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. - $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(); - } + static::ensureLabelFieldValue($entity, $fields); // Return a sub-array of $output containing the keys in $enabled_fields. $input = array_intersect_key($fields, array_flip($enabled_field_names)); @@ -186,4 +178,25 @@ 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 e2a1313..6762bed 100644 --- a/src/LabelOnlyEntity.php +++ b/src/LabelOnlyEntity.php @@ -2,8 +2,9 @@ namespace Drupal\jsonapi; +use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\Entity\FieldableEntityInterface; +use Drupal\Core\TypedData\TypedDataInternalPropertiesHelper; use Drupal\jsonapi\JsonApiResource\ResourceObject; /** @@ -27,12 +28,14 @@ final class LabelOnlyEntity extends ResourceObject { * {@inheritdoc} */ protected function extractFields(EntityInterface $entity) { - if (!$entity instanceof FieldableEntityInterface) { + if (!$entity instanceof ContentEntityInterface) { return []; } + $fields = TypedDataInternalPropertiesHelper::getNonInternalProperties($entity->getTypedData()); + static::ensureLabelFieldValue($entity, $fields); $label_field_name = $this->getLabelFieldName(); return $label_field_name && $this->resourceType->isFieldEnabled($label_field_name) ? - [$this->resourceType->getPublicName($label_field_name) => $entity->get($label_field_name)] + [$this->resourceType->getPublicName($label_field_name) => $fields[$label_field_name]] : []; }