class ResourceType (View source)

internal  JSON:API maintains no PHP API since its API is the HTTP API. This class may change at any time and this will break any dependencies on it.
 

Value object containing all metadata for a JSON:API resource type.

Used to generate routes (collection, individual, etcetera), generate relationship links, and so on.

Constants

TYPE_NAME_URI_PATH_SEPARATOR

A string which is used as path separator in resource type names.

Properties

protected string $entityTypeId

The entity type ID.

protected string $bundle

The bundle ID.

protected string $typeName

The type name.

protected string $deserializationTargetClass

The class to which a payload converts to.

protected bool $internal

Whether this resource type is internal.

protected bool $isLocatable

Whether this resource type's resources are locatable.

protected bool $isMutable

Whether this resource type's resources are mutable.

protected bool $isVersionable

Whether this resource type's resources are versionable.

protected string[] $fields

The list of fields on the underlying entity type + bundle.

protected array $relatableResourceTypesByField

An array of arrays of relatable resource types, keyed by public field name.

protected string[] $fieldMapping

The mapping for field aliases: keys=public names, values=internal names.

Methods

string
getEntityTypeId()

Gets the entity type ID.

string
getTypeName()

Gets the type name.

string
getBundle()

Gets the bundle.

string
getDeserializationTargetClass()

Gets the deserialization target class.

string
getPublicName($field_name)

Translates the entity field name to the public field name.

string
getInternalName($field_name)

Translates the public field name to the entity field name.

getFields()

Gets the attribute and relationship fields of this resource type.

getFieldByPublicName(string $public_field_name)

Gets a particular attribute or relationship field by public field name.

getFieldByInternalName(string $internal_field_name)

Gets a particular attribute or relationship field by internal field name.

bool
hasField(string $field_name)

Checks if the field exists.

bool
isFieldEnabled(string $field_name)

Checks if a field is enabled or not.

bool
includeCount()

Determine whether to include a collection count.

bool
isInternal()

Whether this resource type is internal.

bool
isLocatable()

Whether resources of this resource type are locatable.

bool
isMutable()

Whether resources of this resource type are mutable.

bool
isVersionable()

Whether resources of this resource type are versionable.

__construct(string $entity_type_id, string $bundle, string $deserialization_target_class, bool $internal = FALSE, bool $is_locatable = TRUE, bool $is_mutable = TRUE, bool $is_versionable = FALSE, array $fields = [], null|string $type_name = NULL)

Instantiates a ResourceType object.

setRelatableResourceTypes(array $relatable_resource_types)

Sets the relatable resource types.

array
getRelatableResourceTypes()

Get all resource types with which this type may have a relationship.

getRelatableResourceTypesByField(string $field_name)

Get all resource types with which the given field may have a relationship.

string
getPath()

Get the resource path.

Details

string getEntityTypeId()

Gets the entity type ID.

Return Value

string

The entity type ID.

See also

EntityInterface::getEntityTypeId

string getTypeName()

Gets the type name.

Return Value

string

The type name.

string getBundle()

Gets the bundle.

Return Value

string

The bundle of the entity. Defaults to the entity type ID if the entity type does not make use of different bundles.

See also

EntityInterface::bundle

string getDeserializationTargetClass()

Gets the deserialization target class.

Return Value

string

The deserialization target class.

string getPublicName($field_name)

Translates the entity field name to the public field name.

This is only here so we can allow polymorphic implementations to take a greater control on the field names.

Parameters

$field_name

Return Value

string

The public field name.

string getInternalName($field_name)

Translates the public field name to the entity field name.

This is only here so we can allow polymorphic implementations to take a greater control on the field names.

Parameters

$field_name

Return Value

string

The internal field name as defined in the entity.

ResourceTypeField[] getFields()

Gets the attribute and relationship fields of this resource type.

Return Value

ResourceTypeField[]

The field objects on this resource type.

ResourceTypeField|null getFieldByPublicName(string $public_field_name)

Gets a particular attribute or relationship field by public field name.

Parameters

string $public_field_name

The public field name of the desired field.

Return Value

ResourceTypeField|null

A resource type field object or NULL if the field does not exist on this resource type.

ResourceTypeField|null getFieldByInternalName(string $internal_field_name)

