getEntityTypeFromClass example

'banana' => $banana,
    ]);

    $apple->getOriginalClass()->willReturn('\Drupal\apple\Entity\Apple');

    $banana->getOriginalClass()->willReturn('\Drupal\banana\Entity\Banana');
    $banana->getClass()->willReturn('\Drupal\mango\Entity\Mango');
    $banana->id()
      ->willReturn('banana')
      ->shouldBeCalledTimes(2);

    $entity_type_id = $this->entityTypeRepository->getEntityTypeFromClass('\Drupal\banana\Entity\Banana');
    $this->assertSame('banana', $entity_type_id);
    $entity_type_id = $this->entityTypeRepository->getEntityTypeFromClass('\Drupal\mango\Entity\Mango');
    $this->assertSame('banana', $entity_type_id);
  }

  /** * @covers ::getEntityTypeFromClass */
  public function testGetEntityTypeFromClassNoMatch() {
    $apple = $this->prophesize(EntityTypeInterface::class);
    $banana = $this->prophesize(EntityTypeInterface::class);

    
unset($this->values[$name]);
    }
  }

  /** * {@inheritdoc} */
  public static function create(array $values = []) {
    $entity_type_repository = \Drupal::service('entity_type.repository');
    $entity_type_manager = \Drupal::entityTypeManager();
    $class_name = static::class;
    $storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass($class_name));

    // Always explicitly specify the bundle if the entity has a bundle class.     if ($storage instanceof BundleEntityStorageInterface && ($bundle = $storage->getBundleFromClass($class_name))) {
      $values[$storage->getEntityType()->getKey('bundle')] = $bundle;
    }

    return $storage->create($values);
  }

  /** * {@inheritdoc} */
// Stubs for the denormalizer going from entity type manager to entity     // storage.     $entity_type_id = $this->randomMachineName();
    $entity_type_class = $this->randomMachineName();
    $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
    $entity_type_manager->getDefinition($entity_type_id, FALSE)
      ->willReturn($this->prophesize(ConfigEntityTypeInterface::class)->reveal());
    $entity_type_manager->getStorage($entity_type_id)
      ->willReturn($entity_storage->reveal());
    $entity_type_repository = $this->prophesize(EntityTypeRepositoryInterface::class);
    $entity_type_repository->getEntityTypeFromClass($entity_type_class)
      ->willReturn($entity_type_id);
    $entity_field_manager = $this->prophesize(EntityFieldManagerInterface::class);
    $normalizer = new ConfigEntityNormalizer($entity_type_manager->reveal()$entity_type_repository->reveal()$entity_field_manager->reveal());

    // Verify the denormalizer still works correctly: the mock above creates an     // artificial entity object containing exactly the data it received. It also     // should still set _restSubmittedFields correctly.     $expected_denormalization = (object) [
      '_restSubmittedFields' => [
        'test',
      ],
      

  protected function determineEntityTypeId($class$context) {
    // Get the entity type ID while letting context override the $class param.     return !empty($context['entity_type']) ? $context['entity_type'] : $this->getEntityTypeRepository()->getEntityTypeFromClass($class);
  }

  /** * Gets the entity type definition. * * @param string $entity_type_id * The entity type ID to load the definition for. * * @return \Drupal\Core\Entity\EntityTypeInterface * The loaded entity type definition. * * @throws \Symfony\Component\Serializer\Exception\UnexpectedValueException */

  public function getCacheMaxAge() {
    return $this->cacheMaxAge;
  }

  /** * {@inheritdoc} */
  public static function load($id) {
    $entity_type_repository = \Drupal::service('entity_type.repository');
    $entity_type_manager = \Drupal::entityTypeManager();
    $storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass(static::class));
    return $storage->load($id);
  }

  /** * {@inheritdoc} */
  public static function loadMultiple(array $ids = NULL) {
    $entity_type_repository = \Drupal::service('entity_type.repository');
    $entity_type_manager = \Drupal::entityTypeManager();
    $storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass(static::class));
    return $storage->loadMultiple($ids);
  }

  public function testAmbiguousBundleClassExceptionEntityTypeRepository() {
    $this->container->get('state')->set('entity_test_bundle_class_enable_ambiguous_entity_types', TRUE);
    entity_test_create_bundle('entity_test_no_label');
    entity_test_create_bundle('entity_test_no_label', NULL, 'entity_test_no_label');
    // Now that we have an entity bundle class that's shared by two entirely     // different entity types, we expect an exception to be thrown.     $this->expectException(AmbiguousBundleClassException::class);
    $entity_type = $this->container->get('entity_type.repository')->getEntityTypeFromClass(EntityTestAmbiguousBundleClass::class);
  }

  /** * Checks exception thrown if a bundle class doesn't extend the entity class. */
  public function testBundleClassShouldExtendEntityClass() {
    $this->container->get('state')->set('entity_test_bundle_class_non_inheriting', TRUE);
    $this->entityTypeManager->clearCachedDefinitions();
    $this->expectException(BundleClassInheritanceException::class);
    entity_test_create_bundle('bundle_class');
    $this->storage->create(['type' => 'bundle_class']);
  }
Home | Imprint | This part of the site doesn't use cookies.