ResourceObjectData example


  protected function normalize(EntityInterface $entity, Url $url) {
    // Don't use cached normalizations in tests.     $this->container->get('cache.jsonapi_normalizations')->deleteAll();

    $self_link = new Link(new CacheableMetadata()$url, 'self');
    $resource_type = $this->container->get('jsonapi.resource_type.repository')->getByTypeName(static::$resourceTypeName);
    $doc = new JsonApiDocumentTopLevel(new ResourceObjectData([ResourceObject::createFromEntity($resource_type$entity)], 1)new NullIncludedData()new LinkCollection(['self' => $self_link]));
    return $this->serializer->normalize($doc, 'api_json', [
      'resource_type' => $resource_type,
      'account' => $this->account,
    ])->getNormalization();
  }

  /** * Creates the entity to be tested. * * @return \Drupal\Core\Entity\EntityInterface * The entity to be tested. */
->toString(TRUE);
        $meta['links']['me']['href'] = $me_url->getGeneratedUrl();
        // The cacheability of the `me` URL is the cacheability of that URL         // itself and the currently authenticated user.         $cacheability = $cacheability->merge($me_url);
      }
      catch (RouteNotFoundException $e) {
        // Do not add the link if the route is disabled or marked as internal.       }
    }

    $response = new CacheableResourceResponse(new JsonApiDocumentTopLevel(new ResourceObjectData([])new NullIncludedData()$urls$meta));
    return $response->addCacheableDependency($cacheability);
  }

}

  public function resolve($data$include_parameter) {
    assert($data instanceof ResourceObject || $data instanceof ResourceObjectData);
    $data = $data instanceof ResourceObjectData ? $data : new ResourceObjectData([$data], 1);
    $include_tree = static::toIncludeTree($data$include_parameter);
    return IncludedData::deduplicate($this->resolveIncludeTree($include_tree$data));
  }

  /** * Receives a tree of include field names and resolves resources for it. * * This method takes a tree of relationship field names and JSON:API Data * object. For the top-level of the tree and for each entity in the * collection, it gets the target entity type and IDs for each relationship * field. The method then loads all of those targets and calls itself * recursively with the next level of the tree and those loaded resources. * * @param array $include_tree * The include paths, represented as a tree. * @param \Drupal\jsonapi\JsonApiResource\Data $data * The entity collection from which includes should be resolved. * @param \Drupal\jsonapi\JsonApiResource\Data|null $includes * (Internal use only) Any prior resolved includes. * * @return \Drupal\jsonapi\JsonApiResource\Data * A JSON:API Data of included items. * * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException * Thrown if an included entity type doesn't exist. * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException * Thrown if a storage handler couldn't be loaded. */

  public function testNormalize() {
    $resource_type = $this->container->get('jsonapi.resource_type.repository')->get('node', 'article');

    $resource_object = ResourceObject::createFromEntity($resource_type$this->node);
    $includes = $this->includeResolver->resolve($resource_object, 'uid,field_tags,field_image');

    $jsonapi_doc_object = $this
      ->getNormalizer()
      ->normalize(
        new JsonApiDocumentTopLevel(new ResourceObjectData([$resource_object], 1)$includesnew LinkCollection([])),
        'api_json',
        [
          'resource_type' => $resource_type,
          'account' => NULL,
          'sparse_fieldset' => [
            'node--article' => [
              'title',
              'node_type',
              'uid',
              'field_tags',
              'field_image',
            ],
throw new UnprocessableEntityHttpException($message);
    }

    // @todo Remove line below in favor of commented line in https://www.drupal.org/project/drupal/issues/2878463.     $self_link = new Link(new CacheableMetadata(), Url::fromRoute('jsonapi.file--file.individual', ['entity' => $file->uuid()]), 'self');
    /* $self_link = new Link(new CacheableMetadata(), $this->entity->toUrl('jsonapi'), ['self']); */
    $links = new LinkCollection(['self' => $self_link]);

    $relatable_resource_types = $resource_type->getRelatableResourceTypesByField($resource_type->getPublicName($file_field_name));
    $file_resource_type = reset($relatable_resource_types);
    $resource_object = ResourceObject::createFromEntity($file_resource_type$file);
    return new ResourceResponse(new JsonApiDocumentTopLevel(new ResourceObjectData([$resource_object], 1)new NullIncludedData()$links), 201, []);
  }

  /** * Ensures that the given account is allowed to upload a file. * * @param \Drupal\Core\Session\AccountInterface $account * The account for which access should be checked. * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition * The field for which the file is to be uploaded. * @param \Drupal\Core\Entity\FieldableEntityInterface|null $entity * The entity, if one exists, for which the file is to be uploaded. */

  public function getIndividual(EntityInterface $entity, Request $request) {
    $resource_object = $this->entityAccessChecker->getAccessCheckedResourceObject($entity);
    if ($resource_object instanceof EntityAccessDeniedHttpException) {
      throw $resource_object;
    }
    $primary_data = new ResourceObjectData([$resource_object], 1);
    $response = $this->buildWrappedResponse($primary_data$request$this->getIncludes($request$primary_data));
    return $response;
  }

  /** * Creates an individual entity. * * @param \Drupal\jsonapi\ResourceType\ResourceType $resource_type * The JSON:API resource type for the request to be served. * @param \Symfony\Component\HttpFoundation\Request $request * The request object. * * @return \Drupal\jsonapi\ResourceResponse * The response. * * @throws \Symfony\Component\HttpKernel\Exception\ConflictHttpException * Thrown when the entity already exists. * @throws \Drupal\jsonapi\Exception\UnprocessableHttpEntityException * Thrown when the entity does not pass validation. */
Home | Imprint | This part of the site doesn't use cookies.