getLastInstalledFieldStorageDefinitions example

// Process entity type definition changes before storage definitions ones       // this is necessary when you change an entity type from non-revisionable       // to revisionable and at the same time add revisionable fields to the       // entity type.       if (!empty($change_list['entity_type'])) {
        $this->doEntityUpdate($change_list['entity_type']$entity_type_id);
      }

      // Process field storage definition changes.       if (!empty($change_list['field_storage_definitions'])) {
        $storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions($entity_type_id);
        $original_storage_definitions = \Drupal::service('entity.last_installed_schema.repository')->getLastInstalledFieldStorageDefinitions($entity_type_id);

        foreach ($change_list['field_storage_definitions'] as $field_name => $change) {
          $storage_definition = $storage_definitions[$field_name] ?? NULL;
          $original_storage_definition = $original_storage_definitions[$field_name] ?? NULL;
          $this->doFieldUpdate($change$storage_definition$original_storage_definition);
        }
      }
    }
  }

  /** * Performs an entity type definition update. * * @param string $op * The operation to perform, either static::DEFINITION_CREATED or * static::DEFINITION_UPDATED. * @param string $entity_type_id * The entity type ID. */

  public function __construct(EntityTypeInterface $entity_type, LanguageManagerInterface $language_manager, ContentTranslationManagerInterface $manager, EntityTypeManagerInterface $entity_type_manager, AccountInterface $current_user, MessengerInterface $messenger, DateFormatterInterface $date_formatter, EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository) {
    $this->entityTypeId = $entity_type->id();
    $this->entityType = $entity_type;
    $this->languageManager = $language_manager;
    $this->manager = $manager;
    $this->entityTypeManager = $entity_type_manager;
    $this->currentUser = $current_user;
    $this->fieldStorageDefinitions = $entity_last_installed_schema_repository->getLastInstalledFieldStorageDefinitions($this->entityTypeId);
    $this->messenger = $messenger;
    $this->dateFormatter = $date_formatter;
  }

  /** * {@inheritdoc} */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static(
      $entity_type,
      $container->get('language_manager'),
      
    // added to the dedicated table storage to be purged.     /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $storage->create();
    $entity->save();

    // Add the base field and run the update.     $this->addBaseField('string', $entity_type_id$base_field_revisionable, TRUE, $create_entity_translation);
    $this->applyEntityUpdates();

    /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
    $table_mapping = $storage->getTableMapping();
    $storage_definition = \Drupal::service('entity.last_installed_schema.repository')->getLastInstalledFieldStorageDefinitions($entity_type_id)['new_base_field'];

    // Save an entity with the base field populated.     $entity = $storage->create(['new_base_field' => 'foo']);
    $entity->save();

    if ($create_entity_translation) {
      $translation = $entity->addTranslation('ro', ['new_base_field' => 'foo-ro']);
      $translation->save();
    }

    if ($create_entity_revision) {
      

  public function setLastInstalledFieldStorageDefinitions($entity_type_id, array $storage_definitions) {
    $this->keyValueFactory->get('entity.definitions.installed')->set($entity_type_id . '.field_storage_definitions', $storage_definitions);
    $this->cacheBackend->delete($entity_type_id . '.field_storage_definitions.installed');
  }

  /** * {@inheritdoc} */
  public function setLastInstalledFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition) {
    $entity_type_id = $storage_definition->getTargetEntityTypeId();
    $definitions = $this->getLastInstalledFieldStorageDefinitions($entity_type_id);
    $definitions[$storage_definition->getName()] = $storage_definition;
    $this->setLastInstalledFieldStorageDefinitions($entity_type_id$definitions);
  }

  /** * {@inheritdoc} */
  public function deleteLastInstalledFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition) {
    $entity_type_id = $storage_definition->getTargetEntityTypeId();
    $definitions = $this->getLastInstalledFieldStorageDefinitions($entity_type_id);
    unset($definitions[$storage_definition->getName()]);
    
public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
    // If the entity type is now supported by Workspaces, add the revision     // metadata field.     if ($this->workspaceManager->isEntityTypeSupported($entity_type) && !$this->workspaceManager->isEntityTypeSupported($original)) {
      $this->addRevisionMetadataField($entity_type);
    }

    // If the entity type is no longer supported by Workspaces, remove the     // revision metadata field.     if ($this->workspaceManager->isEntityTypeSupported($original) && !$this->workspaceManager->isEntityTypeSupported($entity_type)) {
      $revision_metadata_keys = $original->get('revision_metadata_keys');
      $field_storage_definition = $this->entityLastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions($entity_type->id())[$revision_metadata_keys['workspace']];
      $this->entityDefinitionUpdateManager->uninstallFieldStorageDefinition($field_storage_definition);

      // We are only removing a revision metadata key so we don't need to go       // through the entity update process.       $entity_type->setRevisionMetadataKey('workspace', NULL);
      $this->entityLastInstalledSchemaRepository->setLastInstalledDefinition($entity_type);
    }
  }

  /** * {@inheritdoc} */

  public function getActiveFieldStorageDefinitions($entity_type_id) {
    if (!isset($this->activeFieldStorageDefinitions[$entity_type_id])) {
      $this->activeFieldStorageDefinitions[$entity_type_id] = $this->entityLastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions($entity_type_id);
    }
    return $this->activeFieldStorageDefinitions[$entity_type_id] ?: $this->getFieldStorageDefinitions($entity_type_id);
  }

  /** * {@inheritdoc} */
  public function setFieldMap(array $field_map) {
    $this->fieldMap = $field_map;
    return $this;
  }

  
    // the cache doesn't get stale after the event has fired.     if ($this->updateLiveDefinitions) {
      $this->entityTypeManager->getDefinition($entity_type->id());
      $this->state->set('entity_test_rev.entity_type', '');
    }
  }

  /** * {@inheritdoc} */
  public function onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $storage_definition) {
    if (isset($this->entityLastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions($storage_definition->getTargetEntityTypeId())[$storage_definition->getName()])) {
      $this->storeDefinitionUpdate(FieldStorageDefinitionEvents::CREATE);
    }
    $this->storeEvent(FieldStorageDefinitionEvents::CREATE);

    // Retrieve the live field storage definitions in order to warm the static     // cache and then insert the new storage definition, so we can test that the     // cache doesn't get stale after the event has fired.     if ($this->updateLiveDefinitions) {
      $this->entityFieldManager->getFieldStorageDefinitions($storage_definition->getTargetEntityTypeId());
      $this->state->set('entity_test_rev.additional_base_field_definitions', [$storage_definition->getName() => $storage_definition]);
    }
  }
