You are here

public function ConfigSelector::selectConfig in Configuration selector 8

Selects configuration to enable and disable after installing a module.

Return value

$this

See also

config_selector_modules_installed()

File

src/ConfigSelector.php, line 210

Class

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

Namespace

Drupal\config_selector

Code

public function selectConfig() {
  $new_configuration_list = array_diff($this->configFactory
    ->listAll(), $this->state
    ->get('config_selector.current_config_list', []));

  // Build a list of feature names of the configuration that's been imported.
  $features = [];
  foreach ($new_configuration_list as $config_name) {

    /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $config_entity */
    $config_entity = $this->configManager
      ->loadConfigEntityByName($config_name);
    if (!$config_entity) {

      // Simple configuration is ignored.
      continue;
    }
    if (!$config_entity
      ->status()) {

      // Disabled configuration is ignored.
      continue;
    }
    $feature = $config_entity
      ->getThirdPartySetting('config_selector', 'feature');
    if ($feature !== NULL) {
      $features[] = $feature;
    }
  }

  // It is possible that the module or profile installed has multiple
  // configurations for the same feature.
  $features = array_unique($features);

  // Process each feature and choose the configuration with the highest
  // priority.
  foreach ($features as $feature) {
    $entity_storage = $this->entityTypeManager
      ->getStorage($config_entity
      ->getEntityTypeId());
    $matching_config = $entity_storage
      ->getQuery()
      ->condition('third_party_settings.config_selector.feature', $feature)
      ->condition('status', FALSE, '<>')
      ->execute();

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

    // The last member of the array has the highest priority and should remain
    // enabled.
    $active_config = array_pop($configs);
    foreach ($configs as $config) {
      $config
        ->setStatus(FALSE)
        ->save();
      $variables = [
        ':disabled_config_href' => static::getConfigEntityLink($config),
        '@disabled_config_label' => $config
          ->label(),
        ':active_config_href' => static::getConfigEntityLink($active_config),
        '@active_config_label' => $active_config
          ->label(),
      ];
      $this->logger
        ->info('Configuration <a href=":disabled_config_href">@disabled_config_label</a> has been disabled in favor of <a href=":active_config_href">@active_config_label</a>.', $variables);
      $this
        ->drupalSetMessage($this
        ->t('Configuration <a href=":disabled_config_href">@disabled_config_label</a> has been disabled in favor of <a href=":active_config_href">@active_config_label</a>.', $variables));
    }
  }
  return $this;
}