class LabelOnlyResourceObject

Same name and namespace in other branches
  1. 10 core/modules/jsonapi/src/JsonApiResource/LabelOnlyResourceObject.php \Drupal\jsonapi\JsonApiResource\LabelOnlyResourceObject
  2. 11.x core/modules/jsonapi/src/JsonApiResource/LabelOnlyResourceObject.php \Drupal\jsonapi\JsonApiResource\LabelOnlyResourceObject
  3. 9 core/modules/jsonapi/src/JsonApiResource/LabelOnlyResourceObject.php \Drupal\jsonapi\JsonApiResource\LabelOnlyResourceObject
  4. 8.9.x core/modules/jsonapi/src/JsonApiResource/LabelOnlyResourceObject.php \Drupal\jsonapi\JsonApiResource\LabelOnlyResourceObject

Value object decorating a ResourceObject; only its label is available.

@internal JSON:API maintains no PHP API. The API is the HTTP API. This class may change at any time and could break any dependencies on it.

Hierarchy

  • class \Drupal\jsonapi\JsonApiResource\LabelOnlyResourceObject extends \Drupal\jsonapi\JsonApiResource\ResourceObject

Expanded class hierarchy of LabelOnlyResourceObject

See also

https://www.drupal.org/project/drupal/issues/3032787

jsonapi.api.php

3 files declare their use of LabelOnlyResourceObject
EntityAccessChecker.php in core/modules/jsonapi/src/Access/EntityAccessChecker.php
IncludeResolver.php in core/modules/jsonapi/src/IncludeResolver.php
JsonApiTopLevelResourceNormalizerTest.php in core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiTopLevelResourceNormalizerTest.php

File

core/modules/jsonapi/src/JsonApiResource/LabelOnlyResourceObject.php, line 18

Namespace

Drupal\jsonapi\JsonApiResource
View source
final class LabelOnlyResourceObject extends ResourceObject {
  
  /**
   * The entity represented by this resource object.
   *
   * @var \Drupal\Core\Entity\EntityInterface
   */
  protected $entity;
  
  /**
   * {@inheritdoc}
   */
  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;
  }
  
  /**
   * Gets the decorated entity.
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *   The label for which to only normalize its label.
   */
  public function getEntity() {
    return $this->entity;
  }
  
  /**
   * Sets the underlying entity.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   An entity.
   */
  protected function setEntity(EntityInterface $entity) {
    $this->entity = $entity;
  }
  
  /**
   * {@inheritdoc}
   */
  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,
    ]);
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
CacheableDependencyTrait::$cacheContexts protected property Cache contexts.
CacheableDependencyTrait::$cacheMaxAge protected property Cache max-age.
CacheableDependencyTrait::$cacheTags protected property Cache tags.
CacheableDependencyTrait::getCacheContexts public function 4
CacheableDependencyTrait::getCacheMaxAge public function 4
CacheableDependencyTrait::getCacheTags public function 4
CacheableDependencyTrait::setCacheability protected function Sets cacheability; useful for value object constructors.
LabelOnlyResourceObject::$entity protected property The entity represented by this resource object.
LabelOnlyResourceObject::createFromEntity public static function Overrides ResourceObject::createFromEntity
LabelOnlyResourceObject::extractFieldsFromEntity protected static function Overrides ResourceObject::extractFieldsFromEntity
LabelOnlyResourceObject::getEntity public function Gets the decorated entity.
LabelOnlyResourceObject::setEntity protected function Sets the underlying entity.
ResourceIdentifierTrait::$resourceIdentifier protected property A ResourceIdentifier object.
ResourceIdentifierTrait::$resourceType protected property The JSON:API resource type of the identified resource object.
ResourceIdentifierTrait::getId public function
ResourceIdentifierTrait::getResourceType public function
ResourceIdentifierTrait::getTypeName public function
ResourceObject::$fields protected property The object's fields.
ResourceObject::$language protected property The resource language.
ResourceObject::$links protected property The resource object's links.
ResourceObject::$versionIdentifier protected property The resource object's version identifier.
ResourceObject::buildLinksFromEntity protected static function Builds a LinkCollection for the given entity.
ResourceObject::extractConfigEntityFields protected static function Extracts a config entity's fields.
ResourceObject::extractContentEntityFields protected static function Extracts a content entity's fields.
ResourceObject::getField public function Gets the given field.
ResourceObject::getFields public function Gets the ResourceObject's fields.
ResourceObject::getLabelFieldName protected static function Determines the entity type's (internal) label field name.
ResourceObject::getLanguage public function Gets the ResourceObject's language.
ResourceObject::getLinks public function Gets the ResourceObject's links.
ResourceObject::getVersionIdentifier public function Gets a version identifier for the ResourceObject.
ResourceObject::hasField public function Whether the resource object has the given field.
ResourceObject::toUrl public function Gets a Url for the ResourceObject.
ResourceObject::__construct public function ResourceObject constructor.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.