annotate core/modules/jsonapi/src/JsonApiResource/LabelOnlyResourceObject.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@18 1 <?php
Chris@18 2
Chris@18 3 namespace Drupal\jsonapi\JsonApiResource;
Chris@18 4
Chris@18 5 use Drupal\Core\Entity\EntityInterface;
Chris@18 6 use Drupal\Core\Entity\RevisionableInterface;
Chris@18 7 use Drupal\jsonapi\ResourceType\ResourceType;
Chris@18 8
Chris@18 9 /**
Chris@18 10 * Value object decorating a ResourceObject; only its label is available.
Chris@18 11 *
Chris@18 12 * @internal JSON:API maintains no PHP API. The API is the HTTP API. This class
Chris@18 13 * may change at any time and could break any dependencies on it.
Chris@18 14 *
Chris@18 15 * @see https://www.drupal.org/project/jsonapi/issues/3032787
Chris@18 16 * @see jsonapi.api.php
Chris@18 17 */
Chris@18 18 final class LabelOnlyResourceObject extends ResourceObject {
Chris@18 19
Chris@18 20 /**
Chris@18 21 * The entity represented by this resource object.
Chris@18 22 *
Chris@18 23 * @var \Drupal\Core\Entity\EntityInterface
Chris@18 24 */
Chris@18 25 protected $entity;
Chris@18 26
Chris@18 27 /**
Chris@18 28 * {@inheritdoc}
Chris@18 29 */
Chris@18 30 public static function createFromEntity(ResourceType $resource_type, EntityInterface $entity, LinkCollection $links = NULL) {
Chris@18 31 $resource_object = new static(
Chris@18 32 $entity,
Chris@18 33 $resource_type,
Chris@18 34 $entity->uuid(),
Chris@18 35 $resource_type->isVersionable() && $entity instanceof RevisionableInterface ? $entity->getRevisionId() : NULL,
Chris@18 36 static::extractFieldsFromEntity($resource_type, $entity),
Chris@18 37 static::buildLinksFromEntity($resource_type, $entity, $links ?: new LinkCollection([]))
Chris@18 38 );
Chris@18 39 $resource_object->setEntity($entity);
Chris@18 40 return $resource_object;
Chris@18 41 }
Chris@18 42
Chris@18 43 /**
Chris@18 44 * Gets the decorated entity.
Chris@18 45 *
Chris@18 46 * @return \Drupal\Core\Entity\EntityInterface
Chris@18 47 * The label for which to only normalize its label.
Chris@18 48 */
Chris@18 49 public function getEntity() {
Chris@18 50 return $this->entity;
Chris@18 51 }
Chris@18 52
Chris@18 53 /**
Chris@18 54 * Sets the underlying entity.
Chris@18 55 *
Chris@18 56 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@18 57 * An entity.
Chris@18 58 */
Chris@18 59 protected function setEntity(EntityInterface $entity) {
Chris@18 60 $this->entity = $entity;
Chris@18 61 }
Chris@18 62
Chris@18 63 /**
Chris@18 64 * {@inheritdoc}
Chris@18 65 */
Chris@18 66 protected static function extractFieldsFromEntity(ResourceType $resource_type, EntityInterface $entity) {
Chris@18 67 $fields = parent::extractFieldsFromEntity($resource_type, $entity);
Chris@18 68 $public_label_field_name = $resource_type->getPublicName(static::getLabelFieldName($entity));
Chris@18 69 return array_intersect_key($fields, [$public_label_field_name => TRUE]);
Chris@18 70 }
Chris@18 71
Chris@18 72 }