getConfigEntitiesToChangeOnDependencyRemoval example

$this->assertNull($this->entityStorage->deleteRevision(1));
  }

  /** * @covers ::delete * @covers ::doDelete */
  public function testDelete() {
    // Dependencies are tested in     // \Drupal\Tests\config\Kernel\ConfigDependencyTest.     $this->configManager
      ->getConfigEntitiesToChangeOnDependencyRemoval('config', ['the_provider.the_config_prefix.foo'], FALSE)
      ->willReturn(['update' => [], 'delete' => [], 'unchanged' => []]);
    $this->configManager
      ->getConfigEntitiesToChangeOnDependencyRemoval('config', ['the_provider.the_config_prefix.bar'], FALSE)
      ->willReturn(['update' => [], 'delete' => [], 'unchanged' => []]);

    $entities = [];
    foreach (['foo', 'bar'] as $id) {
      $entity = $this->getMockEntity(['id' => $id]);
      $entities[] = $entity;

      $config_object = $this->prophesize(Config::class);
      
->willReturn([
        'permission name' => [
          'title' => 'permission display name',
          'provider' => 'some module',
          'dependencies' => ['config' => [$dependency_name]],
        ],
      ]);
    $permission_handler = $prophecy->reveal();
    $role_storage = $this->prophesize(RoleStorageInterface::class)->reveal();
    $module_handler = $this->prophesize(ModuleHandlerInterface::class)->reveal();
    $prophecy = $this->prophesize(ConfigManagerInterface::class);
    $prophecy->getConfigEntitiesToChangeOnDependencyRemoval('config', ['node.type.article'])
      ->willReturn([
        'delete' => [
          new ConfigEntityDependency('core.entity_view_display.node.article.full'),
          new ConfigEntityDependency('field.field.node.article.body'),
        ],
      ]);
    $config_manager = $prophecy->reveal();
    $prophecy = $this->prophesize(EntityTypeInterface::class);
    $prophecy->getPermissionGranularity()
      ->willReturn('entity_type');
    $entity_type = $prophecy->reveal();
    

      ]
    );
    $entity_5->save();

    // Set a more complicated test where dependencies will be fixed.     \Drupal::state()->set('config_test.fix_dependencies', [$entity_1->getConfigDependencyName()]);
    \Drupal::state()->set('config_test.on_dependency_removal_called', []);

    // Do a dry run using     // \Drupal\Core\Config\ConfigManager::getConfigEntitiesToChangeOnDependencyRemoval().     $config_entities = $config_manager->getConfigEntitiesToChangeOnDependencyRemoval('module', ['node']);

    // Assert that \Drupal\config_test\Entity\ConfigTest::onDependencyRemoval()     // is called as expected and with the correct dependencies.     $called = \Drupal::state()->get('config_test.on_dependency_removal_called', []);
    $this->assertArrayNotHasKey($entity_3->id()$called, 'ConfigEntityInterface::onDependencyRemoval() is not called for entity 3.');
    $this->assertSame([$entity_1->id()$entity_4->id()$entity_2->id()$entity_5->id()]array_keys($called), 'The most dependent entities have ConfigEntityInterface::onDependencyRemoval() called first.');
    $this->assertSame(['config' => [], 'content' => [], 'module' => ['node'], 'theme' => []]$called[$entity_1->id()]);
    $this->assertSame(['config' => [$entity_1->getConfigDependencyName()], 'content' => [], 'module' => [], 'theme' => []]$called[$entity_2->id()]);
    $this->assertSame(['config' => [$entity_1->getConfigDependencyName()], 'content' => [], 'module' => ['node'], 'theme' => []]$called[$entity_4->id()]);
    $this->assertSame(['config' => [$entity_1->getConfigDependencyName()], 'content' => [], 'module' => [], 'theme' => []]$called[$entity_5->id()]);

    
/** * {@inheritdoc} */
  public function createSnapshot(StorageInterface $source_storage, StorageInterface $snapshot_storage) {
    self::replaceStorageContents($source_storage$snapshot_storage);
  }

  /** * {@inheritdoc} */
  public function uninstall($type$name) {
    $entities = $this->getConfigEntitiesToChangeOnDependencyRemoval($type[$name], FALSE);
    // Fix all dependent configuration entities.     /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity */
    foreach ($entities['update'] as $entity) {
      $entity->save();
    }
    // Remove all dependent configuration entities.     foreach ($entities['delete'] as $entity) {
      $entity->setUninstalling(TRUE);
      $entity->delete();
    }

    

  protected function addDependencyListsToForm(array &$form$type, array $names, ConfigManagerInterface $config_manager, EntityTypeManagerInterface $entity_type_manager) {
    // Get the dependent entities.     $dependent_entities = $config_manager->getConfigEntitiesToChangeOnDependencyRemoval($type$names);
    $entity_types = [];

    $form['entity_updates'] = [
      '#type' => 'details',
      '#title' => $this->t('Configuration updates'),
      '#description' => $this->t('The listed configuration will be updated.'),
      '#open' => TRUE,
      '#access' => FALSE,
    ];

    foreach ($dependent_entities['update'] as $entity) {
      
$container->get('entity_type.manager')
    );
  }

  /** * {@inheritdoc} */
  protected function permissionsByProvider(): array {
    // Get the names of all config entities that depend on $this->bundle.     $config_name = $this->bundle->getConfigDependencyName();
    $config_entities = $this->configManager
      ->getConfigEntitiesToChangeOnDependencyRemoval('config', [$config_name]);
    $config_names = array_map(
      function D$dependent_config) {
        return $dependent_config->getConfigDependencyName();
      }$config_entities['delete'] ?? []
    );
    $config_names[] = $config_name;

    // Find all the permissions that depend on $this->bundle.     $permissions = $this->permissionHandler->getPermissions();
    $permissions_by_provider = [];
    foreach ($permissions as $permission_name => $permission) {
      

  public static function preDelete(EntityStorageInterface $storage, array $entities) {
    parent::preDelete($storage$entities);

    foreach ($entities as $entity) {
      if ($entity->isUninstalling() || $entity->isSyncing()) {
        // During extension uninstall and configuration synchronization         // deletions are already managed.         break;
      }
      // Fix or remove any dependencies.       $config_entities = static::getConfigManager()->getConfigEntitiesToChangeOnDependencyRemoval('config', [$entity->getConfigDependencyName()], FALSE);
      /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $dependent_entity */
      foreach ($config_entities['update'] as $dependent_entity) {
        $dependent_entity->save();
      }
      foreach ($config_entities['delete'] as $dependent_entity) {
        $dependent_entity->delete();
      }
    }
  }

  /** * Gets the configuration manager. * * @return \Drupal\Core\Config\ConfigManager * The configuration manager. */
Home | Imprint | This part of the site doesn't use cookies.