src/JsonApiResource/ResourceObject.php | 22 ++++++++--- src/LabelOnlyResourceObject.php | 18 +-------- src/Normalizer/ConfigEntityDenormalizer.php | 4 +- src/Normalizer/ContentEntityDenormalizer.php | 6 +-- src/Normalizer/EntityDenormalizerBase.php | 9 +---- src/Normalizer/Relationship.php | 6 +-- src/Normalizer/RelationshipItem.php | 6 +-- src/Normalizer/ResourceObjectNormalizer.php | 45 ++++------------------ .../JsonApiDocumentTopLevelNormalizerTest.php | 1 - 9 files changed, 35 insertions(+), 82 deletions(-) diff --git a/src/JsonApiResource/ResourceObject.php b/src/JsonApiResource/ResourceObject.php index 0893575..4de5db1 100644 --- a/src/JsonApiResource/ResourceObject.php +++ b/src/JsonApiResource/ResourceObject.php @@ -140,12 +140,7 @@ class ResourceObject implements CacheableDependencyInterface, ResourceIdentifier // 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(); + $fields[$this->getLabelFieldName()]->value = $entity->label(); } // Return a sub-array of $output containing the keys in $enabled_fields. @@ -158,6 +153,21 @@ class ResourceObject implements CacheableDependencyInterface, ResourceIdentifier } /** + * Determines the entity type's (internal) label field name. + * + * @return string + * The label field name. + */ + protected function getLabelFieldName() { + $label_field_name = $this->entity->getEntityType()->getKey('label'); + // @todo Remove this work-around after https://www.drupal.org/project/drupal/issues/2450793 lands. + if ($this->entity->getEntityTypeId() === 'user') { + $label_field_name = 'name'; + } + return $label_field_name; + } + + /** * Extracts a config entity's fields. * * @param \Drupal\Core\Config\Entity\ConfigEntityInterface $entity diff --git a/src/LabelOnlyResourceObject.php b/src/LabelOnlyResourceObject.php index 51f6462..f0d567b 100644 --- a/src/LabelOnlyResourceObject.php +++ b/src/LabelOnlyResourceObject.php @@ -27,22 +27,8 @@ final class LabelOnlyResourceObject extends ResourceObject { */ protected function extractFields(EntityInterface $entity) { $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]] - : []; - } - - /** - * Determines the entity type's (internal) label field name. - */ - public function getLabelFieldName() { - $label_field_name = $this->entity->getEntityType()->getKey('label'); - // @todo Remove this work-around after https://www.drupal.org/project/drupal/issues/2450793 lands. - if ($this->entity->getEntityTypeId() === 'user') { - $label_field_name = 'name'; - } - return $label_field_name; + $public_label_field_name = $this->resourceType->getPublicName($this->getLabelFieldName()); + return array_intersect_key($fields, [$public_label_field_name => TRUE]); } } diff --git a/src/Normalizer/ConfigEntityDenormalizer.php b/src/Normalizer/ConfigEntityDenormalizer.php index c54ff87..d53f8b1 100644 --- a/src/Normalizer/ConfigEntityDenormalizer.php +++ b/src/Normalizer/ConfigEntityDenormalizer.php @@ -13,9 +13,7 @@ use Drupal\jsonapi\ResourceType\ResourceType; final class ConfigEntityDenormalizer extends EntityDenormalizerBase { /** - * The interface or class that this Normalizer supports. - * - * @var string + * {@inheritdoc} */ protected $supportedInterfaceOrClass = ConfigEntityInterface::class; diff --git a/src/Normalizer/ContentEntityDenormalizer.php b/src/Normalizer/ContentEntityDenormalizer.php index 65209a1..5c5157a 100644 --- a/src/Normalizer/ContentEntityDenormalizer.php +++ b/src/Normalizer/ContentEntityDenormalizer.php @@ -14,9 +14,7 @@ use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; final class ContentEntityDenormalizer extends EntityDenormalizerBase { /** - * The interface or class that this Normalizer supports. - * - * @var string + * {@inheritdoc} */ protected $supportedInterfaceOrClass = ContentEntityInterface::class; @@ -61,7 +59,7 @@ final class ContentEntityDenormalizer extends EntityDenormalizerBase { 'The attribute %s does not exist on the %s resource type.', $internal_name, $resource_type->getTypeName() - )); + )); } $field_type = $field_map[$internal_name]['type']; diff --git a/src/Normalizer/EntityDenormalizerBase.php b/src/Normalizer/EntityDenormalizerBase.php index 5323422..af84b9a 100644 --- a/src/Normalizer/EntityDenormalizerBase.php +++ b/src/Normalizer/EntityDenormalizerBase.php @@ -17,13 +17,6 @@ use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; abstract class EntityDenormalizerBase extends NormalizerBase implements DenormalizerInterface { /** - * The formats that the Normalizer can handle. - * - * @var array - */ - protected $formats = ['api_json']; - - /** * The JSON:API resource type repository. * * @var \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface @@ -52,7 +45,7 @@ abstract class EntityDenormalizerBase extends NormalizerBase implements Denormal protected $pluginManager; /** - * Constructs an EntityNormalizer object. + * Constructs an EntityDenormalizerBase object. * * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager. diff --git a/src/Normalizer/Relationship.php b/src/Normalizer/Relationship.php index 4895af0..0def02c 100644 --- a/src/Normalizer/Relationship.php +++ b/src/Normalizer/Relationship.php @@ -31,9 +31,9 @@ class Relationship implements AccessibleInterface, CacheableDependencyInterface protected $cardinality; /** - * The entity that holds the relationship. + * The resource object that holds the relationship. * - * @var \Drupal\Core\Entity\EntityInterface + * @var \Drupal\jsonapi\JsonApiResource\ResourceObject */ protected $hostResourceObject; @@ -99,7 +99,7 @@ class Relationship implements AccessibleInterface, CacheableDependencyInterface } /** - * Gets the host entity. + * Gets the host resource object. * * @return \Drupal\jsonapi\JsonApiResource\ResourceObject * The resource object which contains this relationship. diff --git a/src/Normalizer/RelationshipItem.php b/src/Normalizer/RelationshipItem.php index b632575..b348a5a 100644 --- a/src/Normalizer/RelationshipItem.php +++ b/src/Normalizer/RelationshipItem.php @@ -20,7 +20,7 @@ class RelationshipItem { protected $targetKey = 'target_id'; /** - * The target entity. + * The target resource object. * * @var \Drupal\jsonapi\JsonApiResource\ResourceObject|null|false */ @@ -94,7 +94,7 @@ class RelationshipItem { } /** - * Gets the target entity. + * Gets the target resource object. * * @return \Drupal\jsonapi\JsonApiResource\ResourceObject|null|false * The target entity of this relationship item. @@ -116,7 +116,7 @@ class RelationshipItem { /** * Gets the relationship value. * - * Defaults to the entity ID. + * Defaults to the resource object ID. * * @return array * The value of this relationship item. diff --git a/src/Normalizer/ResourceObjectNormalizer.php b/src/Normalizer/ResourceObjectNormalizer.php index 7352d81..6a2f566 100644 --- a/src/Normalizer/ResourceObjectNormalizer.php +++ b/src/Normalizer/ResourceObjectNormalizer.php @@ -26,11 +26,6 @@ class ResourceObjectNormalizer extends NormalizerBase { ]; /** - * {@inheritdoc} - */ - protected $formats = ['api_json']; - - /** * The link manager. * * @var \Drupal\jsonapi\LinkManager\LinkManager @@ -38,7 +33,7 @@ class ResourceObjectNormalizer extends NormalizerBase { protected $linkManager; /** - * Constructs an EntityNormalizer object. + * Constructs an ResourceObjectNormalizer object. * * @param \Drupal\jsonapi\LinkManager\LinkManager $link_manager * The link manager. @@ -63,16 +58,17 @@ class ResourceObjectNormalizer extends NormalizerBase { $context['resource_object'] = $object; $resource_type = $object->getResourceType(); $resource_type_name = $resource_type->getTypeName(); + $fields = $object->getFields(); // Get the bundle ID of the requested resource. This is used to determine if // this is a bundle level resource or an entity level resource. if (!empty($context['sparse_fieldset'][$resource_type_name])) { $field_names = $context['sparse_fieldset'][$resource_type_name]; } else { - $field_names = $this->getFieldNames($object); + $field_names = array_keys($fields); } $normalizer_values = []; - foreach ($this->getFields($object) as $field_name => $field) { + foreach ($fields as $field_name => $field) { $in_sparse_fieldset = in_array($field_name, $field_names); // Omit fields not listed in sparse fieldsets. if (!$in_sparse_fieldset) { @@ -100,33 +96,6 @@ class ResourceObjectNormalizer extends NormalizerBase { } /** - * Extracts a resource object's fields. - * - * @param \Drupal\jsonapi\JsonApiResource\ResourceObject $object - * The resource object from which to extract fields. - * - * @return \Drupal\Core\Field\FieldItemListInterface[]|mixed - * The extracted fields. - */ - protected function getFields(ResourceObject $object) { - return $object->getFields(); - } - - /** - * Gets the field names for the given entity. - * - * @param \Drupal\jsonapi\JsonApiResource\ResourceObject $object - * The resource object. - * - * @return string[] - * The field names. - */ - protected function getFieldNames(ResourceObject $object) { - /* @var \Drupal\Core\Entity\ContentEntityInterface $entity */ - return array_keys($object->getFields()); - } - - /** * Serializes a given field. * * @param mixed $field @@ -140,9 +109,9 @@ class ResourceObjectNormalizer extends NormalizerBase { * The normalized value. */ protected function serializeField($field, array $context, $format) { - // Only content entities return FieldItemListInterface fields from - // ::getFields(). Since config entities do not have "real" fields and - // therefore do not have field access restrictions. + // Only content entities contain FieldItemListInterface fields. Since config + // entities do not have "real" fields and therefore do not have field access + // restrictions. if ($field instanceof FieldItemListInterface) { $field_access_result = $field->access('view', $context['account'], TRUE); if (!$field_access_result->isAllowed()) { diff --git a/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php b/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php index 482b8ce..dec157e 100644 --- a/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php +++ b/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php @@ -218,7 +218,6 @@ class JsonApiDocumentTopLevelNormalizerTest extends JsonapiKernelTestBase { list($request, $resource_type) = $this->generateProphecies('node', 'article'); $resource_object = new ResourceObject($resource_type, $this->node); - $includes = $this->includeResolver->resolve($resource_type, $resource_object, 'uid,field_tags,field_image'); $jsonapi_doc_object = $this