Gets a particular attribute or relationship field by internal field name.

Parameters

string $internal_field_name

The internal field name of the desired field.

Return Value

ResourceTypeField|null

A resource type field object or NULL if the field does not exist on this resource type.

bool hasField(string $field_name)

Checks if the field exists.

Note: a minority of config entity types which do not define a config_export in their entity type annotation will not have their fields represented here because it is impossible to determine them without an instance of config available.

Refactor this in Drupal 9, because thanks to https://www.drupal.org/project/drupal/issues/2949021, `config_export` will be guaranteed to exist, and this won't need an instance anymore.

Parameters

string $field_name

The internal field name.

Return Value

bool

TRUE if the field is known to exist on the resource type; FALSE otherwise.

bool isFieldEnabled(string $field_name)

Checks if a field is enabled or not.

This is only here so we can allow polymorphic implementations to take a greater control on the data model.

Parameters

string $field_name

The internal field name.

Return Value

bool

TRUE if the field exists and is enabled and should be considered as part of the data model. FALSE otherwise.

bool includeCount()

Determine whether to include a collection count.

Return Value

bool

Whether to include a collection count.

bool isInternal()

Whether this resource type is internal.

This must not be used as an access control mechanism.

Internal resource types are not available via the HTTP API. They have no routes and cannot be used for filtering or sorting. They cannot be included in the response using the include query parameter.

However, relationship fields on public resources will include a resource identifier for the referenced internal resource.

This method exists to remove data that should not logically be exposed by the HTTP API. For example, read-only data from an internal resource might be embedded in a public resource using computed fields. Therefore, including the internal resource as a relationship with distinct routes might unnecessarily expose internal implementation details.

Return Value

bool

TRUE if the resource type is internal. FALSE otherwise.

bool isLocatable()

Whether resources of this resource type are locatable.

A resource type may for example not be locatable when it is not stored.

Return Value

bool

TRUE if the resource type's resources are locatable. FALSE otherwise.

bool isMutable()

Whether resources of this resource type are mutable.

Indicates that resources of this type may not be created, updated or deleted (POST, PATCH or DELETE, respectively).

Return Value

bool

TRUE if the resource type's resources are mutable. FALSE otherwise.

bool isVersionable()

Whether resources of this resource type are versionable.

Return Value

bool

TRUE if the resource type's resources are versionable. FALSE otherwise.

__construct(string $entity_type_id, string $bundle, string $deserialization_target_class, bool $internal = FALSE, bool $is_locatable = TRUE, bool $is_mutable = TRUE, bool $is_versionable = FALSE, array $fields = [], null|string $type_name = NULL)

Instantiates a ResourceType object.

Parameters

string $entity_type_id

An entity type ID.

string $bundle

A bundle.

string $deserialization_target_class

The deserialization target class.

bool $internal

(optional) Whether the resource type should be internal.

bool $is_locatable

(optional) Whether the resource type is locatable.

bool $is_mutable

(optional) Whether the resource type is mutable.

bool $is_versionable

(optional) Whether the resource type is versionable.

array $fields

(optional) The resource type fields, keyed by internal field name.

null|string $type_name

The resource type name.

setRelatableResourceTypes(array $relatable_resource_types)

Sets the relatable resource types.

Parameters

array $relatable_resource_types

The resource types with which this resource type may have a relationship. The array should be a multi-dimensional array keyed by public field name whose values are an array of resource types. There may be duplicate across resource types across fields, but not within a field.

array getRelatableResourceTypes()

Get all resource types with which this type may have a relationship.

Return Value

array

The relatable resource types, keyed by relationship field names.

See also

\Drupal\jsonapi\ResourceType\self::setRelatableResourceTypes()

ResourceType[] getRelatableResourceTypesByField(string $field_name)

Get all resource types with which the given field may have a relationship.

Parameters

string $field_name

The public field name.

Return Value

ResourceType[]

The relatable JSON:API resource types.

See also

\Drupal\jsonapi\ResourceType\self::getRelatableResourceTypes()

string getPath()

Get the resource path.

Return Value

string

The path to access this resource type. The function replaces "--" with "/" in the URI path. Example: "node--article" -> "node/article".

See also

ResourceType::TYPE_NAME_URI_PATH_SEPARATOR
jsonapi.base_path