onFieldStorageDefinitionDelete example

protected function doFieldUpdate($op$storage_definition = NULL, $original_storage_definition = NULL) {
    switch ($op) {
      case EntityDefinitionUpdateManagerInterface::DEFINITION_CREATED:
        \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionCreate($storage_definition);
        break;

      case EntityDefinitionUpdateManagerInterface::DEFINITION_UPDATED:
        \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionUpdate($storage_definition$original_storage_definition);
        break;

      case EntityDefinitionUpdateManagerInterface::DEFINITION_DELETED:
        \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionDelete($original_storage_definition);
        break;
    }
  }

  /** * Enables a new entity type definition. */
  protected function enableNewEntityType() {
    $this->state->set('entity_test_new', TRUE);
    $this->applyEntityUpdates('entity_test_new');
  }

  
// Keep the field definition in the deleted fields repository so we can use     // it later during field_purge_batch(), but only if the field has data.     if ($storage_definition instanceof BaseFieldDefinition && $storage instanceof FieldableEntityStorageInterface && $storage->countFieldData($storage_definition, TRUE)) {
      $deleted_storage_definition = clone $storage_definition;
      $deleted_storage_definition->setDeleted(TRUE);
      $this->deletedFieldsRepository->addFieldDefinition($deleted_storage_definition);
      $this->deletedFieldsRepository->addFieldStorageDefinition($deleted_storage_definition);
    }

    if ($storage instanceof FieldStorageDefinitionListenerInterface) {
      $storage->onFieldStorageDefinitionDelete($storage_definition);
    }

    $this->entityLastInstalledSchemaRepository->deleteLastInstalledFieldStorageDefinition($storage_definition);

    $this->eventDispatcher->dispatch(new FieldStorageDefinitionEvent($storage_definition), FieldStorageDefinitionEvents::DELETE);
    $this->entityFieldManager->clearCachedFieldDefinitions();
  }

}

    }
  }

  /** * {@inheritdoc} */
  public static function postDelete(EntityStorageInterface $storage, array $fields) {
    // Notify the storage.     foreach ($fields as $field) {
      if (!$field->deleted) {
        \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionDelete($field);
        $field->deleted = TRUE;
      }
    }
    // Unset static flag.     static::$inDeletion = FALSE;
  }

  /** * {@inheritdoc} */
  public function getSchema() {
    
public function updateFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition) {
    $original = $this->getFieldStorageDefinition($storage_definition->getName()$storage_definition->getTargetEntityTypeId());
    $this->clearCachedDefinitions();
    $this->fieldStorageDefinitionListener->onFieldStorageDefinitionUpdate($storage_definition$original);
  }

  /** * {@inheritdoc} */
  public function uninstallFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition) {
    $this->clearCachedDefinitions();
    $this->fieldStorageDefinitionListener->onFieldStorageDefinitionDelete($storage_definition);
  }

  /** * {@inheritdoc} */
  public function getChangeList() {
    $this->entityTypeManager->useCaches(FALSE);
    $this->entityFieldManager->useCaches(FALSE);
    $change_list = [];

    foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
      
public function onFieldStorageDefinitionEvent(FieldStorageDefinitionEvent $event$event_name) {
    switch ($event_name) {
      case FieldStorageDefinitionEvents::CREATE:
        $this->onFieldStorageDefinitionCreate($event->getFieldStorageDefinition());
        break;

      case FieldStorageDefinitionEvents::UPDATE:
        $this->onFieldStorageDefinitionUpdate($event->getFieldStorageDefinition()$event->getOriginal());
        break;

      case FieldStorageDefinitionEvents::DELETE:
        $this->onFieldStorageDefinitionDelete($event->getFieldStorageDefinition());
        break;
    }
  }

  /** * {@inheritdoc} */
  public function onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $storage_definition) {
  }

  /** * {@inheritdoc} */
    \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionCreate($storage_definitions['custom_base_field']);
    \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionCreate($storage_definitions['custom_bundle_field']);
    /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
    $table_mapping = $this->entityTypeManager->getStorage('entity_test_update')->getTableMapping();
    $base_table = current($table_mapping->getTableNames());
    $base_column = current($table_mapping->getColumnNames('custom_base_field'));
    $this->assertTrue($this->database->schema()->fieldExists($base_table$base_column), 'Table column created');
    $table = $table_mapping->getDedicatedDataTableName($storage_definitions['custom_bundle_field']);
    $this->assertTrue($this->database->schema()->tableExists($table), 'Table created');

    // Make sure the field schema can be deleted.     \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionDelete($storage_definitions['custom_base_field']);
    \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionDelete($storage_definitions['custom_bundle_field']);
    $this->assertFalse($this->database->schema()->fieldExists($base_table$base_column), 'Table column dropped');
    $this->assertFalse($this->database->schema()->tableExists($table), 'Table dropped');
  }

  /** * Updates the entity type definition. * * @param bool $alter * Whether the original definition should be altered or not. */
  
$this->assertFalse($event_subscriber->hasEventFired(FieldStorageDefinitionEvents::UPDATE), 'Entity type update was not dispatched yet.');
    \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionUpdate($updated_storage_definition$storage_definition);
    $this->assertTrue($event_subscriber->hasEventFired(FieldStorageDefinitionEvents::UPDATE), 'Entity type update event successfully dispatched.');
    $this->assertTrue($event_subscriber->hasDefinitionBeenUpdated(FieldStorageDefinitionEvents::UPDATE), 'Last installed field storage definition was updated before the event was fired.');

    // Check that the updated field can be retrieved from the live field storage     // definitions.     $field_storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions('entity_test_rev');
    $this->assertEquals(new TranslatableMarkup('Updated field storage test')$field_storage_definitions['field_storage_test']->getLabel());

    $this->assertFalse($event_subscriber->hasEventFired(FieldStorageDefinitionEvents::DELETE), 'Entity type delete was not dispatched yet.');
    \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionDelete($storage_definition);
    $this->assertTrue($event_subscriber->hasEventFired(FieldStorageDefinitionEvents::DELETE), 'Entity type delete event successfully dispatched.');
    $this->assertTrue($event_subscriber->hasDefinitionBeenUpdated(FieldStorageDefinitionEvents::DELETE), 'Last installed field storage definition was deleted before the event was fired.');

    // Check that the deleted field can no longer be retrieved from the live     // field storage definitions.     $field_storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions('entity_test_rev');
    $this->assertArrayNotHasKey('field_storage_test', $field_storage_definitions);

    // Test entity type events.     $entity_type = $this->entityTypeManager->getDefinition('entity_test_rev');

    
->fields(['deleted' => 1])
        ->execute();
      if ($this->entityType->isRevisionable()) {
        $this->database->update($revision_table)
          ->fields(['deleted' => 1])
          ->execute();
      }
    }

    // Update the field schema.     $this->wrapSchemaException(function D) use ($storage_definition) {
      $this->getStorageSchema()->onFieldStorageDefinitionDelete($storage_definition);
      unset($this->fieldStorageDefinitions[$storage_definition->getName()]);
      $this->tableMapping = NULL;
    });
  }

  /** * Wraps a database schema exception into an entity storage exception. * * @param callable $callback * The callback to be executed. * * @throws \Drupal\Core\Entity\EntityStorageException * When a database schema exception is thrown. */
Home | Imprint | This part of the site doesn't use cookies.