Loading core/modules/jsonapi/src/ResourceType/ResourceType.php +21 −6 Original line number Diff line number Diff line Loading @@ -18,6 +18,13 @@ */ class ResourceType { /** * A string which is used as path separator in resource type names. * * @see \Drupal\jsonapi\ResourceType\ResourceType::getPath() */ const TYPE_NAME_URI_PATH_SEPARATOR = '--'; /** * The entity type ID. * Loading Loading @@ -338,8 +345,10 @@ public function isVersionable() { * (optional) Whether the resource type is versionable. * @param \Drupal\jsonapi\ResourceType\ResourceTypeField[] $fields * (optional) The resource type fields, keyed by internal field name. * @param null|string $type_name * The resource type name. */ public function __construct($entity_type_id, $bundle, $deserialization_target_class, $internal = FALSE, $is_locatable = TRUE, $is_mutable = TRUE, $is_versionable = FALSE, array $fields = []) { public function __construct($entity_type_id, $bundle, $deserialization_target_class, $internal = FALSE, $is_locatable = TRUE, $is_mutable = TRUE, $is_versionable = FALSE, array $fields = [], $type_name = NULL) { $this->entityTypeId = $entity_type_id; $this->bundle = $bundle; $this->deserializationTargetClass = $deserialization_target_class; Loading @@ -349,9 +358,12 @@ public function __construct($entity_type_id, $bundle, $deserialization_target_cl $this->isVersionable = $is_versionable; $this->fields = $fields; $this->typeName = $type_name; if ($type_name === NULL) { $this->typeName = $this->bundle === '?' ? 'unknown' : sprintf('%s--%s', $this->entityTypeId, $this->bundle); : $this->entityTypeId . self::TYPE_NAME_URI_PATH_SEPARATOR . $this->bundle; } $this->fieldMapping = array_flip(array_map(function (ResourceTypeField $field) { return $field->getPublicName(); Loading Loading @@ -420,12 +432,15 @@ public function getRelatableResourceTypesByField($field_name) { * Get the resource path. * * @return string * The path to access this resource type. Default: /entity_type_id/bundle. * The path to access this resource type. The function * replaces "--" with "/" in the URI path. * Example: "node--article" -> "node/article". * * @see \Drupal\jsonapi\ResourceType\ResourceType::TYPE_NAME_URI_PATH_SEPARATOR * @see jsonapi.base_path */ public function getPath() { return sprintf('/%s/%s', $this->getEntityTypeId(), $this->getBundle()); return '/' . implode('/', explode(self::TYPE_NAME_URI_PATH_SEPARATOR, $this->typeName)); } } core/modules/jsonapi/src/ResourceType/ResourceTypeBuildEvent.php +14 −2 Original line number Diff line number Diff line Loading @@ -17,7 +17,7 @@ class ResourceTypeBuildEvent extends Event { /** * The JSON:API resource type name of the instance to be built. * * @var string * @var null|string */ protected $resourceTypeName; Loading Loading @@ -67,7 +67,7 @@ protected function __construct($resource_type_name, array $fields) { * A new event. */ public static function createFromEntityTypeAndBundle(EntityTypeInterface $entity_type, $bundle, array $fields) { return new static(sprintf('%s--%s', $entity_type->id(), $bundle), $fields); return new static($entity_type->id() . ResourceType::TYPE_NAME_URI_PATH_SEPARATOR . $bundle, $fields); } /** Loading @@ -80,6 +80,18 @@ public function getResourceTypeName() { return $this->resourceTypeName; } /** * Sets the name of the resource type to be built. * * @param string $resource_type_name * The resource type name. Also used to build the resource path. * * @see \Drupal\jsonapi\ResourceType\ResourceType::getPath() */ public function setResourceTypeName(string $resource_type_name): void { $this->resourceTypeName = $resource_type_name; } /** * Disables the resource type to be built. */ Loading core/modules/jsonapi/src/ResourceType/ResourceTypeRepository.php +17 −4 Original line number Diff line number Diff line Loading @@ -150,6 +150,7 @@ public function all() { * A JSON:API resource type. */ protected function createResourceType(EntityTypeInterface $entity_type, $bundle) { $type_name = NULL; $raw_fields = $this->getAllFieldNames($entity_type, $bundle); $internalize_resource_type = $entity_type->isInternal(); $fields = static::getFields($raw_fields, $entity_type, $bundle); Loading @@ -158,6 +159,7 @@ protected function createResourceType(EntityTypeInterface $entity_type, $bundle) $this->eventDispatcher->dispatch($event, ResourceTypeBuildEvents::BUILD); $internalize_resource_type = $event->resourceTypeShouldBeDisabled(); $fields = $event->getFields(); $type_name = $event->getResourceTypeName(); } return new ResourceType( $entity_type->id(), Loading @@ -167,7 +169,8 @@ protected function createResourceType(EntityTypeInterface $entity_type, $bundle) static::isLocatableResourceType($entity_type, $bundle), static::isMutableResourceType($entity_type, $bundle), static::isVersionableResourceType($entity_type), $fields $fields, $type_name ); } Loading @@ -180,7 +183,17 @@ public function get($entity_type_id, $bundle) { throw new PreconditionFailedHttpException('Server error. The current route is malformed.'); } return static::lookupResourceType($this->all(), $entity_type_id, $bundle); $map_id = sprintf('jsonapi.resource_type.%s.%s', $entity_type_id, $bundle); $cached = $this->cache->get($map_id); if ($cached) { return $cached->data; } $resource_type = static::lookupResourceType($this->all(), $entity_type_id, $bundle); $this->cache->set($map_id, $resource_type, Cache::PERMANENT, $this->cacheTags); return $resource_type; } /** Loading Loading @@ -508,8 +521,8 @@ protected function getAllBundlesForEntityType($entity_type_id) { * The resource type or NULL if one cannot be found. */ protected static function lookupResourceType(array $resource_types, $entity_type_id, $bundle) { if (isset($resource_types["$entity_type_id--$bundle"])) { return $resource_types["$entity_type_id--$bundle"]; if (isset($resource_types[$entity_type_id . ResourceType::TYPE_NAME_URI_PATH_SEPARATOR . $bundle])) { return $resource_types[$entity_type_id . ResourceType::TYPE_NAME_URI_PATH_SEPARATOR . $bundle]; } foreach ($resource_types as $resource_type) { Loading core/modules/jsonapi/tests/modules/jsonapi_test_collection_count/src/ResourceType/CountableResourceTypeRepository.php +10 −9 Original line number Diff line number Diff line Loading @@ -14,16 +14,17 @@ class CountableResourceTypeRepository extends ResourceTypeRepository { * {@inheritdoc} */ protected function createResourceType(EntityTypeInterface $entity_type, $bundle) { $raw_fields = $this->getAllFieldNames($entity_type, $bundle); $resource_type = parent::createResourceType($entity_type, $bundle); return new CountableResourceType( $entity_type->id(), $bundle, $entity_type->getClass(), $entity_type->isInternal(), static::isLocatableResourceType($entity_type, $bundle), static::isMutableResourceType($entity_type, $bundle), static::isVersionableResourceType($entity_type), static::getFields($raw_fields, $entity_type, $bundle) $resource_type->getEntityTypeId(), $resource_type->getBundle(), $resource_type->getDeserializationTargetClass(), $resource_type->isInternal(), $resource_type->isLocatable(), $resource_type->isMutable(), $resource_type->isVersionable(), $resource_type->getFields(), $resource_type->getTypeName() ); } Loading core/modules/jsonapi/tests/modules/jsonapi_test_resource_type_aliasing/jsonapi_test_resource_type_aliasing.info.ymldeleted 100644 → 0 +0 −3 Original line number Diff line number Diff line name: 'JSON:API test resource type aliasing' type: module package: Testing Loading
core/modules/jsonapi/src/ResourceType/ResourceType.php +21 −6 Original line number Diff line number Diff line Loading @@ -18,6 +18,13 @@ */ class ResourceType { /** * A string which is used as path separator in resource type names. * * @see \Drupal\jsonapi\ResourceType\ResourceType::getPath() */ const TYPE_NAME_URI_PATH_SEPARATOR = '--'; /** * The entity type ID. * Loading Loading @@ -338,8 +345,10 @@ public function isVersionable() { * (optional) Whether the resource type is versionable. * @param \Drupal\jsonapi\ResourceType\ResourceTypeField[] $fields * (optional) The resource type fields, keyed by internal field name. * @param null|string $type_name * The resource type name. */ public function __construct($entity_type_id, $bundle, $deserialization_target_class, $internal = FALSE, $is_locatable = TRUE, $is_mutable = TRUE, $is_versionable = FALSE, array $fields = []) { public function __construct($entity_type_id, $bundle, $deserialization_target_class, $internal = FALSE, $is_locatable = TRUE, $is_mutable = TRUE, $is_versionable = FALSE, array $fields = [], $type_name = NULL) { $this->entityTypeId = $entity_type_id; $this->bundle = $bundle; $this->deserializationTargetClass = $deserialization_target_class; Loading @@ -349,9 +358,12 @@ public function __construct($entity_type_id, $bundle, $deserialization_target_cl $this->isVersionable = $is_versionable; $this->fields = $fields; $this->typeName = $type_name; if ($type_name === NULL) { $this->typeName = $this->bundle === '?' ? 'unknown' : sprintf('%s--%s', $this->entityTypeId, $this->bundle); : $this->entityTypeId . self::TYPE_NAME_URI_PATH_SEPARATOR . $this->bundle; } $this->fieldMapping = array_flip(array_map(function (ResourceTypeField $field) { return $field->getPublicName(); Loading Loading @@ -420,12 +432,15 @@ public function getRelatableResourceTypesByField($field_name) { * Get the resource path. * * @return string * The path to access this resource type. Default: /entity_type_id/bundle. * The path to access this resource type. The function * replaces "--" with "/" in the URI path. * Example: "node--article" -> "node/article". * * @see \Drupal\jsonapi\ResourceType\ResourceType::TYPE_NAME_URI_PATH_SEPARATOR * @see jsonapi.base_path */ public function getPath() { return sprintf('/%s/%s', $this->getEntityTypeId(), $this->getBundle()); return '/' . implode('/', explode(self::TYPE_NAME_URI_PATH_SEPARATOR, $this->typeName)); } }
core/modules/jsonapi/src/ResourceType/ResourceTypeBuildEvent.php +14 −2 Original line number Diff line number Diff line Loading @@ -17,7 +17,7 @@ class ResourceTypeBuildEvent extends Event { /** * The JSON:API resource type name of the instance to be built. * * @var string * @var null|string */ protected $resourceTypeName; Loading Loading @@ -67,7 +67,7 @@ protected function __construct($resource_type_name, array $fields) { * A new event. */ public static function createFromEntityTypeAndBundle(EntityTypeInterface $entity_type, $bundle, array $fields) { return new static(sprintf('%s--%s', $entity_type->id(), $bundle), $fields); return new static($entity_type->id() . ResourceType::TYPE_NAME_URI_PATH_SEPARATOR . $bundle, $fields); } /** Loading @@ -80,6 +80,18 @@ public function getResourceTypeName() { return $this->resourceTypeName; } /** * Sets the name of the resource type to be built. * * @param string $resource_type_name * The resource type name. Also used to build the resource path. * * @see \Drupal\jsonapi\ResourceType\ResourceType::getPath() */ public function setResourceTypeName(string $resource_type_name): void { $this->resourceTypeName = $resource_type_name; } /** * Disables the resource type to be built. */ Loading
core/modules/jsonapi/src/ResourceType/ResourceTypeRepository.php +17 −4 Original line number Diff line number Diff line Loading @@ -150,6 +150,7 @@ public function all() { * A JSON:API resource type. */ protected function createResourceType(EntityTypeInterface $entity_type, $bundle) { $type_name = NULL; $raw_fields = $this->getAllFieldNames($entity_type, $bundle); $internalize_resource_type = $entity_type->isInternal(); $fields = static::getFields($raw_fields, $entity_type, $bundle); Loading @@ -158,6 +159,7 @@ protected function createResourceType(EntityTypeInterface $entity_type, $bundle) $this->eventDispatcher->dispatch($event, ResourceTypeBuildEvents::BUILD); $internalize_resource_type = $event->resourceTypeShouldBeDisabled(); $fields = $event->getFields(); $type_name = $event->getResourceTypeName(); } return new ResourceType( $entity_type->id(), Loading @@ -167,7 +169,8 @@ protected function createResourceType(EntityTypeInterface $entity_type, $bundle) static::isLocatableResourceType($entity_type, $bundle), static::isMutableResourceType($entity_type, $bundle), static::isVersionableResourceType($entity_type), $fields $fields, $type_name ); } Loading @@ -180,7 +183,17 @@ public function get($entity_type_id, $bundle) { throw new PreconditionFailedHttpException('Server error. The current route is malformed.'); } return static::lookupResourceType($this->all(), $entity_type_id, $bundle); $map_id = sprintf('jsonapi.resource_type.%s.%s', $entity_type_id, $bundle); $cached = $this->cache->get($map_id); if ($cached) { return $cached->data; } $resource_type = static::lookupResourceType($this->all(), $entity_type_id, $bundle); $this->cache->set($map_id, $resource_type, Cache::PERMANENT, $this->cacheTags); return $resource_type; } /** Loading Loading @@ -508,8 +521,8 @@ protected function getAllBundlesForEntityType($entity_type_id) { * The resource type or NULL if one cannot be found. */ protected static function lookupResourceType(array $resource_types, $entity_type_id, $bundle) { if (isset($resource_types["$entity_type_id--$bundle"])) { return $resource_types["$entity_type_id--$bundle"]; if (isset($resource_types[$entity_type_id . ResourceType::TYPE_NAME_URI_PATH_SEPARATOR . $bundle])) { return $resource_types[$entity_type_id . ResourceType::TYPE_NAME_URI_PATH_SEPARATOR . $bundle]; } foreach ($resource_types as $resource_type) { Loading
core/modules/jsonapi/tests/modules/jsonapi_test_collection_count/src/ResourceType/CountableResourceTypeRepository.php +10 −9 Original line number Diff line number Diff line Loading @@ -14,16 +14,17 @@ class CountableResourceTypeRepository extends ResourceTypeRepository { * {@inheritdoc} */ protected function createResourceType(EntityTypeInterface $entity_type, $bundle) { $raw_fields = $this->getAllFieldNames($entity_type, $bundle); $resource_type = parent::createResourceType($entity_type, $bundle); return new CountableResourceType( $entity_type->id(), $bundle, $entity_type->getClass(), $entity_type->isInternal(), static::isLocatableResourceType($entity_type, $bundle), static::isMutableResourceType($entity_type, $bundle), static::isVersionableResourceType($entity_type), static::getFields($raw_fields, $entity_type, $bundle) $resource_type->getEntityTypeId(), $resource_type->getBundle(), $resource_type->getDeserializationTargetClass(), $resource_type->isInternal(), $resource_type->isLocatable(), $resource_type->isMutable(), $resource_type->isVersionable(), $resource_type->getFields(), $resource_type->getTypeName() ); } Loading
core/modules/jsonapi/tests/modules/jsonapi_test_resource_type_aliasing/jsonapi_test_resource_type_aliasing.info.ymldeleted 100644 → 0 +0 −3 Original line number Diff line number Diff line name: 'JSON:API test resource type aliasing' type: module package: Testing