getStorageClass example


trait SqlFieldableEntityTypeListenerTrait {

  /** * {@inheritdoc} */
  public function onFieldableEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original, array $field_storage_definitions, array $original_field_storage_definitions, array &$sandbox = NULL) {
    /** @var \Drupal\Core\Entity\EntityStorageInterface $original_storage */
    $original_storage = $this->entityTypeManager->createHandlerInstance($original->getStorageClass()$original);
    $has_data = $original_storage->hasData();

    // If 'progress' is not set, then this will be the first run of the batch.     if (!isset($sandbox['progress'])) {
      // We cannot support updating the schema of an entity type from       // revisionable to non-revisionable or translatable to non-translatable       // because that can lead to unintended data loss.       // @todo Add support for these conversions in case there is no data loss.       // @see https://www.drupal.org/project/drupal/issues/3024727       $convert_rev_to_non_rev = $original->isRevisionable() && !$entity_type->isRevisionable();
      $convert_mul_to_non_mul = $original->isTranslatable() && !$entity_type->isTranslatable();
      

  protected static function isLocatableResourceType(EntityTypeInterface $entity_type$bundle) {
    assert(is_string($bundle) && !empty($bundle), 'A bundle ID is required. Bundleless entity types should pass the entity type ID again.');
    return $entity_type->getStorageClass() !== ContentEntityNullStorage::class;
  }

  /** * Whether an entity type is a versionable resource type. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type to assess. * * @return bool * TRUE if the entity type is versionable, FALSE otherwise. */
  
// 201 for well-formed request that creates another entity.     // If the entity is stored, delete the first created entity (in case there     // is a uniqueness constraint).     if (get_class($this->entityStorage) !== ContentEntityNullStorage::class) {
      $this->entityStorage->load(static::$firstCreatedEntityId)->delete();
    }
    $response = $this->request('POST', $url$request_options);
    $this->assertResourceResponse(201, FALSE, $response);
    $this->assertFalse($response->hasHeader('X-Drupal-Cache'));

    if ($this->entity->getEntityType()->getStorageClass() !== ContentEntityNullStorage::class && $this->entity->getEntityType()->hasKey('uuid')) {
      $second_created_entity = $this->entityStorage->load(static::$secondCreatedEntityId);
      $uuid = $second_created_entity->uuid();
      // @todo Remove line below in favor of commented line in https://www.drupal.org/project/drupal/issues/2878463.       $location = Url::fromRoute(sprintf('jsonapi.%s.individual', static::$resourceTypeName)['entity' => $uuid]);
      /* $location = $this->entityStorage->load(static::$secondCreatedEntityId)->toUrl('jsonapi')->setAbsolute(TRUE)->toString(); */
      if (static::$resourceTypeIsVersionable) {
        assert($created_entity instanceof RevisionableInterface);
        $location->setOption('query', ['resourceVersion' => 'id:' . $second_created_entity->getRevisionId()]);
      }
      $this->assertSame([$location->setAbsolute()->toString()]$response->getHeader('Location'));

      
$this->assertFalse($response->hasHeader('X-Drupal-Cache'));
    // If the entity is stored, perform extra checks.     if (get_class($this->entityStorage) !== ContentEntityNullStorage::class) {
      // Assert that the entity was indeed created, and that the response body       // contains the serialized created entity.       $created_entity = $this->entityStorage->loadUnchanged(static::$firstCreatedEntityId);
      $created_entity_normalization = $this->serializer->normalize($created_entitystatic::$format['account' => $this->account]);
      $this->assertSame($created_entity_normalization$this->serializer->decode((string) $response->getBody()static::$format));
      $this->assertStoredEntityMatchesSentNormalization($this->getNormalizedPostEntity()$created_entity);
    }

    if ($this->entity->getEntityType()->getStorageClass() !== ContentEntityNullStorage::class && $this->entity->getEntityType()->hasKey('uuid')) {
      // 500 when creating an entity with a duplicate UUID.       $normalized_entity = $this->getModifiedEntityForPostTesting();
      $normalized_entity[$created_entity->getEntityType()->getKey('uuid')] = [['value' => $created_entity->uuid()]];
      if ($label_field) {
        $normalized_entity[$label_field] = [['value' => $this->randomMachineName()]];
      }
      $request_options[RequestOptions::BODY] = $this->serializer->encode($normalized_entitystatic::$format);

      $response = $this->request('POST', $url$request_options);
      $this->assertSame(500, $response->getStatusCode());
      $this->assertStringContainsString('Internal Server Error', (string) $response->getBody());

      

    }
    return $schema;
  }

  /** * {@inheritdoc} */
  public function requiresEntityDataMigration(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
    // If the original storage has existing entities, or it is impossible to     // determine if that is the case, require entity data to be migrated.     $original_storage_class = $original->getStorageClass();
    if (!class_exists($original_storage_class)) {
      return TRUE;
    }

    // Data migration is not needed when only indexes changed, as they can be     // applied if there is data.     if (!$this->hasSharedTableStructureChange($entity_type$original)) {
      return FALSE;
    }

    return $this->storage->hasData();
  }
$this->clearCachedDefinitions();
  }

  /** * {@inheritdoc} */
  public function onFieldableEntityTypeCreate(EntityTypeInterface $entity_type, array $field_storage_definitions) {
    $entity_type_id = $entity_type->id();

    // @todo Forward this to all interested handlers, not only storage, once     // iterating handlers is possible: https://www.drupal.org/node/2332857.     $storage = $this->entityTypeManager->createHandlerInstance($entity_type->getStorageClass()$entity_type);
    if ($storage instanceof EntityTypeListenerInterface) {
      $storage->onFieldableEntityTypeCreate($entity_type$field_storage_definitions);
    }

    $this->entityLastInstalledSchemaRepository->setLastInstalledDefinition($entity_type);
    if ($entity_type->entityClassImplements(FieldableEntityInterface::class)) {
      $this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinitions($entity_type_id$field_storage_definitions);
    }

    $this->eventDispatcher->dispatch(new EntityTypeEvent($entity_type), EntityTypeEvents::CREATE);
    $this->clearCachedDefinitions();
  }
/** * Tests the getStorageClass() method. */
  public function testGetStorageClass() {
    $controller = $this->getTestHandlerClass();
    $entity_type = $this->setUpEntityType([
      'handlers' => [
        'storage' => $controller,
      ],
    ]);
    $this->assertSame($controller$entity_type->getStorageClass());
  }

  /** * Tests the setStorageClass() method. */
  public function testSetStorageClass() {
    $controller = $this->getTestHandlerClass();
    $entity_type = $this->setUpEntityType([]);
    $this->assertSame($entity_type$entity_type->setStorageClass($controller));
  }

  
$expected_prefix = $definition['provider'] . '.' . $definition['config_prefix'];
    $this->assertEquals($expected_prefix$config_entity->getConfigPrefix());
  }

  /** * @covers ::__construct */
  public function testConstruct() {
    $config_entity = new ConfigEntityType([
      'id' => 'example_config_entity_type',
    ]);
    $this->assertEquals('Drupal\Core\Config\Entity\ConfigEntityStorage', $config_entity->getStorageClass());
  }

  /** * @covers ::__construct */
  public function testConstructBadStorage() {
    $this->expectException(ConfigEntityStorageClassException::class);
    $this->expectExceptionMessage('\Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage is not \Drupal\Core\Config\Entity\ConfigEntityStorage or it does not extend it');
    new ConfigEntityType([
      'id' => 'example_config_entity_type',
      'handlers' => ['storage' => '\Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage'],
    ]);
Home | Imprint | This part of the site doesn't use cookies.