Skip to content
Snippets Groups Projects

Issue #3210116 Patch 17 updates

Open dshumaker requested to merge issue/environment_indicator-3210116:4.x into 4.x
6 files
+ 261
6
Compare changes
  • Side-by-side
  • Inline
Files
6
@@ -2,15 +2,79 @@
namespace Drupal\environment_indicator\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
use Drupal\environment_indicator\Service\EnvironmentIndicatorServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Basic Environment Indicator controls form.
*/
class EnvironmentIndicatorSettingsForm extends ConfigFormBase implements FormInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The Drupal state service.
*
* @var Drupal\Core\State\StateInterface
*/
protected $state;
/**
* The environment indicator service.
*
* @var \Drupal\environment_indicator\Service\EnvironmentIndicatorServiceInterface
*/
protected $environmentIndicatorService;
/**
* Constructs an EnvironmentIndicatorSettingsForm instance.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The configuration factory service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
* @param \Drupal\Core\State\StateInterface $state
* The Drupal state service.
* @param \Drupal\environment_indicator\Service\EnvironmentIndicatorServiceInterface $environmentIndicatorService
* The environment indiator service.
*/
public function __construct(
ConfigFactoryInterface $configFactory,
EntityTypeManagerInterface $entityTypeManager,
StateInterface $state,
EnvironmentIndicatorServiceInterface $environmentIndicatorService
) {
parent::__construct($configFactory);
$this->entityTypeManager = $entityTypeManager;
$this->state = $state;
$this->environmentIndicatorService = $environmentIndicatorService;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('entity_type.manager'),
$container->get('state'),
$container->get('environment_indicator.service')
);
}
/**
* {@inheritdoc}
*/
@@ -39,6 +103,28 @@ class EnvironmentIndicatorSettingsForm extends ConfigFormBase implements FormInt
'#description' => $this->t('If checked, a favicon will be added with the environment colors when the indicator is shown.'),
'#default_value' => $config->get('favicon') ?: FALSE,
];
$environments = $this->entityTypeManager->getStorage('environment_indicator')->loadMultiple();
$options = ['' => $this->t('-- SELECT --')];
foreach ($environments as $environment) {
$options[$environment->id()] = $environment->label();
}
if (!empty($options)) {
// Note - can't use dependency injection here as it does not contain
// overriden values in settings.php, which is the specific value that
// needs to be checked. If this value does not exist, the value saved in
// the state is used instead. This is because the value in settings.php
// takes precedence over the value saved in state.
$disabled = !empty(\Drupal::config('environment_indicator.indicator')->get('current_environment'));
$form['current_environment'] = [
'#type' => 'select',
'#title' => $this->t('Current Environment'),
'#options' => $options,
'#default_value' => $this->environmentIndicatorService->getCurrentEnvironment(),
'#disabled' => $disabled,
'#description' => $this->t("This value is stored outside configuration, and must be set manually on each environment. Note that if <code>\$config['environment_indicator.indicator']['current_environment']</code> is set in <code>local.settings.php</code>, that value is used and this input is disabled."),
];
}
return $form;
}
@@ -59,6 +145,13 @@ class EnvironmentIndicatorSettingsForm extends ConfigFormBase implements FormInt
$config->set($property, $form_state->getValue($property));
});
$config->save();
if ($current_environment = $form_state->getValue('current_environment')) {
$this->state->set('evironment_indicator.current', $current_environment);
}
else {
$this->state->delete('evironment_indicator.current');
}
parent::submitForm($form, $form_state);
}
Loading