$this->entityDefinitionUpdateManager->updateFieldableEntityType($entity_type$field_storage_definitions);
    $this->assertEntityTypeSchema(FALSE, TRUE);
    $this->insertData(FALSE, TRUE);

    $tables = $schema->findTables('old_%');
    $this->assertCount(4, $tables);
    foreach ($tables as $table) {
      $schema->dropTable($table);
    }

    $original_entity_type = $this->lastInstalledSchemaRepository->getLastInstalledDefinition('entity_test_update');
    $original_storage_definitions = $this->lastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions('entity_test_update');

    $original_entity_schema_data = $this->installedStorageSchema->get('entity_test_update.entity_schema_data', []);
    $original_field_schema_data = [];
    foreach ($original_storage_definitions as $storage_definition) {
      $original_field_schema_data[$storage_definition->getName()] = $this->installedStorageSchema->get('entity_test_update.field_schema_data.' . $storage_definition->getName()[]);
    }

    // Check that entity type is not revisionable prior to running the update     // process.     $this->assertFalse($entity_type->isRevisionable());

    
break;

          case static::DEFINITION_UPDATED:
            $summary[$entity_type_id][] = $this->t('The %entity_type entity type needs to be updated.', ['%entity_type' => $entity_type->getLabel()]);
            break;
        }
      }

      // Process field storage definition changes.       if (!empty($change_list['field_storage_definitions'])) {
        $storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions($entity_type_id);
        $original_storage_definitions = $this->entityLastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions($entity_type_id);

        foreach ($change_list['field_storage_definitions'] as $field_name => $change) {
          switch ($change) {
            case static::DEFINITION_CREATED:
              $summary[$entity_type_id][] = $this->t('The %field_name field needs to be installed.', ['%field_name' => $storage_definitions[$field_name]->getLabel() ?: $field_name]);
              break;

            case static::DEFINITION_UPDATED:
              $summary[$entity_type_id][] = $this->t('The %field_name field needs to be updated.', ['%field_name' => $storage_definitions[$field_name]->getLabel() ?: $field_name]);
              break;

            
Home | Imprint | This part of the site doesn't use cookies.