You are here

public function Switcher::select in Configuration selector 8.2

Same name and namespace in other branches
  1. 8 src/Controller/Switcher.php \Drupal\config_selector\Controller\Switcher::select()

Selects the supplied configuration entity.

Parameters

\Drupal\config_selector\Entity\FeatureInterface $config_selector_feature: The Configuration selector feature.

string $config_entity_type: The entity type of the configuration entity we are switching to.

string $config_entity_id: The ID of the configuration entity we are switching to.

Return value

\Symfony\Component\HttpFoundation\RedirectResponse This always redirects to the feature's manage route.

1 string reference to 'Switcher::select'
config_selector.routing.yml in ./config_selector.routing.yml
config_selector.routing.yml

File

src/Controller/Switcher.php, line 29

Class

Switcher
Allows the UI to switch between different configuration entities.

Namespace

Drupal\config_selector\Controller

Code

public function select(FeatureInterface $config_selector_feature, $config_entity_type, $config_entity_id) {
  $redirect = $this
    ->redirect('entity.config_selector_feature.manage', [
    'config_selector_feature' => $config_selector_feature
      ->id(),
  ]);

  /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $config_entity */
  $config_entity = $this
    ->getConfigEntity($config_selector_feature, $config_entity_type, $config_entity_id);
  if (!$config_entity) {
    return $redirect;
  }

  // Enable the entity and disable the others.
  $config_entity
    ->setStatus(TRUE);
  $config_entity
    ->save();
  $entities = $config_selector_feature
    ->getConfigurationByType($config_entity_type);
  unset($entities[$config_entity
    ->id()]);
  $args = [
    ':enabled_link' => ConfigSelector::getConfigEntityLink($config_entity),
    '@enabled_label' => $config_entity
      ->label(),
  ];
  foreach ($entities as $entity) {
    if ($entity
      ->status()) {
      $entity
        ->setStatus(FALSE);
      $entity
        ->save();
      $args[':disabled_link'] = ConfigSelector::getConfigEntityLink($entity);
      $args['@disabled_label'] = $entity
        ->label();
      $this
        ->getLogger('config_selector')
        ->info('Configuration entity <a href=":disabled_link">@disabled_label</a> has been disabled in favor of <a href=":enabled_link">@enabled_label</a>.', $args);
    }
  }
  $this
    ->messenger()
    ->addStatus($this
    ->t('Configuration entity <a href=":enabled_link">@enabled_label</a> has been selected.', $args));
  return $redirect;
}