You are here

public function ConfigSelector::selectConfigOnUninstall in Configuration selector 8.2

Same name and namespace in other branches
  1. 8 src/ConfigSelector.php \Drupal\config_selector\ConfigSelector::selectConfigOnUninstall()

Selects configuration to enable after uninstalling a module.

Return value

$this

See also

config_selector_modules_uninstalled()

File

src/ConfigSelector.php, line 191

Class

ConfigSelector
Selects configuration to enable after a module install or uninstall.

Namespace

Drupal\config_selector

Code

public function selectConfigOnUninstall() {
  $features = $this->state
    ->get('config_selector.feature_uninstall_list', []);
  foreach ($features as $config_entity_id => $feature) {
    $entity_type_id = $this->configManager
      ->getEntityTypeIdByName($config_entity_id);
    if (!$entity_type_id) {

      // The entity type no longer exists there will not be any replacement
      // config.
      continue;
    }

    // Get all the possible configuration for the feature.
    $entity_storage = $this->entityTypeManager
      ->getStorage($entity_type_id);
    $matching_config = $entity_storage
      ->getQuery()
      ->condition('third_party_settings.config_selector.feature', $feature)
      ->execute();

    /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface[] $configs */
    $configs = $entity_storage
      ->loadMultiple($matching_config);
    $this
      ->sortConfigEntities($configs);

    // If any of the configuration is enabled there is nothing to do here.
    foreach ($configs as $config) {
      if ($config
        ->status()) {
        continue 2;
      }
    }

    // No configuration is enabled. Enable the highest priority one.
    $highest_priority_config = array_pop($configs);
    $highest_priority_config
      ->setStatus(TRUE)
      ->save();
    $variables = [
      ':active_config_href' => static::getConfigEntityLink($highest_priority_config),
      '@active_config_label' => $highest_priority_config
        ->label(),
    ];
    $this->logger
      ->info('Configuration <a href=":active_config_href">@active_config_label</a> has been enabled.', $variables);
    $this->messenger
      ->addStatus($this
      ->t('Configuration <a href=":active_config_href">@active_config_label</a> has been enabled.', $variables));
  }

  // Reset the list.
  $this->state
    ->set('config_selector.feature_uninstall_list', []);
  return $this;
}