onFieldStorageDefinitionCreate example

public function onFieldableEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original, array $field_storage_definitions, array $original_field_storage_definitions, array &$sandbox = NULL) {
    $this->wrapSchemaException(function D) use ($entity_type$original$field_storage_definitions$original_field_storage_definitions, &$sandbox) {
      $this->getStorageSchema()->onFieldableEntityTypeUpdate($entity_type$original$field_storage_definitions$original_field_storage_definitions$sandbox);
    });
  }

  /** * {@inheritdoc} */
  public function onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $storage_definition) {
    $this->wrapSchemaException(function D) use ($storage_definition) {
      $this->getStorageSchema()->onFieldStorageDefinitionCreate($storage_definition);
      $this->fieldStorageDefinitions[$storage_definition->getName()] = $storage_definition;
      $this->tableMapping = NULL;
    });
  }

  /** * {@inheritdoc} */
  public function onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
    $this->wrapSchemaException(function D) use ($storage_definition$original) {
      $this->getStorageSchema()->onFieldStorageDefinitionUpdate($storage_definition$original);
      
/** * Listener method for any field storage definition event. * * @param \Drupal\Core\Field\FieldStorageDefinitionEvent $event * The field storage definition event object. * @param string $event_name * The event name. */
  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;
    }
  }

  
/** * Tests the custom bundle field creation and deletion. */
  public function testCustomFieldCreateDelete() {
    // Install the module which adds the field.     $this->installModule('entity_schema_test');
    $storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions('entity_test_update');
    $this->assertNotNull($storage_definitions['custom_base_field'], 'Base field definition found.');
    $this->assertNotNull($storage_definitions['custom_bundle_field'], 'Bundle field definition found.');

    // Make sure the field schema can be created.     \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']);
    

  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;
    }
  }

  
/** * {@inheritdoc} */
  public function onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $storage_definition) {
    $entity_type_id = $storage_definition->getTargetEntityTypeId();

    // @todo Forward this to all interested handlers, not only storage, once     // iterating handlers is possible: https://www.drupal.org/node/2332857.     $storage = $this->entityTypeManager->getStorage($entity_type_id);
    if ($storage instanceof FieldStorageDefinitionListenerInterface) {
      $storage->onFieldStorageDefinitionCreate($storage_definition);
    }

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

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

  /** * {@inheritdoc} */
  
public function installFieldStorageDefinition($name$entity_type_id$provider, FieldStorageDefinitionInterface $storage_definition) {
    // @todo Pass a mutable field definition interface when we have one. See     // https://www.drupal.org/node/2346329.     if ($storage_definition instanceof BaseFieldDefinition) {
      $storage_definition
        ->setName($name)
        ->setTargetEntityTypeId($entity_type_id)
        ->setProvider($provider)
        ->setTargetBundle(NULL);
    }
    $this->clearCachedDefinitions();
    $this->fieldStorageDefinitionListener->onFieldStorageDefinitionCreate($storage_definition);
  }

  /** * {@inheritdoc} */
  public function getFieldStorageDefinition($name$entity_type_id) {
    $storage_definitions = $this->entityLastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions($entity_type_id);
    return isset($storage_definitions[$name]) ? clone $storage_definitions[$name] : NULL;
  }

  /** * {@inheritdoc} */
throw new FieldException("Attempt to create field storage {$this->getName()} which is reserved by entity type {$this->getTargetEntityTypeId()}.");
    }

    // Check that the field type is known.     $field_type = $field_type_manager->getDefinition($this->getType(), FALSE);
    if (!$field_type) {
      throw new FieldException("Attempt to create a field storage of unknown type {$this->getType()}.");
    }
    $this->module = $field_type['provider'];

    // Notify the field storage definition listener.     \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionCreate($this);
  }

  /** * {@inheritdoc} */
  public function calculateDependencies() {
    parent::calculateDependencies();
    // Ensure the field is dependent on the providing module.     $this->addDependency('module', $this->getTypeProvider());
    // Ask the field type for any additional storage dependencies.     // @see \Drupal\Core\Field\FieldItemInterface::calculateStorageDependencies()
$this->setUpStorageSchema($expected);

    $table_mapping = new TestSqlContentDefaultTableMapping($this->entityType, $this->storageDefinitions);
    $table_mapping->setFieldNames($entity_type_idarray_keys($this->storageDefinitions));
    $table_mapping->setExtraColumns($entity_type_id['default_langcode']);

    $this->storageSchema->expects($this->any())
      ->method('getTableMapping')
      ->willReturn($table_mapping);

    $this->assertNull(
      $this->storageSchema->onFieldStorageDefinitionCreate($field_storage)
    );
  }

  /** * Tests the schema for a field dedicated table for an entity with a string identifier. * * @covers ::onFieldStorageDefinitionCreate * @covers ::getDedicatedTableSchema * @covers ::createDedicatedTableSchema */
  public function testDedicatedTableSchemaForEntityWithStringIdentifier() {
    
$event_subscriber = $this->container->get('entity_test.definition.subscriber');
    $event_subscriber->enableEventTracking();
    $event_subscriber->enableLiveDefinitionUpdates();

    // Test field storage definition events.     $storage_definition = FieldStorageDefinition::create('string')
      ->setName('field_storage_test')
      ->setLabel(new TranslatableMarkup('Field storage test'))
      ->setTargetEntityTypeId('entity_test_rev');

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

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

    $updated_storage_definition = clone $storage_definition;
    $updated_storage_definition->setLabel(new TranslatableMarkup('Updated field storage test'));
    $this->assertFalse($event_subscriber->hasEventFired(FieldStorageDefinitionEvents::UPDATE), 'Entity type update was not dispatched yet.');
    
Home | Imprint | This part of the site doesn't use cookies.