diff --git a/config/install/environment_indicator.settings.yml b/config/install/environment_indicator.settings.yml
index 1817ad5af7dbc57222bc43f585634329f93a29c7..800635760d5cea09f5c53f2491fea6fb5ee5705a 100644
--- a/config/install/environment_indicator.settings.yml
+++ b/config/install/environment_indicator.settings.yml
@@ -1,3 +1,4 @@
toolbar_integration:
- toolbar
favicon: true
+version_identifier: 'deployment_identifier'
diff --git a/config/schema/environment_indicator.schema.yml b/config/schema/environment_indicator.schema.yml
index 9b75be7e08f0ffe527bc79b08024d149a81b1148..0acf638a1efecdda721dee5ee9c55e1e5cd15d6d 100644
--- a/config/schema/environment_indicator.schema.yml
+++ b/config/schema/environment_indicator.schema.yml
@@ -39,17 +39,21 @@ environment_indicator.settings:
favicon:
type: boolean
label: 'Show a colored favicon for environment'
+ version_identifier:
+ type: string
+ label: 'Source of version identifier to display'
+ default_value: 'deployment_identifier'
environment_indicator.indicator:
- type: config_object
- label: 'Environment indicator local environment'
- mapping:
- name:
- type: string
- label: 'Environment name'
- fg_color:
- type: string
- label: 'Foreground color'
- bg_color:
- type: string
- label: 'Background color'
+ type: config_object
+ label: 'Environment indicator local environment'
+ mapping:
+ name:
+ type: string
+ label: 'Environment name'
+ fg_color:
+ type: string
+ label: 'Foreground color'
+ bg_color:
+ type: string
+ label: 'Background color'
diff --git a/environment_indicator.install b/environment_indicator.install
new file mode 100644
index 0000000000000000000000000000000000000000..4631de31456f387a7e8d9f4b0d9908b6bf2ff5a6
--- /dev/null
+++ b/environment_indicator.install
@@ -0,0 +1,20 @@
+getEditable('environment_indicator.settings');
+
+ // Check if the version_identifier setting exists, and set a default value if it does not.
+ if ($settings->get('version_identifier') === NULL) {
+ $settings->set('version_identifier', 'deployment_identifier');
+ $settings->save();
+ }
+}
diff --git a/environment_indicator.module b/environment_indicator.module
index 4d19b074f4254b4acafadbf396da90a5864d2467..73afbe6505bd92b2462ecf5280a1951d4bd50bff 100644
--- a/environment_indicator.module
+++ b/environment_indicator.module
@@ -16,13 +16,25 @@
function environment_indicator_help($route_name, RouteMatchInterface $route_match) {
$permissions_url = Url::fromRoute('user.admin_permissions', [], ['fragment' => 'module-environment_indicator'])->toString();
$settings_url = Url::fromRoute('environment_indicator.settings')->toString();
+ // Define reusable variables for common strings and links.
+ $access_permission_text = t('The Environment Indicator\'s visibility depends upon the permissions of the viewer. The access environment indicator permission must be enabled for a user role in order for users of that role to see the indicator.', [':permissions' => $permissions_url]);
+ $deployment_identifier_info = '
' . t('Deployment Identifier: The deployment identifier mechanism is provided by Drupal core and is used by various hosting providers. It is a string that can be used to identify a particular deployment of your site. This is useful for identifying the specific code or configuration version currently deployed. The deployment identifier can be set in the settings.php file or through other mechanisms in your deployment process.') . '
';
+ $version_identifier_info = '' . t('Source of Version Identifier to Display: You can configure the source of the version identifier to display in the environment indicator through the settings page. The available options are:', [
+ ':settings' => $settings_url,
+ ]) . '
';
+ $version_identifier_info .= '';
+ $version_identifier_info .= '- ' . t('Deployment Identifier (Default):') . ' ' . t('Typically set in the
settings.php file or through your deployment process.') . ' ';
+ $version_identifier_info .= '- ' . t('Environment Indicator Current Release:') . ' ' . t('Set the "environment_indicator.current_release" state using Drush:
drush sset environment_indicator.current_release v1.2.44.') . ' ';
+ $version_identifier_info .= '- ' . t('Drupal Version:') . ' ' . t('Displays the current Drupal version.') . '
';
+ $version_identifier_info .= '- ' . t('None:') . ' ' . t('No version identifier is displayed.') . '
';
+ $version_identifier_info .= '
';
+
switch ($route_name) {
case 'environment_indicator.settings':
$output = '' . t('The Environment Indicator adds a coloured strip to the site informing you which environment you are currently in (Development, Staging, Production, etc.)') . '
';
- $output .= '' . t('The Environment Indicator\'s visibility depends upon the permissions of the viewer. The access environment indicator permission must be enabled for a user role in order for users of that role to see the indicator.', [
- ':permissions' => $permissions_url,
- ]) . '
';
- $output .= '' . t('The recommended way to add information about your release is to set the "environment_indicator.current_release" state. Use your git hooks to set the state using drush: drush sset environment_indicator.current_release v1.2.44') . '
';
+ $output .= '' . $access_permission_text . '
';
+ $output .= $version_identifier_info;
+ $output .= $deployment_identifier_info;
return $output;
case 'entity.environment_indicator.collection':
@@ -31,9 +43,11 @@ function environment_indicator_help($route_name, RouteMatchInterface $route_matc
return $output;
case 'help.page.environment_indicator':
- $output = '' . t('The Environment Indicator adds a coloured strip to the site informing you which environment you are currently in (Development, Staging, Production, etc.') . '
';
- $output .= '' . t('The Environment Indicator settings page allows you to modify some elements of the indicator\'s behavior and appearance. Since the appearance of the indicator is dependent on your site theme, substantial customisations require modifications to your site\'s theme and CSS files.', [':settings' => $settings_url]) . '
';
- $output .= '' . t('The Environment Indicator\'s visibility depends upon the permissions of the viewer. The access environment indicator permission must be enabled for a user role in order for users of that role to see the indicator.', [':permissions' => $permissions_url]) . '
';
+ $output = '' . t('The Environment Indicator adds a coloured strip to the site informing you which environment you are currently in (Development, Staging, Production, etc.)') . '
';
+ $output .= '' . t('The Environment Indicator settings page allows you to modify some elements of the indicator\'s behavior and appearance. Since the appearance of the indicator is dependent on your site theme, substantial customisations require modifications to your site\'s theme and CSS files.', [
+ ':settings' => $settings_url,
+ ]) . '
';
+ $output .= '' . $access_permission_text . '
';
$output .= '' . t('Modify the "environment_indicator.indicator" configuration object to control how the environment indicator is presented:') . '
';
$output .= '';
$output .= '- $config[\'environment_indicator.indicator\'][\'bg_color\']
- ';
@@ -46,7 +60,8 @@ function environment_indicator_help($route_name, RouteMatchInterface $route_matc
$output .= t('The text that will be displayed on the indicator.') . '
';
$output .= '$config[\'environment_indicator.indicator\'][\'name\'] = \'Staging\';
';
$output .= '
';
- $output .= '' . t('The recommended way to add information about your release is to set the "environment_indicator.current_release" state. Use your git hooks to set the state using drush: drush sset environment_indicator.current_release v1.2.44') . '
';
+ $output .= $version_identifier_info;
+ $output .= $deployment_identifier_info;
return $output;
}
diff --git a/src/Form/EnvironmentIndicatorSettingsForm.php b/src/Form/EnvironmentIndicatorSettingsForm.php
index 2031394174ab6496ede4615f881b67929b069ed5..8730d76b4ae08a96d8412e6d55e8fa932e786ce3 100644
--- a/src/Form/EnvironmentIndicatorSettingsForm.php
+++ b/src/Form/EnvironmentIndicatorSettingsForm.php
@@ -2,14 +2,41 @@
namespace Drupal\environment_indicator\Form;
-use Drupal\Core\Form\FormInterface;
+use Drupal\Core\Cache\CacheFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Basic Environment Indicator controls form.
*/
-class EnvironmentIndicatorSettingsForm extends ConfigFormBase implements FormInterface {
+class EnvironmentIndicatorSettingsForm extends ConfigFormBase {
+
+ /**
+ * The cache factory service.
+ *
+ * @var \Drupal\Core\Cache\CacheFactoryInterface
+ */
+ protected $cacheFactory;
+
+ /**
+ * Constructs a new EnvironmentIndicatorSettingsForm object.
+ *
+ * @param \Drupal\Core\Cache\CacheFactoryInterface $cacheFactory
+ * The cache factory service.
+ */
+ public function __construct(CacheFactoryInterface $cacheFactory) {
+ $this->cacheFactory = $cacheFactory;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function create(ContainerInterface $container) {
+ return new static(
+ $container->get('cache_factory')
+ );
+ }
/**
* {@inheritdoc}
@@ -23,7 +50,9 @@ public function getFormId() {
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('environment_indicator.settings');
+
$form = parent::buildForm($form, $form_state);
+
$form['toolbar_integration'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Toolbar integration'),
@@ -33,12 +62,27 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#description' => $this->t('Select the toolbars that you want to integrate with.'),
'#default_value' => $config->get('toolbar_integration') ?: [],
];
+
$form['favicon'] = [
'#type' => 'checkbox',
'#title' => $this->t('Show favicon'),
'#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,
];
+
+ $form['version_identifier'] = [
+ '#type' => 'select',
+ '#title' => $this->t('Source of version identifier to display'),
+ '#description' => $this->t('Select the source of the version identifier to display in the environment indicator.'),
+ '#options' => [
+ 'environment_indicator_current_release' => $this->t('Environment Indicator Current Release'),
+ 'deployment_identifier' => $this->t('Deployment Identifier'),
+ 'drupal_version' => $this->t('Drupal Version'),
+ 'none' => $this->t('None'),
+ ],
+ '#default_value' => $config->get('version_identifier') ?: 'deployment_identifier',
+ ];
+
return $form;
}
@@ -54,12 +98,16 @@ protected function getEditableConfigNames() {
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->config('environment_indicator.settings');
- $properties = ['toolbar_integration', 'favicon'];
- array_walk($properties, function ($property) use ($config, $form_state) {
- $config->set($property, $form_state->getValue($property));
- });
- $config->save();
+
+ $config->set('toolbar_integration', array_filter($form_state->getValue('toolbar_integration')))
+ ->set('favicon', $form_state->getValue('favicon'))
+ ->set('version_identifier', $form_state->getValue('version_identifier'))
+ ->save();
+
parent::submitForm($form, $form_state);
+
+ $this->cacheFactory->get('render')->deleteAll();
+ $this->cacheFactory->get('discovery')->deleteAll();
}
}
diff --git a/src/ToolbarHandler.php b/src/ToolbarHandler.php
index d3363db274374d73216105d37abb9abd956f725e..fdd313ea15ee6dbf6692740517edaaf53b517374 100644
--- a/src/ToolbarHandler.php
+++ b/src/ToolbarHandler.php
@@ -67,10 +67,16 @@ class ToolbarHandler implements ContainerInjectionInterface {
/**
* ToolbarHandler constructor.
*
+ * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
+ * The module handler service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\Session\AccountProxyInterface $account
* The current user.
+ * @param \Drupal\Core\State\StateInterface $state
+ * The state system.
+ * @param \Drupal\Core\Site\Settings $settings
+ * The settings.
*/
public function __construct(ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory, AccountProxyInterface $account, StateInterface $state, Settings $settings) {
$this->moduleHandler = $module_handler;
@@ -107,6 +113,7 @@ public function hasAccessAll(): bool {
* User can access a specific indicator.
*
* @param $environment
+ * The environment identifier.
*
* @return bool
*/
@@ -200,19 +207,34 @@ public function toolbar(): array {
}
/**
- * Retrieve the current release from the state or deployment_identifier.
+ * Retrieve value from the selected version identifier source.
*
* @return string|null
*/
- public function getCurrentRelease() {
- $current_release = $this->state->get('environment_indicator.current_release');
- if ($current_release !== NULL) {
- return (string) $current_release;
- }
+ public function getCurrentRelease(): ?string {
+ $version_identifier = $this->config->get('version_identifier');
+
+ switch ($version_identifier) {
+ case 'environment_indicator_current_release':
+ $current_release = $this->state->get('environment_indicator.current_release');
+ if ($current_release !== NULL) {
+ return (string) $current_release;
+ }
+ break;
+
+ case 'deployment_identifier':
+ $deployment_identifier = $this->settings->get('deployment_identifier');
+ if ($deployment_identifier !== NULL) {
+ return (string) $deployment_identifier;
+ }
+ break;
+
+ case 'drupal_version':
+ return \Drupal::VERSION;
- $deployment_identifier = $this->settings->get('deployment_identifier');
- if ($deployment_identifier !== NULL) {
- return (string) $deployment_identifier;
+ case 'none':
+ default:
+ return NULL;
}
return NULL;
@@ -318,3 +340,4 @@ function (EnvironmentIndicator $entity) use ($current_path) {
return $links;
}
}
+