countFieldData example

    if (!$this->hasSharedTableStructureChange($entity_type$original)) {
      return FALSE;
    }

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

  /** * {@inheritdoc} */
  public function requiresFieldDataMigration(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
    return !$this->storage->countFieldData($original, TRUE);
  }

  /** * {@inheritdoc} */
  public function onEntityTypeCreate(EntityTypeInterface $entity_type) {
    $this->onFieldableEntityTypeCreate($entity_type$this->entityFieldManager->getFieldStorageDefinitions($entity_type->id()));
  }

  /** * {@inheritdoc} */

  protected static function initializeSandbox(array &$context, ConfigImporter $config_importer) {
    $context['sandbox']['field']['purge_batch_size'] = \Drupal::config('field.settings')->get('purge_batch_size');
    // Save the future list of installed extensions to limit the amount of times     // the configuration is read from disk.     $context['sandbox']['field']['extensions'] = $config_importer->getStorageComparer()->getSourceStorage()->read('core.extension');

    $context['sandbox']['field']['steps_to_delete'] = 0;
    $fields = static::getFieldStoragesToPurge($context['sandbox']['field']['extensions']$config_importer->getUnprocessedConfiguration('delete'));
    foreach ($fields as $field) {
      $row_count = \Drupal::entityTypeManager()->getStorage($field->getTargetEntityTypeId())
        ->countFieldData($field);
      if ($row_count > 0) {
        // The number of steps to delete each field is determined by the         // purge_batch_size setting. For example if the field has 9 rows and the         // batch size is 10 then this will add 1 step to $number_of_steps.         $how_many_steps = ceil($row_count / $context['sandbox']['field']['purge_batch_size']);
        $context['sandbox']['field']['steps_to_delete'] += $how_many_steps;
      }
    }
    // Each field possibly needs one last field_purge_batch() call to remove the     // last field and the field storage itself.     $context['sandbox']['field']['steps_to_delete'] += count($fields);

    
$this->assertSame($expected$this->findPrimaryKeys($entity_type));

    // Now test updating a field without data. This will end up deleting     // and re-creating the field, similar to the code above.     $update_manager->updateFieldStorageDefinition($field);
    $this->assertSame($expected$this->findPrimaryKeys($entity_type));

    // Now test updating a field with data.     /** @var \Drupal\Core\Entity\FieldableEntityStorageInterface $storage */
    $storage = $this->entityTypeManager->getStorage($entity_type_id);
    $storage->create()->save();
    $this->assertTrue($storage->countFieldData($field, TRUE));
    $update_manager->updateFieldStorageDefinition($field);
    $this->assertSame($expected$this->findPrimaryKeys($entity_type));
    $this->assertTrue($storage->countFieldData($field, TRUE));
  }

  /** * Finds the primary keys for a given entity type. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type whose primary keys are being fetched. * * @return array[] * An array where the keys are the table names of the entity type's tables * and the values are a list of the respective primary keys. */
$entity_type_manager = \Drupal::entityTypeManager();

    parent::preDelete($storage$fields);

    // Keep the field definitions in the deleted fields repository so we can use     // them later during field_purge_batch().     /** @var \Drupal\field\FieldConfigInterface $field */
    foreach ($fields as $field) {
      // Only mark a field for purging if there is data. Otherwise, just remove       // it.       $target_entity_storage = $entity_type_manager->getStorage($field->getTargetEntityTypeId());
      if (!$field->deleted && $target_entity_storage instanceof FieldableEntityStorageInterface && $target_entity_storage->countFieldData($field->getFieldStorageDefinition(), TRUE)) {
        $field = clone $field;
        $field->deleted = TRUE;
        $field->fieldStorage = NULL;
        $deleted_fields_repository->addFieldDefinition($field);
      }
    }
  }

  /** * {@inheritdoc} */
  

  public function onFieldStorageDefinitionDelete(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);

    // 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);

    
'entity_type' => 'entity_test',
      'type' => 'integer',
      'cardinality' => 2,
    ]);
    $field_storage->save();
    FieldConfig::create([
      'field_storage' => $field_storage,
      'bundle' => 'entity_test',
    ])->save();

    $this->assertFalse($field_storage->hasdata(), 'There are no entities with field data.');
    $this->assertSame(0, $this->storage->countFieldData($field_storage), 'There are 0 entities with field data.');

    // Create 1 entity without the field.     $entity = EntityTest::create();
    $entity->name->value = $this->randomMachineName();
    $entity->save();

    $this->assertFalse($field_storage->hasdata(), 'There are no entities with field data.');
    $this->assertSame(0, $this->storage->countFieldData($field_storage), 'There are 0 entities with field data.');

    // Create 12 entities to ensure that the purging works as expected.     for ($i = 0; $i < 12; $i++) {
      
// Delete or fix any configuration that is dependent, for example, fields.     parent::preDelete($storage$field_storages);

    // Keep the field storage definitions in the deleted fields repository so we     // can use them later during field_purge_batch().     /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */
    foreach ($field_storages as $field_storage) {
      // Only mark a field for purging if there is data. Otherwise, just remove       // it.       $target_entity_storage = \Drupal::entityTypeManager()->getStorage($field_storage->getTargetEntityTypeId());
      if (!$field_storage->deleted && $target_entity_storage instanceof FieldableEntityStorageInterface && $target_entity_storage->countFieldData($field_storage, TRUE)) {
        $storage_definition = clone $field_storage;
        $storage_definition->deleted = TRUE;
        $deleted_fields_repository->addFieldStorageDefinition($storage_definition);
      }
    }
  }

  /** * {@inheritdoc} */
  public static function postDelete(EntityStorageInterface $storage, array $fields) {
    
Home | Imprint | This part of the site doesn't use cookies.