Switcher.php in Configuration selector 8.2
File
src/Controller/Switcher.php
View source
<?php
namespace Drupal\config_selector\Controller;
use Drupal\config_selector\ConfigSelector;
use Drupal\config_selector\ConfigSelectorSortTrait;
use Drupal\config_selector\Entity\FeatureInterface;
use Drupal\Core\Controller\ControllerBase;
class Switcher extends ControllerBase {
use ConfigSelectorSortTrait;
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(),
]);
$config_entity = $this
->getConfigEntity($config_selector_feature, $config_entity_type, $config_entity_id);
if (!$config_entity) {
return $redirect;
}
$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;
}
protected function getConfigEntity(FeatureInterface $config_selector_feature, $config_entity_type, $config_entity_id) {
$config_entity = $this
->entityTypeManager()
->getStorage($config_entity_type)
->load($config_entity_id);
if (!$config_entity) {
$this
->messenger()
->addWarning($this
->t('Configuration entity of type %type and ID $id does not exist.', [
'%type' => $config_entity_type,
'%id' => $config_entity_id,
]));
return FALSE;
}
if ($config_entity
->getThirdPartySetting('config_selector', 'feature') !== $config_selector_feature
->id()) {
$this
->messenger()
->addWarning($this
->t('Configuration entity %config_label is not part of the %feature_label feature.', [
'%config_label' => $config_entity
->label(),
'%feature_label' => $config_selector_feature
->label(),
]));
return FALSE;
}
return $config_entity;
}
}
Classes
Name |
Description |
Switcher |
Allows the UI to switch between different configuration entities. |