Unverified Commit fc51b4b1 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3105318 by bbrala, mglaman, ravi.shankar, Wim Leers, gabesullice,...

Issue #3105318 by bbrala, mglaman, ravi.shankar, Wim Leers, gabesullice, larowlan, bojanz, e0ipso: Add a public API for aliasing resource type names
parent 068f00c4
Loading
Loading
Loading
Loading
+21 −6
Original line number Diff line number Diff line
@@ -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.
   *
@@ -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;
@@ -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();
@@ -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));
  }

}
+14 −2
Original line number Diff line number Diff line
@@ -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;

@@ -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);
  }

  /**
@@ -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.
   */
+17 −4
Original line number Diff line number Diff line
@@ -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);
@@ -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(),
@@ -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
    );
  }

@@ -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;
  }

  /**
@@ -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) {
+10 −9
Original line number Diff line number Diff line
@@ -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()
    );
  }

+0 −3
Original line number Diff line number Diff line
name: 'JSON:API test resource type aliasing'
type: module
package: Testing
Loading