getByTypeName example


  public function testGetRelatableResourceTypes($resource_type_name$relatable_type_names) {
    // We're only testing the fields that we set up.     $test_fields = [
      'field_ref_foo',
      'field_ref_bar',
      'field_ref_any',
    ];

    $resource_type = $this->resourceTypeRepository->getByTypeName($resource_type_name);

    // This extracts just the relationship fields under test.     $subjects = array_intersect_key(
      $resource_type->getRelatableResourceTypes(),
      array_flip($test_fields)
    );

    // Map the related resource type to their type name so we can just compare     // the type names rather that the whole object.     foreach ($test_fields as $field_name) {
      if (isset($subjects[$field_name])) {
        
public function getFieldsProvider() {
    return [
      [['type', 'node_type']],
      [['id', 'node_id']],
    ];
  }

  /** * Tests that resource types can be disabled by a build subscriber. */
  public function testResourceTypeDisabling() {
    $this->assertFalse($this->resourceTypeRepository->getByTypeName('node--article')->isInternal());
    $this->assertFalse($this->resourceTypeRepository->getByTypeName('node--page')->isInternal());
    $this->assertFalse($this->resourceTypeRepository->getByTypeName('user--user')->isInternal());
    $disabled_resource_types = [
      'node--page',
      'user--user',
    ];
    \Drupal::state()->set('jsonapi_test_resource_type_builder.disabled_resource_types', $disabled_resource_types);
    Cache::invalidateTags(['jsonapi_resource_types']);
    $this->assertFalse($this->resourceTypeRepository->getByTypeName('node--article')->isInternal());
    $this->assertTrue($this->resourceTypeRepository->getByTypeName('node--page')->isInternal());
    $this->assertTrue($this->resourceTypeRepository->getByTypeName('user--user')->isInternal());
  }
'info' => [
        'href' => 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1',
      ],
      'via' => ['href' => 'http://localhost/'],
    ]$normalized['errors'][0]['links']);
  }

  /** * Tests the message and exceptions when requesting a Label only resource. */
  public function testAliasFieldRouteException() {
    $this->assertSame('uid', $this->resourceTypeRepository->getByTypeName('node--article')->getPublicName('uid'));
    $this->assertSame('roles', $this->resourceTypeRepository->getByTypeName('user--user')->getPublicName('roles'));
    $resource_type_field_aliases = [
      'node--article' => [
        'uid' => 'author',
      ],
      'user--user' => [
        'roles' => 'user_roles',
      ],
    ];
    \Drupal::state()->set('jsonapi_test_resource_type_builder.resource_type_field_aliases', $resource_type_field_aliases);
    Cache::invalidateTags(['jsonapi_resource_types']);
    

  public function __construct(ResourceTypeRepositoryInterface $resource_type_repository) {
    $this->resourceTypeRepository = $resource_type_repository;
  }

  /** * {@inheritdoc} */
  public function convert($value$definition$name, array $defaults) {
    return $this->resourceTypeRepository->getByTypeName($value);
  }

  /** * {@inheritdoc} */
  public function applies($definition$name, Route $route) {
    return (!empty($definition['type']) && $definition['type'] === static::PARAM_TYPE_ID);
  }

}

  protected function getEntityFromResourceIdentifier(ResourceIdentifier $resource_identifier) {
    $resource_type_name = $resource_identifier->getTypeName();
    if (!($target_resource_type = $this->resourceTypeRepository->getByTypeName($resource_type_name))) {
      throw new BadRequestHttpException("The resource type `{$resource_type_name}` does not exist.");
    }
    $id = $resource_identifier->getId();
    if (!($targeted_resource = $this->entityRepository->loadEntityByUuid($target_resource_type->getEntityTypeId()$id))) {
      throw new BadRequestHttpException("The targeted `{$resource_type_name}` resource with ID `{$id}` does not exist.");
    }
    return $targeted_resource;
  }

  /** * Determines if the client needs to be updated with new relationship data. * * @param array $received_resource_identifiers * The array of resource identifiers given by the client. * @param array $final_resource_identifiers * The final array of resource identifiers after applying the requested * changes. * * @return bool * Whether the final array of resource identifiers is different than the * client-sent data. */
      $relationships = array_map(function D$relationship) {
        if (empty($relationship['data'])) {
          return [];
        }
        if (empty($relationship['data'][0]['id'])) {
          throw new BadRequestHttpException("No ID specified for related resource");
        }
        $id_list = array_column($relationship['data'], 'id');
        if (empty($relationship['data'][0]['type'])) {
          throw new BadRequestHttpException("No type specified for related resource");
        }
        if (!$resource_type = $this->resourceTypeRepository->getByTypeName($relationship['data'][0]['type'])) {
          throw new BadRequestHttpException("Invalid type specified for related resource: '" . $relationship['data'][0]['type'] . "'");
        }

        $entity_type_id = $resource_type->getEntityTypeId();
        try {
          $entity_storage = $this->entityTypeManager->getStorage($entity_type_id);
        }
        catch (PluginNotFoundException $e) {
          throw new BadRequestHttpException("Invalid type specified for related resource: '" . $relationship['data'][0]['type'] . "'");
        }
        // In order to maintain the order ($delta) of the relationships, we need
public function getTypeName() {
    return $this->resourceTypeName;
  }

  /** * {@inheritdoc} */
  public function getResourceType() {
    if (!isset($this->resourceType)) {
      /** @var \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface $resource_type_repository */
      $resource_type_repository = \Drupal::service('jsonapi.resource_type.repository');
      $this->resourceType = $resource_type_repository->getByTypeName($this->getTypeName());
    }
    return $this->resourceType;
  }

  /** * Gets the ResourceIdentifier's ID. * * @return string * The ID. */
  public function getId() {
    
protected $normalizer;

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    $resource_type_repository = $this->prophesize(ResourceTypeRepository::class);

    $resource_type_repository
      ->getByTypeName(Argument::any())
      ->willReturn(new ResourceType('node', 'article', NULL));

    $entity_storage = $this->prophesize(EntityStorageInterface::class);
    $self = $this;
    $uuid_to_id = [
      '76dd5c18-ea1b-4150-9e75-b21958a2b836' => 1,
      'fcce1b61-258e-4054-ae36-244d25a9e04c' => 2,
    ];
    $entity_storage->loadByProperties(Argument::type('array'))
      ->will(function D$args) use ($self$uuid_to_id) {
        $result = [];
        
    // as expected.     $this->account = $this->createUser();
    $this->container->get('current_user')->setAccount($this->account);

    // Create an entity.     $entity_type_manager = $this->container->get('entity_type.manager');
    $this->entityStorage = $entity_type_manager->getStorage(static::$entityTypeId);
    $this->uuidKey = $entity_type_manager->getDefinition(static::$entityTypeId)
      ->getKey('uuid');
    $this->entity = $this->setUpFields($this->createEntity()$this->account);

    $this->resourceType = $this->container->get('jsonapi.resource_type.repository')->getByTypeName(static::$resourceTypeName);
  }

  /** * Sets up additional fields for testing. * * @param \Drupal\Core\Entity\EntityInterface $entity * The primary test entity. * @param \Drupal\user\UserInterface $account * The primary test user account. * * @return \Drupal\Core\Entity\EntityInterface * The reloaded entity with the new fields attached. * * @throws \Drupal\Core\Entity\EntityStorageException */
Home | Imprint | This part of the site doesn't use cookies.