comparison core/modules/jsonapi/src/JsonApiResource/LabelOnlyResourceObject.php @ 18:af1871eacc83

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