final class LabelOnlyResourceObject extends ResourceObject (View source)

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.
 

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

Traits

Trait for \Drupal\Core\Cache\CacheableDependencyInterface.

Used to associate an object like an exception to a particular resource.

Properties

protected string[] $cacheContexts

Cache contexts.

from  CacheableDependencyTrait
protected string[] $cacheTags

Cache tags.

from  CacheableDependencyTrait
protected int $cacheMaxAge

Cache max-age.

from  CacheableDependencyTrait
protected ResourceIdentifier $resourceIdentifier

A ResourceIdentifier object.

from  ResourceIdentifierTrait
protected ResourceType $resourceType

The JSON:API resource type of the identified resource object.

from  ResourceIdentifierTrait
protected string|null $versionIdentifier

The resource object's version identifier.

from  ResourceObject
protected FieldItemListInterface[]|array $fields

The object's fields.

from  ResourceObject
$links

The resource object's links.

from  ResourceObject
protected EntityInterface $entity

The entity represented by this resource object.

Methods

$this
setCacheability(CacheableDependencyInterface $cacheability)

Sets cacheability; useful for value object constructors.

getCacheTags()

{@inheritdoc}

getId()

{@inheritdoc}

getTypeName()

{@inheritdoc}

__construct(CacheableDependencyInterface $cacheability, ResourceType $resource_type, string $id, mixed|null $revision_id, array $fields, LinkCollection $links)

ResourceObject constructor.

static ResourceObject
createFromEntity(ResourceType $resource_type, EntityInterface $entity, LinkCollection $links = NULL)

Creates a new ResourceObject from an entity.

bool
hasField(string $public_field_name)

Whether the resource object has the given field.

getField(string $public_field_name)

Gets the given field.

array
getFields()

Gets the ResourceObject's fields.

getLinks()

Gets the ResourceObject's links.

string
getVersionIdentifier()

Gets a version identifier for the ResourceObject.

Url
toUrl()

Gets a Url for the ResourceObject.

static mixed|FieldItemListInterface[]
extractFieldsFromEntity(ResourceType $resource_type, EntityInterface $entity)

Extracts the entity's fields.

static LinkCollection
buildLinksFromEntity(ResourceType $resource_type, EntityInterface $entity, LinkCollection $links)

Builds a LinkCollection for the given entity.

extractContentEntityFields(ResourceType $resource_type, ContentEntityInterface $entity)

Extracts a content entity's fields.

static string
getLabelFieldName(EntityInterface $entity)

Determines the entity type's (internal) label field name.

static array
extractConfigEntityFields(ResourceType $resource_type, ConfigEntityInterface $entity)

Extracts a config entity's fields.

getEntity()

Gets the decorated entity.

setEntity(EntityInterface $entity)

Sets the underlying entity.

Details

protected $this setCacheability(CacheableDependencyInterface $cacheability)

Sets cacheability; useful for value object constructors.

Parameters

CacheableDependencyInterface $cacheability

The cacheability to set.

Return Value

$this

getCacheTags()

{@inheritdoc}

getCacheContexts()

{@inheritdoc}

getCacheMaxAge()

{@inheritdoc}

getId()

{@inheritdoc}

getTypeName()

{@inheritdoc}

getResourceType()

{@inheritdoc}

__construct(CacheableDependencyInterface $cacheability, ResourceType $resource_type, string $id, mixed|null $revision_id, array $fields, LinkCollection $links)

ResourceObject constructor.

Parameters

CacheableDependencyInterface $cacheability

The cacheability for the resource object.

ResourceType $resource_type

The JSON:API resource type of the resource object.

string $id

The resource object's ID.

mixed|null $revision_id

The resource object's version identifier. NULL, if the resource object is not versionable.

array $fields

An array of the resource object's fields, keyed by public field name.

LinkCollection $links

The links for the resource object.

static ResourceObject createFromEntity(ResourceType $resource_type, EntityInterface $entity, LinkCollection $links = NULL)

Creates a new ResourceObject from an entity.

Parameters

ResourceType $resource_type

The JSON:API resource type of the resource object.

EntityInterface $entity

The entity to be represented by this resource object.

LinkCollection $links

(optional) Any links for the resource object, if a self link is not provided, one will be automatically added if the resource is locatable and is not an internal entity.

Return Value

ResourceObject

An instantiated resource object.

bool hasField(string $public_field_name)

Whether the resource object has the given field.

Parameters

string $public_field_name

A public field name.

Return Value

bool

TRUE if the resource object has the given field, FALSE otherwise.

mixed|FieldItemListInterface|null getField(string $public_field_name)

Gets the given field.

Parameters

string $public_field_name

A public field name.

Return Value

mixed|FieldItemListInterface|null

The field or NULL if the resource object does not have the given field.

See also

\Drupal\jsonapi\JsonApiResource\::extractFields()

array getFields()

Gets the ResourceObject's fields.

Return Value

array

The resource object's fields, keyed by public field name.

See also

\Drupal\jsonapi\JsonApiResource\::extractFields()

Gets the ResourceObject's links.

Return Value

LinkCollection

The resource object's links.

string getVersionIdentifier()

Gets a version identifier for the ResourceObject.

Return Value

string

The version identifier of the resource object, if the resource type is versionable.

Url toUrl()

Gets a Url for the ResourceObject.

Return Value

Url

The URL for the identified resource object.

Exceptions

LogicException

See also

ResourceTypeRepository::isLocatableResourceType

static protected mixed|FieldItemListInterface[] extractFieldsFromEntity(ResourceType $resource_type, EntityInterface $entity)

Extracts the entity's fields.

Parameters

ResourceType $resource_type

The JSON:API resource type of the given entity.

EntityInterface $entity

The entity from which fields should be extracted.

Return Value

mixed|FieldItemListInterface[]

If the resource object represents a content entity, the fields will be objects satisfying FieldItemListInterface. If it represents a config entity, the fields will be scalar values or arrays.

static protected LinkCollection buildLinksFromEntity(ResourceType $resource_type, EntityInterface $entity, LinkCollection $links)

Builds a LinkCollection for the given entity.

Parameters

ResourceType $resource_type

The JSON:API resource type of the given entity.

EntityInterface $entity

The entity for which to build links.

LinkCollection $links

(optional) Any extra links for the resource object, if a self link is not provided, one will be automatically added if the resource is locatable and is not an internal entity.

Return Value

LinkCollection

The built links.

static protected FieldItemListInterface[] extractContentEntityFields(ResourceType $resource_type, ContentEntityInterface $entity)

Extracts a content entity's fields.

Parameters

ResourceType $resource_type

The JSON:API resource type of the given entity.

ContentEntityInterface $entity

The config entity from which fields should be extracted.

Return Value

FieldItemListInterface[]

The fields extracted from a content entity.

static protected string getLabelFieldName(EntityInterface $entity)

Determines the entity type's (internal) label field name.

Parameters

EntityInterface $entity

The entity from which fields should be extracted.

Return Value

string

The label field name.

static protected array extractConfigEntityFields(ResourceType $resource_type, ConfigEntityInterface $entity)

Extracts a config entity's fields.

Parameters

ResourceType $resource_type

The JSON:API resource type of the given entity.

ConfigEntityInterface $entity

The config entity from which fields should be extracted.

Return Value

array

The fields extracted from a config entity.

EntityInterface getEntity()

Gets the decorated entity.

Return Value

EntityInterface

The label for which to only normalize its label.

protected setEntity(EntityInterface $entity)

Sets the underlying entity.

Parameters

EntityInterface $entity

An entity.