diff --git a/config/install/environment_indicator.settings.yml b/config/install/environment_indicator.settings.yml
index 1817ad5af7dbc57222bc43f585634329f93a29c7..21e84910ce40095af5877a23b69a9beff3c08c69 100644
--- a/config/install/environment_indicator.settings.yml
+++ b/config/install/environment_indicator.settings.yml
@@ -1,3 +1,5 @@
toolbar_integration:
- toolbar
favicon: true
+version_identifier: 'environment_indicator_current_release'
+version_identifier_fallback: 'deployment_identifier'
diff --git a/config/schema/environment_indicator.schema.yml b/config/schema/environment_indicator.schema.yml
index 9b75be7e08f0ffe527bc79b08024d149a81b1148..f133e03541ab3f5fa2f3fa3589ce6bc8bf94300c 100644
--- a/config/schema/environment_indicator.schema.yml
+++ b/config/schema/environment_indicator.schema.yml
@@ -39,17 +39,25 @@ 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: 'environment_indicator_current_release'
+ version_identifier_fallback:
+ type: string
+ label: 'Fallback 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..cb82231623bf8d188ca69c102b2c2f678d12569b
--- /dev/null
+++ b/environment_indicator.install
@@ -0,0 +1,26 @@
+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', 'environment_indicator_current_release');
+ }
+
+ // Initialize version_identifier_fallback with the old default fallback value.
+ if ($settings->get('version_identifier_fallback') === NULL) {
+ $settings->set('version_identifier_fallback', 'deployment_identifier');
+ }
+
+ $settings->save();
+}
diff --git a/environment_indicator.module b/environment_indicator.module
index 03fb99ac0965205bbe50d3ab4b006e4d966e399f..f419d2b610257b5e8d24da8744e686a17d6a89f0 100644
--- a/environment_indicator.module
+++ b/environment_indicator.module
@@ -16,13 +16,27 @@
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('Environment Indicator Current Release (Default):') . ' ' . t('Set the "environment_indicator.current_release" state using Drush:
drush sset environment_indicator.current_release v1.2.44.') . ' ';
+ $version_identifier_info .= '- ' . t('Deployment Identifier:') . ' ' . t('Typically set in the
settings.php file or through your deployment process.') . ' ';
+ $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 .= '
';
+ $fallback_identifier_info = '' . t('Fallback Source of Version Identifier to Display: Configure a fallback source for the version identifier to ensure version information is always available. Selecting "none" for the primary source disallows a fallback source.') . '
';
+
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 .= $fallback_identifier_info;
+ $output .= $deployment_identifier_info;
return $output;
case 'entity.environment_indicator.collection':
@@ -31,9 +45,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 +62,9 @@ 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 .= $fallback_identifier_info;
+ $output .= $deployment_identifier_info;
return $output;
}
@@ -170,7 +188,7 @@ function template_preprocess_environment_indicator(array &$variables) {
$attributes['style'] = implode('; ', $data);
$attributes['id'] = (empty($attributes['id']) ? (empty($attributes['#id']) ? 'environment-indicator' : $attributes['#id']) : $attributes['id']);
$variables['attributes'] = $attributes;
- $variables['switcher'] = isset($element['switcher']) ? $element['switcher'] : '';
+ $variables['switcher'] = $element['switcher'] ?? '';
}
/**
diff --git a/src/Form/EnvironmentIndicatorSettingsForm.php b/src/Form/EnvironmentIndicatorSettingsForm.php
index 2031394174ab6496ede4615f881b67929b069ed5..2cb8ddc706480b2b74dca73308d61f3626aa9f7e 100644
--- a/src/Form/EnvironmentIndicatorSettingsForm.php
+++ b/src/Form/EnvironmentIndicatorSettingsForm.php
@@ -2,14 +2,13 @@
namespace Drupal\environment_indicator\Form;
-use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Basic Environment Indicator controls form.
*/
-class EnvironmentIndicatorSettingsForm extends ConfigFormBase implements FormInterface {
+class EnvironmentIndicatorSettingsForm extends ConfigFormBase {
/**
* {@inheritdoc}
@@ -23,7 +22,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,15 +34,68 @@ 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,
];
+
+ $version_identifier_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'),
+ ];
+
+ $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' => $version_identifier_options,
+ '#default_value' => $config->get('version_identifier') ?: 'deployment_identifier',
+ '#ajax' => [
+ 'callback' => '::updateFallbackOptions',
+ 'event' => 'change',
+ 'wrapper' => 'version-identifier-fallback-wrapper',
+ ],
+ ];
+
+ $version_identifier = $form_state->getValue('version_identifier', $config->get('version_identifier') ?: 'deployment_identifier');
+
+ if ($version_identifier === 'none') {
+ $fallback_options = ['none' => $this->t('None')];
+ }
+ else {
+ $fallback_options = array_diff_key($version_identifier_options, [$version_identifier => '']);
+ }
+
+ $form['version_identifier_fallback'] = [
+ '#type' => 'select',
+ '#title' => $this->t('Fallback source of version identifier to display'),
+ '#description' => $this->t('Select the fallback source of the version identifier to display in the environment indicator.'),
+ '#options' => $fallback_options,
+ '#default_value' => $config->get('version_identifier_fallback') ?: 'none',
+ '#prefix' => '',
+ '#suffix' => '
',
+ '#states' => [
+ 'visible' => [
+ ':input[name="version_identifier"]' => ['!value' => 'none']
+ ]
+ ],
+ ];
+
return $form;
}
+ /**
+ * AJAX callback to update the fallback options.
+ */
+ public function updateFallbackOptions(array &$form, FormStateInterface $form_state) {
+ return $form['version_identifier_fallback'];
+ }
+
/**
* {@inheritdoc}
*/
@@ -54,12 +108,15 @@ 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'))
+ ->set('version_identifier_fallback', $form_state->getValue('version_identifier_fallback'))
+ ->save();
+
parent::submitForm($form, $form_state);
+
}
}
diff --git a/src/ToolbarHandler.php b/src/ToolbarHandler.php
index c2cccc7c0eef6b43558236e3456e620bdfdd7999..d5a3ea4c8c25a69bd0f8bc7c26ef71b99cb53762 100644
--- a/src/ToolbarHandler.php
+++ b/src/ToolbarHandler.php
@@ -117,6 +117,7 @@ public function hasAccessAll(): bool {
* User can access a specific indicator.
*
* @param $environment
+ * The environment identifier.
*
* @return bool
*/
@@ -215,24 +216,53 @@ 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');
+ $version_identifier_fallback = $this->config->get('version_identifier_fallback');
+
+ $release = $this->getVersionIdentifier($version_identifier);
+ if ($release !== NULL) {
+ return $release;
}
- $deployment_identifier = $this->settings->get('deployment_identifier');
- if ($deployment_identifier !== NULL) {
- return (string) $deployment_identifier;
+ if ($version_identifier !== $version_identifier_fallback) {
+ return $this->getVersionIdentifier($version_identifier_fallback);
}
return NULL;
}
+ /**
+ * Helper function to get version identifier based on the type.
+ *
+ * @param string $type
+ * The type of version identifier.
+ *
+ * @return string|null
+ */
+ protected function getVersionIdentifier(string $type): ?string {
+ switch ($type) {
+ case 'environment_indicator_current_release':
+ $current_release = $this->state->get('environment_indicator.current_release');
+ return $current_release !== NULL ? (string) $current_release : NULL;
+
+ case 'deployment_identifier':
+ $deployment_identifier = $this->settings->get('deployment_identifier');
+ return $deployment_identifier !== NULL ? (string) $deployment_identifier : NULL;
+
+ case 'drupal_version':
+ return \Drupal::VERSION;
+
+ case 'none':
+ default:
+ return NULL;
+ }
+ }
+
/**
* Construct the title for the active environment.
*
@@ -324,3 +354,4 @@ function (EnvironmentIndicator $entity) use ($current_path) {
}
}
+