field_purge_batch example

// Use assertEquals and not assertSame here to prevent that a different       // sequence of the columns in the table will affect the check.       $this->assertEquals($expected$result);
    }

    // Check that the field storage definition is marked for purging.     $deleted_storage_definitions = \Drupal::service('entity_field.deleted_fields_repository')->getFieldStorageDefinitions();
    $this->assertArrayHasKey($storage_definition->getUniqueStorageIdentifier()$deleted_storage_definitions, 'The base field is marked for purging.');

    // Purge field data, and check that the storage definition has been     // completely removed once the data is purged.     field_purge_batch(10);
    $deleted_storage_definitions = \Drupal::service('entity_field.deleted_fields_repository')->getFieldStorageDefinitions();
    $this->assertEmpty($deleted_storage_definitions, 'The base field has been deleted.');
    $this->assertFalse($schema_handler->tableExists($dedicated_deleted_table_name), 'A dedicated field table was deleted after new_base_field was purged.');

    if (isset($dedicated_deleted_revision_table_name)) {
      $this->assertFalse($schema_handler->tableExists($dedicated_deleted_revision_table_name), 'A dedicated field revision table was deleted after new_base_field was purged.');
    }
  }

  /** * Test cases for ::testBaseFieldDeleteWithExistingData. */
$this->assertEquals(24, $result, 'The field table has 24 rows.');
    }

    $this->assertTrue($field_storage->hasdata(), 'There are entities with field data.');
    $this->assertEquals(12, $this->storage->countFieldData($field_storage), 'There are 12 entities with field data.');

    // Ensure the methods work on deleted fields.     $field_storage->delete();
    $this->assertTrue($field_storage->hasdata(), 'There are entities with deleted field data.');
    $this->assertEquals(12, $this->storage->countFieldData($field_storage), 'There are 12 entities with deleted field data.');

    field_purge_batch(6);
    $this->assertTrue($field_storage->hasdata(), 'There are entities with deleted field data.');
    $this->assertEquals(6, $this->storage->countFieldData($field_storage), 'There are 6 entities with deleted field data.');

    $entity_type = 'entity_test_rev';
    $this->createFieldWithStorage('_2', $entity_type);

    $entity_init = $this->container->get('entity_type.manager')
      ->getStorage($entity_type)
      ->create(['type' => $entity_type]);
    $cardinality = $this->fieldTestData->field_storage_2->getCardinality();

    
->condition('deleted', 1)
      ->countQuery()
      ->execute();
    $this->assertEquals(1, $result->fetchField(), 'Field data has been deleted');

    // Ensure that the field no longer exists in the field map.     $field_map = \Drupal::service('entity_field.manager')->getFieldMap();
    $this->assertFalse(isset($field_map['entity_test_update']['custom_bundle_field']));

    // Purge field data, and check that the storage definition has been     // completely removed once the data is purged.     field_purge_batch(10);
    $this->assertFalse($this->database->schema()->tableExists($table), 'Custom field table was deleted');
  }

}

    $this->drupalLogin($this->adminUser);

    // Drop default comment field added in CommentTestBase::setUp().     FieldStorageConfig::loadByName('node', 'comment')->delete();
    if ($field_storage = FieldStorageConfig::loadByName('node', 'comment_forum')) {
      $field_storage->delete();
    }

    // Purge field data now to allow comment module to be uninstalled once the     // field has been deleted.     field_purge_batch(10);

    // Uninstall the comment module.     $edit = [];
    $edit['uninstall[comment]'] = TRUE;
    $this->drupalGet('admin/modules/uninstall');
    $this->submitForm($edit, 'Uninstall');
    $this->submitForm([], 'Uninstall');
    $this->rebuildContainer();
    $this->assertFalse($this->container->get('module_handler')->moduleExists('comment'), 'Comment module uninstalled.');

    // Install core content type module (book).
$this->messenger()->addError($this->t('There was a problem removing the %field from the %type content type.', ['%field' => $this->entity->label(), '%type' => $bundle_label]));
    }

    $form_state->setRedirectUrl($this->getCancelUrl());

    // Fields are purged on cron. However field module prevents disabling modules     // when field types they provided are used in a field until it is fully     // purged. In the case that a field has minimal or no content, a single call     // to field_purge_batch() will remove it from the system. Call this with a     // low batch limit to avoid administrators having to wait for cron runs when     // removing fields that meet this criteria.     field_purge_batch(10);
  }

}
    $field_storages = static::getFieldStoragesToPurge($context['sandbox']['field']['extensions']$config_importer->getUnprocessedConfiguration('delete'));
    // Get the first field storage to process.     $field_storage = reset($field_storages);
    if (!isset($context['sandbox']['field']['current_storage_id']) || $context['sandbox']['field']['current_storage_id'] != $field_storage->id()) {
      $context['sandbox']['field']['current_storage_id'] = $field_storage->id();
      // If the storage has not been deleted yet we need to do that. This is the       // case when the storage deletion is staged.       if (!$field_storage->isDeleted()) {
        $field_storage->delete();
      }
    }
    field_purge_batch($context['sandbox']['field']['purge_batch_size']$field_storage->getUniqueStorageIdentifier());
    $context['sandbox']['field']['current_progress']++;
    $fields_to_delete_count = count(static::getFieldStoragesToPurge($context['sandbox']['field']['extensions']$config_importer->getUnprocessedConfiguration('delete')));
    if ($fields_to_delete_count == 0) {
      $context['finished'] = 1;
    }
    else {
      $context['finished'] = $context['sandbox']['field']['current_progress'] / $context['sandbox']['field']['steps_to_delete'];
      $context['message'] = \Drupal::translation()->translate('Purging field @field_label', ['@field_label' => $field_storage->label()]);
    }
  }

  
$this->assertSame([]$active->listAll($field_config_name));
    $this->assertSame([]$active->listAll($field_config_name_2a));
    $this->assertSame([]$active->listAll($field_config_name_2b));

    // Check that only the first storage definition is preserved in state.     $deleted_storages = \Drupal::state()->get('field.storage.deleted', []);
    $this->assertTrue(isset($deleted_storages[$field_storage_uuid]));
    $this->assertFalse(isset($deleted_storages[$field_storage_uuid_2]));

    // Purge field data, and check that the storage definition has been     // completely removed once the data is purged.     field_purge_batch(10);
    $deleted_storages = \Drupal::state()->get('field.storage.deleted', []);
    $this->assertEmpty($deleted_storages, 'Fields are deleted');
  }

}
$entity->{$field_name}->setValue($this->_generateTestFieldValues(1));
      $entity->save();
    }

    // Check that the two field storages have different tables.     $storage = \Drupal::entityTypeManager()->getStorage($this->entityTypeId);
    /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
    $table_mapping = $storage->getTableMapping();
    $deleted_table_name = $table_mapping->getDedicatedDataTableName($deleted_field_storage, TRUE);
    $active_table_name = $table_mapping->getDedicatedDataTableName($field_storage);

    field_purge_batch(50);

    // Ensure the new field still has its table and the deleted one has been     // removed.     $this->assertTrue(\Drupal::database()->schema()->tableExists($active_table_name));
    $this->assertFalse(\Drupal::database()->schema()->tableExists($deleted_table_name));

    // The field has been removed from the system.     $fields = \Drupal::entityTypeManager()->getStorage('field_config')->loadByProperties(['field_storage_uuid' => $deleted_field_storage->uuid(), 'deleted' => TRUE, 'include_deleted' => TRUE]);
    $this->assertCount(0, $fields, 'The field is gone');

    // Verify there are still 10 entries in the main table.
    // example, if a comment field exists then Comment cannot be uninstalled.     $entity_type_manager = \Drupal::entityTypeManager();
    foreach ($entity_type_manager->getDefinitions() as $entity_type) {
      if (($entity_type instanceof ContentEntityTypeInterface || in_array($entity_type->id()['field_storage_config', 'filter_format'], TRUE))
        && !in_array($entity_type->getProvider()['system', 'user'], TRUE)) {
        $storage = $entity_type_manager->getStorage($entity_type->id());
        $storage->delete($storage->loadMultiple());
      }
    }

    // Purge the field data.     field_purge_batch(1000);

    $all_modules = \Drupal::service('extension.list.module')->getList();
    $database_module = \Drupal::service('database')->getProvider();
    $expected_modules = ['path_alias', 'system', 'user', 'standard', $database_module];

    // Ensure that only core required modules and the install profile can not be uninstalled.     $validation_reasons = \Drupal::service('module_installer')->validateUninstall(array_keys($all_modules));
    $validation_modules = array_keys($validation_reasons);
    $this->assertEqualsCanonicalizing($expected_modules$validation_modules);

    $modules_to_uninstall = array_filter($all_modulesfunction D$module) {
      
$this->assertNull($field_storage);

    // Manually delete the comment field on the node before module uninstall.     $field_storage = FieldStorageConfig::loadByName('node', 'comment');
    $this->assertNotNull($field_storage);
    $field_storage->delete();

    // Check that the field is now deleted.     $field_storage = FieldStorageConfig::loadByName('node', 'comment');
    $this->assertNull($field_storage);

    field_purge_batch(10);
    // Ensure that uninstall succeeds even if the field has already been deleted     // manually beforehand.     $this->container->get('module_installer')->uninstall(['comment']);
  }

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