LabelOnlyResourceObject.php in Drupal 10
File
core/modules/jsonapi/src/JsonApiResource/LabelOnlyResourceObject.php
View source
<?php
namespace Drupal\jsonapi\JsonApiResource;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\RevisionableInterface;
use Drupal\jsonapi\ResourceType\ResourceType;
final class LabelOnlyResourceObject extends ResourceObject {
protected $entity;
public static function createFromEntity(ResourceType $resource_type, EntityInterface $entity, LinkCollection $links = NULL) {
$resource_object = new static($entity, $resource_type, $entity
->uuid(), $resource_type
->isVersionable() && $entity instanceof RevisionableInterface ? $entity
->getRevisionId() : NULL, static::extractFieldsFromEntity($resource_type, $entity), static::buildLinksFromEntity($resource_type, $entity, $links ?: new LinkCollection([])));
$resource_object
->setEntity($entity);
return $resource_object;
}
public function getEntity() {
return $this->entity;
}
protected function setEntity(EntityInterface $entity) {
$this->entity = $entity;
}
protected static function extractFieldsFromEntity(ResourceType $resource_type, EntityInterface $entity) {
$fields = parent::extractFieldsFromEntity($resource_type, $entity);
$public_label_field_name = $resource_type
->getPublicName(static::getLabelFieldName($entity));
return array_intersect_key($fields, [
$public_label_field_name => TRUE,
]);
}
}