Skip to content
Snippets Groups Projects
Commit cb920508 authored by Chris Green's avatar Chris Green Committed by Ignacio Sánchez Holgueras
Browse files

Issue #3324621 by trackleft2, joegraduate, ericjenkins, darktek, utcwebdev,...

Issue #3324621 by trackleft2, joegraduate, ericjenkins, darktek, utcwebdev, timwood, ankitjhakal, devkinetic, dabblela, kurttrowbridge, pameeela, pcate, lisastreeter: Make version indicator source and version indicator source fallback configurable
parent f98dd03a
No related branches found
No related tags found
1 merge request!44Resolve #3324621 Make version indicator source and version indicator source fallback configurable.
Pipeline #368285 passed with warnings
toolbar_integration:
- toolbar
favicon: true
version_identifier: 'environment_indicator_current_release'
version_identifier_fallback: 'deployment_identifier'
......@@ -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'
<?php
/**
* @file
* Install, update and uninstall functions for the Environment Indicator module.
*/
/**
* Initialize the version_identifier and version_identifier_fallback settings for the environment indicator.
*/
function environment_indicator_update_8101() {
$config_factory = \Drupal::configFactory();
$settings = $config_factory->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();
}
......@@ -16,13 +16,27 @@ use Drupal\environment_indicator\ToolbarHandler;
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 <a href=":permissions">access environment indicator</a> 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 = '<p>' . t('<strong>Deployment Identifier:</strong> 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 <code>settings.php</code> file or through other mechanisms in your deployment process.') . '</p>';
$version_identifier_info = '<p>' . t('<strong>Source of Version Identifier to Display:</strong> You can configure the source of the version identifier to display in the environment indicator through the <a href=":settings">settings page</a>. The available options are:', [
':settings' => $settings_url,
]) . '</p>';
$version_identifier_info .= '<ul>';
$version_identifier_info .= '<li><strong>' . t('Environment Indicator Current Release (Default):') . '</strong> ' . t('Set the "environment_indicator.current_release" state using Drush: <code>drush sset environment_indicator.current_release v1.2.44</code>.') . '</li>';
$version_identifier_info .= '<li><strong>' . t('Deployment Identifier:') . '</strong> ' . t('Typically set in the <code>settings.php</code> file or through your deployment process.') . '</li>';
$version_identifier_info .= '<li><strong>' . t('Drupal Version:') . '</strong> ' . t('Displays the current Drupal version.') . '</li>';
$version_identifier_info .= '<li><strong>' . t('None:') . '</strong> ' . t('No version identifier is displayed.') . '</li>';
$version_identifier_info .= '</ul>';
$fallback_identifier_info = '<p>' . t('<strong>Fallback Source of Version Identifier to Display:</strong> 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.') . '</p>';
switch ($route_name) {
case 'environment_indicator.settings':
$output = '<p>' . t('The Environment Indicator adds a coloured strip to the site informing you which environment you are currently in (Development, Staging, Production, etc.)') . '</p>';
$output .= '<p>' . t('The Environment Indicator\'s visibility depends upon the permissions of the viewer. The <a href=":permissions">access environment indicator</a> permission must be enabled for a user role in order for users of that role to see the indicator.', [
':permissions' => $permissions_url,
]) . '</p>';
$output .= '<p>' . 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: <code>drush sset environment_indicator.current_release v1.2.44</code>') . '</p>';
$output .= '<p>' . $access_permission_text . '</p>';
$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 = '<p>' . t('The Environment Indicator adds a coloured strip to the site informing you which environment you are currently in (Development, Staging, Production, etc.') . '</p>';
$output .= '<p>' . t('The Environment Indicator <a href=":settings">settings page</a> 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]) . '</p>';
$output .= '<p>' . t('The Environment Indicator\'s visibility depends upon the permissions of the viewer. The <a href=":permissions">access environment indicator</a> permission must be enabled for a user role in order for users of that role to see the indicator.', [':permissions' => $permissions_url]) . '</p>';
$output = '<p>' . t('The Environment Indicator adds a coloured strip to the site informing you which environment you are currently in (Development, Staging, Production, etc.)') . '</p>';
$output .= '<p>' . t('The Environment Indicator <a href=":settings">settings page</a> 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,
]) . '</p>';
$output .= '<p>' . $access_permission_text . '</p>';
$output .= '<p>' . t('Modify the "environment_indicator.indicator" configuration object to control how the environment indicator is presented:') . '</p>';
$output .= '<dl>';
$output .= '<dt><em>$config[\'environment_indicator.indicator\'][\'bg_color\']</em></dt><dd>';
......@@ -46,7 +62,9 @@ function environment_indicator_help($route_name, RouteMatchInterface $route_matc
$output .= t('The text that will be displayed on the indicator.') . '<br/>';
$output .= '$config[\'environment_indicator.indicator\'][\'name\'] = \'Staging\';<br /></dd></dt>';
$output .= '</dl>';
$output .= '<p>' . 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: <code>drush sset environment_indicator.current_release v1.2.44</code>') . '</p>';
$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'] ?? '';
}
/**
......
......@@ -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 @@ class EnvironmentIndicatorSettingsForm extends ConfigFormBase implements FormInt
*/
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 @@ class EnvironmentIndicatorSettingsForm extends ConfigFormBase implements FormInt
'#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' => '<div id="version-identifier-fallback-wrapper">',
'#suffix' => '</div>',
'#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 @@ class EnvironmentIndicatorSettingsForm extends ConfigFormBase implements FormInt
*/
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);
}
}
......@@ -117,6 +117,7 @@ class ToolbarHandler {
* User can access a specific indicator.
*
* @param $environment
* The environment identifier.
*
* @return bool
*/
......@@ -215,24 +216,53 @@ class ToolbarHandler {
}
/**
* 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 @@ class ToolbarHandler {
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment