clean install

This commit is contained in:
matze committed 2024-12-10 15:08:16 +01:00
commit e14eb2d8fd
31193 files changed
+3555714

No files matched your search

@@ -0,0 +1,171 @@
# Configuration Update Reports
* Introduction
* Installation
* Generating reports in the user interface
* Generating reports using Drush commands
* Important notes *** Be sure to read this section ***
## Introduction
This module provides a report that allows you to see the differences between the
default configuration items provided by the current versions of your installed
modules, themes, and install profile, and the active configuration of your
site. From this report, you can also import new configuration provided by
updates, and revert your site configuration to the provided values.
The main use case is: You update a module, and it has either changed default
configuration that it provides, or added new default configuration items that
you didn't get when you first installed the module. You want to be able to
import the new items, view the differences between the active site configuration
and the changed configuration, and possibly "revert" (or it may be an update) to
the newly-provided default configuration.
## Installation
Install the module in the normal way for Drupal modules. The only dependencies
are the Configuration Manager module (Drupal core), and the Configuration Update
Base module (part of this same project download).
## Generating reports in the user interface
You can generate configuration reports at Administration >> Configuration >>
Development >> Configuration management >> Update report (path:
admin/config/development/configuration/report ).
You can generate a report for a particular type of configuration object, such as
Actions, Tours, Views, etc. Or, you can generate a report for an installed
module, theme, or install profile. Finally, you can generate a report that
contains all configuration in one report.
The report has three sections, depending on what type you choose:
1. Missing configuration items: Configuration items that are provided as
defaults by your currently-installed modules, themes, and install profile
that are missing from your active site configuration.
Any items listed here can be imported into your site.
2. Added configuration items: Configuration items that you added to the site
(not provided by a currently-installed module, theme, or install
profile). This section is only shown when you are running the report based on
a configuration type.
Items listed here can be exported, which is useful for developers or if you
want to keep your site configuration in a version control system.
3. Changed configuration items: Configuration items that are in your active site
configuration that differ from the same item currently being provided by an
installed module, theme, or install profile.
You can export these items, see the differences between what is on your site
and what the module/theme/profile is currently providing, or "revert" to the
version currently being provided by the module/theme/profile in its default
configuration.
Note that the differences may be a bit hard to read, but hopefully they'll
give you the general idea of what has changed.
## Generating reports using Drush commands
The reports detailed in the previous section can also be generated, in pieces,
using Drush commands (https://drupal.org/project/drush):
drush config-list-types (clt)
Lists all the config types on your system. Reports can be run for
'system.simple' (simple configuration), and 'system.all' (all types), in
addition to the types listed by this command.
drush config-added-report (cra)
drush config-missing-report (crm)
drush config-different-report (crd)
drush config-inactive-report (cri)
Run config reports (see below).
drush config-diff (cfd)
Show config differences for one item between active and imported (see below).
The report commands run reports that tell what config has been added, is
missing, is inactive, or is different between your active site configuration and
the imported default configuration from config/install directories of your
installed profile, modules, and themes.
For each report except "added", the first argument is one of:
- type: Runs the report for a configuration type; use drush config-list-types to
list them.
- module: Runs the report for an installed module.
- theme: Runs the report for an installed theme.
- profile: Runs the report for the install profile.
The second argument for reports is the machine name of the configuration type,
module, theme, or install profile you want to run the report for. For the
"added" report, this is the only argument, as the added report is always by
configuration type.
These are the same as the reports you get in the UI, which is described above;
the difference is that in Drush the report is separated into pieces, and the
UI report sections have more columns giving information about each config item.
Once you have found a configuration item with differences, you can view the
differences using the config-diff command. This is a normalized/formatted diff
like in the UI of this module, so see above for details.
Drush examples:
drush clt
drush crm module node
drush cra block
drush crd theme olivero
drush crd type system.all
drush crd type system.simple
drush crd profile standard
drush cfd block.block.olivero_search
Once you have figured out which configuration items are added, missing, or
different, you can:
- Export them - see drush config-export.
- Import missing configuration or revert to provided default values. There are
Drush commands to do this:
drush config-revert (cfr)
Revert a config item found in a "different" report to the current version in
the module, theme, or install profile that provides it.
drush config-revert-multiple (cfrm)
Revert all config items of one type or from one module, theme, or install
profile to the provided versions.
drush config-import-missing (cfi)
Import a config item found in a "missing" or "inactive" report from the
current version in the module, theme, or install profile that provides it.
You can also to use the drush config-import command to import/update multiple
configuration items. But this requires that you locate the files containing the
configuration you want to import, and copy them to your configuration staging
directory.
## Important notes
Here are some notes about how this module functions:
* This module is always looking at the base configuration items, without
overrides (from settings.php, for example) or translations.
* It is possible for an install profile on a site to provide configuration that
overrides configuration from a module or theme. The install profile version
always takes precedence. As an example, consider the case where module Foo
provides a configuration item called foo.settings, and install profile Bar
overrides this with its own file. Any reports that include foo.settings will
be based on the differences between your site's active configuration and the
version in the install profile. This is not usually a problem, but it can be
confusing if you're looking at the Foo module report. The foo.settings item
will be present, but the differences reported will be between the install
profile's version and your site's active configuration, not the differences
between the Foo module version and your site's active configuration.
@@ -0,0 +1,9 @@
config_update_ui.settings:
type: config_entity
label: 'Configuration Update Reports settings'
mapping:
ignore:
type: sequence
label: 'Ignored configuration items'
sequence:
type: string
@@ -0,0 +1,14 @@
name: 'Configuration Update Reports'
type: module
description: 'Adds an updates report and revert functionality to configuration management.'
core_version_requirement: ^9.4 || ^10 || ^11
php: 8.1
configure: config_update_ui.report
dependencies:
- config_update:config_update
- drupal:config
# Information added by Drupal.org packaging script on 2024-08-25
version: '2.0.0-alpha4'
project: 'config_update'
datestamp: 1724596933
@@ -0,0 +1,5 @@
report_css:
version: 1.x
css:
theme:
css/config_update_ui.report.css: {}
@@ -0,0 +1,5 @@
config_update_ui.report:
route_name: config_update_ui.report
title: 'Updates report'
base_route: config.sync
weight: 1000
@@ -0,0 +1,54 @@
<?php
/**
* @file
* Configuration Update Reports module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
/**
* Implements hook_help().
*/
function config_update_ui_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.config_update_ui':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Configuration Update Reports module provides a report that allows you to see the differences between the default configuration provided by the current versions of your installed modules, themes, and install profile, and the active configuration of your site. From this report, you can also import new configuration provided by updates, and revert your active configuration to the provided default values.') . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Checking for configuration differences') . '</dd>';
$output .= '<dd>' . t('The <a href=":report">Updates report</a> shows you configuration that differs between your site\'s active configuration and the installed modules, themes, and install profile. Choose a particular configuration type (or All), or run the report for a particular module, theme, or your install profile.', [':report' => Url::fromRoute('config_update_ui.report')->toString()]) . '</dd>';
$output .= '</dl>';
return $output;
case 'config_update_ui.report':
$output = '';
$output .= '<p>' . t('This report shows which default configuration items provided by the current versions of your installed modules, themes, and install profile differ from the active configuration of your site. You can generate the report for all configuration of a certain type; for a particular installed module, theme, or your install profile (only modules, themes, and install profiles that actually provide configuration are listed); or for all configuration.') . '</p>';
$output .= '<p>' . t('Some configuration provided by modules has dependencies, and is only added to your system when all the dependencies are satisfied. This is known as <em>optional</em> configuration.') . '</p>';
$output .= '<p>' . t('Differences, including missing and added configuration, can be due to either an updated module or theme providing different configuration from when you installed it, or to changes made by site administrators. This report does not differentiate between these two sources for differences, and note that an install profile can also silently override module/theme configuration. Overrides (such as those in your settings.php file) and translations are not considered when looking at differences.') . '</p>';
return $output;
case 'config_update_ui.diff':
return '<p>' . t('Most configuration is organized into a hierarchy of settings; at a minimum, it is a one-level hierarchy where each setting has a name and a value, and the hierarchy comes in when some of the settings have multiple components.') . '</p><p>' . t('Configuration items are normalized and formatted before computing differences. The normalization step alphabetizes the components at each level of the hierarchy, and removes a few components whose differences should be ignored, such as the UUID. The formatting step shows the full hierarchy of each configuration value with :: separators for the hierarchy levels, and a : separator between the lowest-level setting name and the value, so that in a line-by-line diff you can always see which values are actually different. Green lines with + signs have been added, and yellow lines with - signs have been removed.') . '</p><p>' . t('Note that differences are considering the base configuration, without overrides from your settings.php file, or translations.') . '</p>';
}
}
/**
* Implements hook_menu_links_discovered_alter().
*/
function config_update_ui_menu_links_discovered_alter(&$links) {
if (\Drupal::moduleHandler()->moduleExists('admin_toolbar_tools')) {
// Add the Updates report link to the Tools menu, but only if the
// Admin Toolbar Extra Tools module is present.
$links['admin_toolbar_tools.config.update'] = [
'title' => t('Updates report'),
'route_name' => 'config_update_ui.report',
'menu_name' => 'admin',
'parent' => 'config.sync',
'weight' => 3,
];
}
}
@@ -0,0 +1,8 @@
view config updates report:
title: 'View configuration updates report'
revert configuration:
title: 'Revert any configuration'
restrict access: true
delete configuration:
title: 'Delete any configuration'
restrict access: true
@@ -0,0 +1,43 @@
config_update_ui.report:
path: '/admin/config/development/configuration/report/{report_type}/{name}'
defaults:
_controller: '\Drupal\config_update_ui\Controller\ConfigUpdateController::report'
_title: 'Updates report'
report_type: NULL
name: NULL
requirements:
_permission: 'view config updates report'
config_update_ui.import:
path: '/admin/config/development/configuration/report/import/{config_type}/{config_name}'
defaults:
_title: 'Import'
_form: '\Drupal\config_update_ui\Form\ConfigImportConfirmForm'
requirements:
_permission: 'import configuration'
config_update_ui.diff:
path: '/admin/config/development/configuration/report/diff/{config_type}/{config_name}'
defaults:
_title: 'Differences'
_controller: '\Drupal\config_update_ui\Controller\ConfigUpdateController::diff'
config_type: NULL
config_name: NULL
requirements:
_permission: 'view config updates report'
config_update_ui.revert:
path: '/admin/config/development/configuration/report/revert/{config_type}/{config_name}'
defaults:
_title: 'Revert'
_form: '\Drupal\config_update_ui\Form\ConfigRevertConfirmForm'
requirements:
_permission: 'revert configuration'
config_update_ui.delete:
path: '/admin/config/development/configuration/report/delete/{config_type}/{config_name}'
defaults:
_title: 'Delete'
_form: '\Drupal\config_update_ui\Form\ConfigDeleteConfirmForm'
requirements:
_permission: 'delete configuration'
@@ -0,0 +1,13 @@
/**
* @file
* Styling for the Config Updates report.
*/
table.config-update-report {
margin-top: 2em;
}
table.config-update-report caption {
font-size: 1.1em;
font-weight: bold;
}
@@ -0,0 +1,6 @@
services:
config_update_ui.commands:
class: \Drupal\config_update_ui\Commands\ConfigUpdateUiCommands
arguments: ['@entity_type.manager', '@config_update.config_diff', '@config_update.config_list', '@config_update.config_update']
tags:
- { name: drush.command }
@@ -0,0 +1,407 @@
<?php
namespace Drupal\config_update_ui\Commands;
use Drupal\Component\Diff\DiffFormatter;
use Drupal\config_update\ConfigDiffer;
use Drupal\config_update\ConfigListerWithProviders;
use Drupal\config_update\ConfigReverter;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drush\Commands\DrushCommands;
use Drush\Drush;
/**
* A set of Drush commands for Config Update Manager.
*/
class ConfigUpdateUiCommands extends DrushCommands {
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected EntityTypeManagerInterface $entityManager;
/**
* The config differ.
*
* @var \Drupal\config_update\ConfigDiffer
*/
protected ConfigDiffer $configDiff;
/**
* The config lister.
*
* @var \Drupal\config_update\ConfigListerWithProviders
*/
protected ConfigListerWithProviders $configList;
/**
* The config reverter.
*
* @var \Drupal\config_update\ConfigReverter
*/
protected ConfigReverter $configUpdate;
/**
* Constructs a ConfigUpdateUiCommands object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityManager
* The entity type manager.
* @param \Drupal\config_update\ConfigDiffer $configDiff
* The config differ.
* @param \Drupal\config_update\ConfigListerWithProviders $configList
* The config lister.
* @param \Drupal\config_update\ConfigReverter $configUpdate
* The config reverter.
*/
public function __construct(EntityTypeManagerInterface $entityManager, ConfigDiffer $configDiff, ConfigListerWithProviders $configList, ConfigReverter $configUpdate) {
parent::__construct();
$this->entityManager = $entityManager;
$this->configDiff = $configDiff;
$this->configList = $configList;
$this->configUpdate = $configUpdate;
$this->logger = Drush::logger();
}
/**
* Lists config types.
*
* @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
* A structured data object of rows of configuration types.
*
* @command config:list-types
* @aliases clt,config-list-types
*/
public function listTypes() {
$definitions = $this->configList->listTypes();
return array_keys($definitions);
}
/**
* Displays added config items.
*
* Displays a list of config items that did not come from your installed
* modules, themes, or install profile.
*
* @param string $name
* The type of config to report on. See config-list-types to list them.
* You can also use system.all for all types, or system.simple for simple
* config.
*
* @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
* A structured data object of rows of added configuration items.
*
* @usage drush config-added-report action
* Displays the added config report for action config.
*
* @command config:added-report
* @aliases cra,config-added-report
*/
public function addedReport(string $name) {
[$activeList, $installList, $optionalList] = $this->configList->listConfig('type', $name);
$addedItems = array_diff($activeList, $installList, $optionalList);
if (!count($addedItems)) {
$this->logger->success(dt('No added config.'));
}
sort($addedItems);
return $addedItems;
}
/**
* Displays missing config items.
*
* Displays a list of config items from your installed modules, themes, or
* install profile that are not currently in your active config.
*
* @param string $type
* Run the report for: module, theme, profile, or "type" for config entity
* type.
* @param string $name
* The machine name of the module, theme, etc. to report on. See
* config-list-types to list types for config entities; you can also use
* system.all for all types, or system.simple for simple config.
*
* @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
* A structured data object of rows of missing configuration items.
*
* @usage drush config-missing-report type action
* Displays the missing config report for action config.
*
* @command config:missing-report
* @aliases crm,config-missing-report
*/
public function missingReport(string $type, string $name) {
[$activeList, $installList] = $this->configList->listConfig($type, $name);
$missingItems = array_diff($installList, $activeList);
if (!count($missingItems)) {
$this->logger->success(dt('No missing config.'));
}
sort($missingItems);
return $missingItems;
}
/**
* Displays optional config items.
*
* Displays a list of optional config items from your installed modules,
* themes, or install profile that are not currently in your active config.
*
* @param string $type
* Run the report for: module, theme, profile, or "type" for config entity
* type.
* @param string $name
* The machine name of the module, theme, etc. to report on. See
* config-list-types to list types for config entities; you can also use
* system.all for all types, or system.simple for simple config.
*
* @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
* A structured data object of rows of inactive configuration items.
*
* @usage drush config-inactive-report type action
* Displays the inactive config report for action config.
*
* @command config:inactive-report
* @aliases cri,config-inactive-report
*/
public function inactiveReport(string $type, string $name) {
[$activeList, $optionalList] = $this->configList->listConfig($type, $name);
$inactiveItems = array_diff($optionalList, $activeList);
if (!count($inactiveItems)) {
$this->logger->success(dt('No inactive config.'));
}
sort($inactiveItems);
return $inactiveItems;
}
/**
* Displays differing config items.
*
* Displays a list of config items that differ from the versions provided by
* your installed modules, themes, or install profile. See config-diff to
* show what the differences are.
*
* @param string $type
* Run the report for: module, theme, profile, or "type" for config entity
* type.
* @param string $name
* The machine name of the module, theme, etc. to report on. See
* config-list-types to list types for config entities; you can also use
* system.all for all types, or system.simple for simple config.
*
* @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
* A structured data object of rows of differing configuration items.
*
* @usage drush config-different-report type action
* Displays the differing config report for action config.
*
* @command config:different-report
* @aliases crd,config-different-report
*/
public function differentReport(string $type, string $name) {
$differentItems = $this->getDifferentItems($type, $name);
if (!count($differentItems)) {
$this->logger->success(dt('No different config'));
}
return $differentItems;
}
/**
* Displays a diff of a config item.
*
* Displays line-by-line differences for one config item between your active
* config and the version currently being provided by an installed module,
* theme, or install profile.
*
* @param string $name
* The config item to diff. See config-different-report to list config
* items that are different.
*
* @return string
* The formatted diff output.
*
* @usage drush config-diff block.block.olivero_search
* Displays the config differences for the search block
* in the Olivero theme.
*
* @command config:diff
* @aliases cfd,config-diff
*/
public function diff(string $name): string {
$extension = $this->configUpdate->getFromExtension('', $name);
$active = $this->configUpdate->getFromActive('', $name);
if ($extension && $active) {
$diff = $this->configDiff->diff($extension, $active);
// Drupal\Component\Diff\DiffFormatter does not expose a service so we
// instantiate it manually here.
$diffFormatter = new DiffFormatter();
return $diffFormatter->format($diff);
}
$this->logger->error(dt('Config is missing, cannot diff.'));
return '';
}
/**
* Reverts a config item.
*
* Reverts one config item in active storage to the version provided by an
* installed module, theme, or install profile.
*
* @param string $name
* The config item to revert. See config-different-report to list config
* items that are different.
*
* @usage drush config-revert block.block.olivero_search
* Revert the config for the search block in the Olivero theme to the
* version provided by the install profile.
*
* @command config:revert
* @aliases cfr,config-revert
*/
public function revert(string $name) {
$type = $this->configList->getTypeNameByConfigName($name);
// The lister gives NULL if simple configuration, but the reverter expects
// 'system.simple' so we convert it.
if ($type === NULL) {
$type = 'system.simple';
}
$shortname = $this->getConfigShortname($type, $name);
if ($this->configUpdate->revert($type, $shortname)) {
$this->logger->success(dt('The configuration item @name was reverted to its source.', ['@name' => $name]));
}
else {
$this->logger->error(dt('There was an error and the configuration item @name was not reverted.', ['@name' => $name]));
}
}
/**
* Imports missing config item.
*
* Imports a missing or inactive config item provided by an installed module,
* theme, or install profile. Be sure that requirements are met.
*
* @param string $name
* The name of the config item to import (usually the ID you would see in
* the user interface). See config-missing-report to list config items that
* are missing, and config-inactive-report to list config items that are
* inactive.
*
* @usage drush config-import-missing block.block.olivero_search
* Import the config for the search block in the Olivero theme from the
* version provided by the install profile.
*
* @command config:import-missing
* @aliases cfi,config-import-missing
*/
public function importMissing(string $name) {
$type = $this->configList->getTypeNameByConfigName($name);
// The lister gives NULL if simple configuration, but the reverter expects
// 'system.simple' so we convert it.
if ($type === NULL) {
$type = 'system.simple';
}
$shortname = $this->getConfigShortname($type, $name);
if ($this->configUpdate->import($type, $shortname)) {
$this->logger->success(dt('The configuration item @name was imported from its source.', ['@name' => $name]));
}
else {
$this->logger->error(dt('There was an error and the configuration item @name was not imported.', ['@name' => $name]));
}
}
/**
* Reverts multiple config items to extension provided version.
*
* Reverts a set of config items to the versions provided by installed
* modules, themes, or install profiles. A set is all differing items from
* one extension, or one type of configuration.
*
* @param string $type
* Type of set to revert: "module" for all items from a module, "theme" for
* all items from a theme, "profile" for all items from the install profile,
* or "type" for all items of one config entity type. See
* config-different-report to list config items that are different.
* @param string $name
* The machine name of the module, theme, etc. to revert items of. All
* items in the corresponding config-different-report will be reverted.
*
* @usage drush config-revert-multiple type action
* Revert all differing config items of type action.
*
* @command config:revert-multiple
* @aliases cfrm,config-revert-multiple
*/
public function revertMultiple(string $type, string $name) {
$different = $this->getDifferentItems($type, $name);
foreach ($different as $name) {
$this->revert($name);
}
}
/**
* Lists differing config items.
*
* Lists config items that differ from the versions provided by your
* installed modules, themes, or install profile. See config-diff to show
* what the differences are.
*
* @param string $type
* Run the report for: module, theme, profile, or "type" for config entity
* type.
* @param string $name
* The machine name of the module, theme, etc. to report on. See
* config-list-types to list types for config entities; you can also use
* system.all for all types, or system.simple for simple config.
*
* @return array
* An array of differing configuration items.
*/
protected function getDifferentItems(string $type, string $name): array {
[$activeList, $installList, $optionalList] = $this->configList->listConfig($type, $name);
$addedItems = array_diff($activeList, $installList, $optionalList);
$activeAndAddedItems = array_diff($activeList, $addedItems);
$differentItems = [];
foreach ($activeAndAddedItems as $name) {
$active = $this->configUpdate->getFromActive('', $name);
$extension = $this->configUpdate->getFromExtension('', $name);
if (!$this->configDiff->same($active, $extension)) {
$differentItems[] = $name;
}
}
sort($differentItems);
return $differentItems;
}
/**
* Gets the config item shortname given the type and name.
*
* @param string $type
* The type of the config item.
* @param string $name
* The name of the config item.
*
* @return string
* The shortname for the configuration item.
*/
protected function getConfigShortname(string $type, string $name): string {
$shortname = $name;
if ($type != 'system.simple') {
$definition = $this->entityManager->getDefinition($type);
$prefix = $definition->getConfigPrefix() . '.';
if (str_starts_with($name, $prefix)) {
$shortname = substr($name, strlen($prefix));
}
}
return $shortname;
}
}
@@ -0,0 +1,718 @@
<?php
namespace Drupal\config_update_ui\Controller;
use Drupal\config_update\ConfigDiffInterface;
use Drupal\config_update\ConfigListByProviderInterface;
use Drupal\config_update\ConfigRevertInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Diff\DiffFormatter;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\Url;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Returns responses for Configuration Revert module operations.
*/
class ConfigUpdateController extends ControllerBase {
/**
* The config differ.
*
* @var \Drupal\config_update\ConfigDiffInterface
*/
protected $configDiff;
/**
* The config lister.
*
* @var \Drupal\config_update\ConfigListByProviderInterface
*/
protected $configList;
/**
* The config reverter.
*
* @var \Drupal\config_update\ConfigRevertInterface
*/
protected $configRevert;
/**
* The diff formatter.
*
* @var \Drupal\Core\Diff\DiffFormatter
*/
protected $diffFormatter;
/**
* The module extension list.
*
* @var \Drupal\Core\Extension\ModuleExtensionList
*/
protected $moduleExtensionList;
/**
* The theme handler.
*
* @var \Drupal\Core\Extension\ThemeHandlerInterface
*/
protected $themeHandler;
/**
* The config factory.
*
* @var \Drupal\Core\config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Logger interface.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* Constructs a ConfigUpdateController object.
*
* @param \Drupal\config_update\ConfigDiffInterface $config_diff
* The config differ.
* @param \Drupal\config_update\ConfigListByProviderInterface $config_list
* The config lister.
* @param \Drupal\config_update\ConfigRevertInterface $config_update
* The config reverter.
* @param \Drupal\Core\Diff\DiffFormatter $diff_formatter
* The diff formatter to use.
* @param \Drupal\Core\Extension\ModuleExtensionList $module_extension_list
* The module extension list.
* @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
* The theme handler.
* @param \drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Psr\Log\LoggerInterface $logger
* The logger instance.
*/
public function __construct(ConfigDiffInterface $config_diff, ConfigListByProviderInterface $config_list, ConfigRevertInterface $config_update, DiffFormatter $diff_formatter, ModuleExtensionList $module_extension_list, ThemeHandlerInterface $theme_handler, ConfigFactoryInterface $config_factory, LoggerInterface $logger) {
$this->configDiff = $config_diff;
$this->configList = $config_list;
$this->configRevert = $config_update;
$this->diffFormatter = $diff_formatter;
$this->diffFormatter->show_header = FALSE;
$this->moduleExtensionList = $module_extension_list;
$this->themeHandler = $theme_handler;
$this->configFactory = $config_factory;
$this->logger = $logger;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config_update.config_diff'),
$container->get('config_update.config_list'),
$container->get('config_update.config_update'),
$container->get('diff.formatter'),
$container->get('extension.list.module'),
$container->get('theme_handler'),
$container->get('config.factory'),
$container->get('logger.factory')->get('config_update_ui')
);
}
/**
* Shows the diff between active and provided configuration.
*
* @param string $config_type
* The type of configuration.
* @param string $config_name
* The name of the config item, without the prefix.
*
* @return array
* Render array for page showing differences between them.
*/
public function diff($config_type, $config_name) {
$diff = $this->configDiff->diff(
$this->configRevert->getFromExtension($config_type, $config_name),
$this->configRevert->getFromActive($config_type, $config_name)
);
$build = [];
$definition = $this->configList->getType($config_type);
$config_type_label = ($definition) ? $definition->getLabel() : $this->t('Simple configuration');
$build['#title'] = $this->t('Config difference for @type @name',
[
'@type' => $config_type_label,
'@name' => $config_name,
]
);
$build['#attached']['library'][] = 'system/diff';
$rows = $this->diffFormatter->format($diff);
$build['diff'] = [
'#type' => 'table',
'#header' => [
['data' => $this->t('Source config'), 'colspan' => '2'],
['data' => $this->t('Site config'), 'colspan' => '2'],
],
'#rows' => $rows,
'#empty' => $this->t('There are no changes.'),
'#attributes' => ['class' => ['diff']],
];
$url = new Url('config_update_ui.report');
$build['wrapper'] = [
'#type' => 'container',
'#attributes' => [
'class' => ['form-actions'],
],
];
$links = [];
if (!empty($rows)) {
$links['revert'] = [
'url' => Url::fromRoute('config_update_ui.revert',
[
'config_type' => $config_type,
'config_name' => $config_name,
]
),
'title' => $this->t('Revert to source'),
];
}
$links['export'] = [
'url' => Url::fromRoute('config.export_single', ['config_type' => $config_type, 'config_name' => $config_name]),
'title' => $this->t('Export'),
];
$links['delete'] = [
'url' => Url::fromRoute('config_update_ui.delete', ['config_type' => $config_type, 'config_name' => $config_name]),
'title' => $this->t('Delete'),
];
$build['wrapper']['operations'] = [
'#type' => 'dropbutton',
'#links' => $links,
];
$build['wrapper']['back'] = [
'#type' => 'link',
'#attributes' => [
'class' => [
'button',
],
],
'#title' => $this->t("Back to 'Updates report' page"),
'#url' => $url,
];
return $build;
}
/**
* Generates the config updates report.
*
* @param string $report_type
* (optional) Type of report to run:
* - type: Configuration entity type.
* - module: Module.
* - theme: Theme.
* - profile: Install profile.
* @param string $name
* (optional) Name of specific item to run report for (config entity type
* ID, module machine name, etc.). Ignored for profile.
*
* @return array
* Render array for report, with section at the top for selecting another
* report to run. If either $report_type or $name is missing, the report
* itself is not generated.
*/
public function report($report_type = NULL, $name = NULL) {
$links = $this->generateReportLinks();
$report = $this->generateReport($report_type, $name);
if (!$report) {
return $links;
}
// If there is a report, extract the title, put table of links in a
// details element, and add report to build.
$build = [];
$build['#title'] = $report['#title'];
unset($report['#title']);
$build['links_wrapper'] = [
'#type' => 'details',
'#title' => $this->t('Generate new report'),
'#children' => $links,
];
$build['report'] = $report;
$build['#attached']['library'][] = 'config_update/report_css';
return $build;
}
/**
* Generates the operations links for running individual reports.
*
* @return array
* Render array for the operations links for running reports.
*/
protected function generateReportLinks() {
// These links are put into an 'operations' render array element. They do
// not look good outside of tables. Also note that the array index in
// operations links is used as a class on the LI element. Some classes are
// special in the Seven CSS, such as "contextual", so avoid hitting these
// accidentally by prefixing.
$build = [];
$build['links'] = [
'#type' => 'table',
'#header' => [
$this->t('Report type'),
$this->t('Report on'),
],
'#rows' => [],
];
// Full report of all configuration.
$links['report_full'] = [
'title' => $this->t('Everything'),
'url' => Url::fromRoute('config_update_ui.report', ['report_type' => 'type', 'name' => 'system.all']),
];
$build['links']['#rows'][] = [
$this->t('Full report'),
[
'data' => [
'#type' => 'operations',
'#links' => $links,
],
],
];
// Reports by configuration type.
$definitions = $this->configList->listTypes();
$links = [];
foreach ($definitions as $entity_type => $definition) {
$links['report_type_' . $entity_type] = [
'title' => $definition->getLabel(),
'url' => Url::fromRoute('config_update_ui.report', ['report_type' => 'type', 'name' => $entity_type]),
];
}
uasort($links, [$this, 'sortLinks']);
$links = [
'report_type_system.simple' => [
'title' => $this->t('Simple configuration'),
'url' => Url::fromRoute('config_update_ui.report', ['report_type' => 'type', 'name' => 'system.simple']),
],
] + $links;
$build['links']['#rows'][] = [
$this->t('Single configuration type'),
[
'data' => [
'#type' => 'operations',
'#links' => $links,
],
],
];
// Make a list of installed modules.
$profile = $this->getProfileName();
$modules = $this->moduleExtensionList->getList();
$links = [];
foreach ($modules as $machine_name => $module) {
if ($machine_name != $profile && $this->configList->providerHasConfig('module', $machine_name)) {
$links['report_module_' . $machine_name] = [
'title' => $this->moduleExtensionList->getName($machine_name),
'url' => Url::fromRoute('config_update_ui.report', ['report_type' => 'module', 'name' => $machine_name]),
];
}
}
uasort($links, [$this, 'sortLinks']);
$build['links']['#rows'][] = [
$this->t('Single module'),
[
'data' => [
'#type' => 'operations',
'#links' => $links,
],
],
];
// Make a list of installed themes.
$themes = $this->themeHandler->listInfo();
$links = [];
foreach ($themes as $machine_name => $theme) {
if ($this->configList->providerHasConfig('theme', $machine_name)) {
$links['report_theme_' . $machine_name] = [
'title' => $this->themeHandler->getName($machine_name),
'url' => Url::fromRoute('config_update_ui.report', ['report_type' => 'theme', 'name' => $machine_name]),
];
}
}
uasort($links, [$this, 'sortLinks']);
$build['links']['#rows'][] = [
$this->t('Single theme'),
[
'data' => [
'#type' => 'operations',
'#links' => $links,
],
],
];
$links = [];
// Profile is just one option.
if ($this->configList->providerHasConfig('profile', $profile)) {
$links['report_profile_' . $profile] = [
'title' => $this->moduleExtensionList->getName($profile),
'url' => Url::fromRoute('config_update_ui.report', ['report_type' => 'profile']),
];
$build['links']['#rows'][] = [
$this->t('Installation profile'),
[
'data' => [
'#type' => 'operations',
'#links' => $links,
],
],
];
}
return $build;
}
/**
* Generates a report about config updates.
*
* @param string $report_type
* Type of report to generate: 'type', 'module', 'theme', or 'profile'.
* @param string $value
* Machine name of a configuration type, module, or theme to generate the
* report for. Ignored for profile, since that uses the active profile.
*
* @return array
* Render array for the updates report. Empty if invalid or missing
* report type or value.
*/
protected function generateReport($report_type, $value) {
// Figure out what to name the report, and incidentally, validate that
// $value exists for this type of report.
switch ($report_type) {
case 'type':
if ($value == 'system.all') {
$label = $this->t('All configuration');
}
elseif ($value == 'system.simple') {
$label = $this->t('Simple configuration');
}
else {
$definition = $this->configList->getType($value);
if (!$definition) {
return NULL;
}
$label = $this->t('@name configuration', ['@name' => $definition->getLabel()]);
}
break;
case 'module':
$list = $this->moduleExtensionList->getList();
if (!isset($list[$value])) {
return NULL;
}
$label = $this->t('@name module', ['@name' => $this->moduleExtensionList->getName($value)]);
break;
case 'theme':
$list = $this->themeHandler->listInfo();
if (!isset($list[$value])) {
return NULL;
}
$label = $this->t('@name theme', ['@name' => $this->themeHandler->getName($value)]);
break;
case 'profile':
$profile = $this->getProfileName();
$label = $this->t('@name profile', ['@name' => $this->moduleExtensionList->getName($profile)]);
break;
default:
return NULL;
}
// List the active and extension-provided config.
[$active_list, $install_list, $optional_list] = $this->configList->listConfig($report_type, $value);
// Build the report.
$build = [];
$build['#title'] = $this->t('Configuration updates report for @label', ['@label' => $label]);
$build['report_header'] = ['#markup' => '<h3>' . $this->t('Updates report') . '</h3>'];
// List items missing from site.
$removed = array_diff($install_list, $active_list);
$build['removed'] = [
'#caption' => $this->t('Missing configuration items'),
'#empty' => $this->t('None: all provided configuration items are in your active configuration.'),
] + $this->makeReportTable($removed, 'extension', ['import']);
// List optional items that are not installed.
$inactive = array_diff($optional_list, $active_list);
$build['inactive'] = [
'#caption' => $this->t('Inactive optional items'),
'#empty' => $this->t('None: all optional configuration items are in your active configuration.'),
] + $this->makeReportTable($inactive, 'extension', ['import']);
// List items added to site, which only makes sense in the report for a
// config type.
$added = array_diff($active_list, $install_list, $optional_list);
if ($report_type == 'type') {
$build['added'] = [
'#caption' => $this->t('Added configuration items'),
'#empty' => $this->t('None: all active configuration items of this type were provided by modules, themes, or install profile.'),
] + $this->makeReportTable($added, 'active', ['export', 'delete']);
}
// For differences, we need to go through the array of config in both
// and see if each config item is the same or not.
$both = array_diff($active_list, $added);
$different = [];
foreach ($both as $name) {
if (!$this->configDiff->same(
$this->configRevert->getFromExtension('', $name),
$this->configRevert->getFromActive('', $name)
)) {
$different[] = $name;
}
}
$build['different'] = [
'#caption' => $this->t('Changed configuration items'),
'#empty' => $this->t('None: no active configuration items differ from their current provided versions.'),
] + $this->makeReportTable($different, 'active',
['diff', 'export', 'revert', 'delete']);
return $build;
}
/**
* Builds a table for the report.
*
* @param string[] $names
* List of machine names of config items for the table.
* @param string $storage
* Config storage the items can be loaded from, either 'active' or
* 'extension'.
* @param string[] $actions
* Action links to include, one or more of:
* - diff
* - revert
* - export
* - import
* - delete.
*
* @return array
* Render array for the table, not including the #empty and #prefix
* properties.
*/
protected function makeReportTable(array $names, $storage, array $actions) {
$build = [];
$ignored_items = $this->configFactory->get('config_update_ui.settings')->get('ignore');
if (is_array($ignored_items)) {
$names = array_diff($names, $ignored_items);
}
$build['#type'] = 'table';
$build['#attributes'] = ['class' => ['config-update-report']];
$build['#header'] = [
'name' => [
'data' => $this->t('Machine name'),
],
'label' => [
'data' => $this->t('Label (if any)'),
'class' => [RESPONSIVE_PRIORITY_LOW],
],
'type' => [
'data' => $this->t('Type'),
'class' => [RESPONSIVE_PRIORITY_MEDIUM],
],
'provider' => [
'data' => $this->t('Provider'),
'class' => [RESPONSIVE_PRIORITY_LOW],
],
'operations' => [
'data' => $this->t('Operations'),
],
];
$build['#rows'] = [];
sort($names);
foreach ($names as $name) {
$row = [];
if ($storage == 'active') {
$config = $this->configRevert->getFromActive('', $name);
}
else {
$config = $this->configRevert->getFromExtension('', $name);
}
if (empty($config)) {
$this->logger->error('Malformed config file @config_name.', ['@config_name' => $name]);
continue;
}
// Figure out what type of config it is, and get the ID.
$entity_type = $this->configList->getTypeNameByConfigName($name);
if (!$entity_type) {
// This is simple config.
$id = $name;
$label = '';
$type_label = $this->t('Simple configuration');
$entity_type = 'system.simple';
}
else {
$definition = $this->configList->getType($entity_type);
$id_key = $definition->getKey('id');
$id = $config[$id_key];
// The label key is not required.
if ($label_key = $definition->getKey('label')) {
$label = $config[$label_key];
}
else {
$label = '';
}
$type_label = $definition->getLabel();
}
$row[] = $name;
$row[] = $label;
$row[] = $type_label;
$provider = $this->configList->getConfigProvider($name);
$provider_name = '';
if (!empty($provider)) {
switch ($provider[0]) {
case 'profile':
$provider_name = $this->moduleExtensionList->getName($provider[1]);
if ($provider_name) {
$provider_name = $this->t('@name profile', ['@name' => $provider_name]);
}
else {
$provider_name = '';
}
break;
case 'module':
$provider_name = $this->moduleExtensionList->getName($provider[1]);
if ($provider_name) {
$provider_name = $this->t('@name module', ['@name' => $provider_name]);
}
else {
$provider_name = '';
}
break;
case 'theme':
$provider_name = $this->themeHandler->getName($provider[1]);
if ($provider_name) {
$provider_name = $this->t('@name theme', ['@name' => $provider_name]);
}
else {
$provider_name = '';
}
break;
}
}
$row[] = $provider_name;
$links = [];
$routes = [
'export' => 'config.export_single',
'import' => 'config_update_ui.import',
'diff' => 'config_update_ui.diff',
'revert' => 'config_update_ui.revert',
'delete' => 'config_update_ui.delete',
];
$titles = [
'export' => $this->t('Export'),
'import' => $this->t('Import from source'),
'diff' => $this->t('Show differences'),
'revert' => $this->t('Revert to source'),
'delete' => $this->t('Delete'),
];
foreach ($actions as $action) {
$links[$action] = [
'url' => Url::fromRoute($routes[$action], ['config_type' => $entity_type, 'config_name' => $id]),
'title' => $titles[$action],
];
}
$row[] = [
'data' => [
'#type' => 'operations',
'#links' => $links,
],
];
$build['#rows'][] = $row;
}
return $build;
}
/**
* Compares links for uasort(), to sort by displayed link title.
*/
protected static function sortLinks($link1, $link2) {
$title1 = $link1['title'];
$title2 = $link2['title'];
if ($title1 == $title2) {
return 0;
}
return ($title1 < $title2) ? -1 : 1;
}
/**
* Returns the name of the install profile.
*
* For backwards compatibility with pre/post 8.3.x, tries to get it from
* either configuration or settings.
*
* @return string
* The name of the install profile.
*/
protected function getProfileName() {
// Code adapted from DrupalKernel::getInstalProfile() in Core.
// In Core 8.3.x or later, read from config.
$profile = $this->configFactory->get('core.extension')->get('profile');
if (!empty($profile)) {
return $profile;
}
else {
// If system_update_8300() has not yet run, use settings.
return Settings::get('install_profile');
}
}
}
@@ -0,0 +1,149 @@
<?php
namespace Drupal\config_update_ui\Form;
use Drupal\config_update\ConfigListInterface;
use Drupal\config_update\ConfigRevertInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Defines a confirmation form for deleting configuration.
*/
class ConfigDeleteConfirmForm extends ConfirmFormBase {
/**
* The type of config being deleted.
*
* @var string
*/
protected $type;
/**
* The name of the config item being deleted, without the prefix.
*
* @var string
*/
protected $name;
/**
* The config lister.
*
* @var \Drupal\config_update\ConfigListInterface
*/
protected $configList;
/**
* The config reverter.
*
* @var \Drupal\config_update\ConfigRevertInterface
*/
protected $configRevert;
/**
* Constructs a ConfigDeleteConfirmForm object.
*
* @param \Drupal\config_update\ConfigListInterface $config_list
* The config lister.
* @param \Drupal\config_update\ConfigRevertInterface $config_update
* The config reverter.
*/
public function __construct(ConfigListInterface $config_list, ConfigRevertInterface $config_update) {
$this->configList = $config_list;
$this->configRevert = $config_update;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config_update.config_list'),
$container->get('config_update.config_update')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'config_delete_confirm';
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
if ($this->type == 'system.simple') {
$type_label = $this->t('Simple configuration');
}
else {
$definition = $this->configList->getType($this->type);
if (!$definition) {
// Make a 404 error if the type doesn't exist.
throw new NotFoundHttpException();
}
$type_label = $definition->get('label');
}
// To delete, the configuration item must exist in active storage. Check
// that and make a 404 error if not.
$active = $this->configRevert->getFromActive($this->type, $this->name);
if (!$active) {
throw new NotFoundHttpException();
}
return $this->t('Are you sure you want to delete the %type config %item?',
[
'%type' => $type_label,
'%item' => $this->name,
]
);
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('config_update_ui.report');
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return $this->t('This action cannot be undone. Manually deleting configuration from this page can cause problems on your site due to missing dependencies, and should only be done if there is no other way to delete a problematic piece of configuration.');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete');
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $config_type = NULL, $config_name = NULL) {
$this->type = $config_type;
$this->name = $config_name;
$form = parent::buildForm($form, $form_state);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->configRevert->delete($this->type, $this->name);
$this->messenger()->addMessage($this->t('The configuration %item has been deleted.', ['%item' => $this->name]));
$form_state->setRedirectUrl($this->getCancelUrl());
}
}
@@ -0,0 +1,151 @@
<?php
namespace Drupal\config_update_ui\Form;
use Drupal\config_update\ConfigListInterface;
use Drupal\config_update\ConfigRevertInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Defines a confirmation form for importing configuration.
*/
class ConfigImportConfirmForm extends ConfirmFormBase {
/**
* The type of config being imported.
*
* @var string
*/
protected $type;
/**
* The name of the config item being imported, without the prefix.
*
* @var string
*/
protected $name;
/**
* The config lister.
*
* @var \Drupal\config_update\ConfigListInterface
*/
protected $configList;
/**
* The config reverter.
*
* @var \Drupal\config_update\ConfigRevertInterface
*/
protected $configRevert;
/**
* Constructs a ConfigImportConfirmForm object.
*
* @param \Drupal\config_update\ConfigListInterface $config_list
* The config lister.
* @param \Drupal\config_update\ConfigRevertInterface $config_update
* The config reverter.
*/
public function __construct(ConfigListInterface $config_list, ConfigRevertInterface $config_update) {
$this->configList = $config_list;
$this->configRevert = $config_update;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config_update.config_list'),
$container->get('config_update.config_update')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'config_import_confirm';
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
if ($this->type == 'system.simple') {
$type_label = $this->t('Simple configuration');
}
else {
$definition = $this->configList->getType($this->type);
if (!$definition) {
// Make a 404 error if the type doesn't exist.
throw new NotFoundHttpException();
}
$type_label = $definition->get('label');
}
// To import (as opposed to revert), the configuration item must exist in
// extension storage but not active storage, so check that, and make a 404
// error if not.
$extension = $this->configRevert->getFromExtension($this->type, $this->name);
$active = $this->configRevert->getFromActive($this->type, $this->name);
if (!$extension || $active) {
throw new NotFoundHttpException();
}
return $this->t('Are you sure you want to import the %type config %item from its source configuration?',
[
'%type' => $type_label,
'%item' => $this->name,
]
);
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('config_update_ui.report');
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return $this->t('Configuration will be added to your site. This action cannot be undone.');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Import');
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $config_type = NULL, $config_name = NULL) {
$this->type = $config_type;
$this->name = $config_name;
$form = parent::buildForm($form, $form_state);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->configRevert->import($this->type, $this->name);
$this->messenger()->addMessage($this->t('The configuration %item has been imported from its source.', ['%item' => $this->name]));
$form_state->setRedirectUrl($this->getCancelUrl());
}
}
@@ -0,0 +1,151 @@
<?php
namespace Drupal\config_update_ui\Form;
use Drupal\config_update\ConfigListInterface;
use Drupal\config_update\ConfigRevertInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Defines a confirmation form for reverting configuration.
*/
class ConfigRevertConfirmForm extends ConfirmFormBase {
/**
* The type of config being reverted.
*
* @var string
*/
protected $type;
/**
* The name of the config item being reverted, without the prefix.
*
* @var string
*/
protected $name;
/**
* The config lister.
*
* @var \Drupal\config_update\ConfigListInterface
*/
protected $configList;
/**
* The config reverter.
*
* @var \Drupal\config_update\ConfigRevertInterface
*/
protected $configRevert;
/**
* Constructs a ConfigRevertConfirmForm object.
*
* @param \Drupal\config_update\ConfigListInterface $config_list
* The config lister.
* @param \Drupal\config_update\ConfigRevertInterface $config_update
* The config reverter.
*/
public function __construct(ConfigListInterface $config_list, ConfigRevertInterface $config_update) {
$this->configList = $config_list;
$this->configRevert = $config_update;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config_update.config_list'),
$container->get('config_update.config_update')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'config_update_confirm';
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
if ($this->type == 'system.simple') {
$type_label = $this->t('Simple configuration');
}
else {
$definition = $this->configList->getType($this->type);
if (!$definition) {
// Make a 404 error if the type doesn't exist.
throw new NotFoundHttpException();
}
$type_label = $definition->get('label');
}
// To revert (as opposed to import), the configuration item must exist in
// both active storage and extension storage, so check that and make a 404
// error if not.
$extension = $this->configRevert->getFromExtension($this->type, $this->name);
$active = $this->configRevert->getFromActive($this->type, $this->name);
if (!$extension || !$active) {
throw new NotFoundHttpException();
}
return $this->t('Are you sure you want to revert the %type config %item to its source configuration?',
[
'%type' => $type_label,
'%item' => $this->name,
]
);
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('config_update_ui.report');
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return $this->t('Customizations will be lost. This action cannot be undone.');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Revert');
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $config_type = NULL, $config_name = NULL) {
$this->type = $config_type;
$this->name = $config_name;
$form = parent::buildForm($form, $form_state);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->configRevert->revert($this->type, $this->name);
$this->messenger()->addMessage($this->t('The configuration %item has been reverted to its source.', ['%item' => $this->name]));
$form_state->setRedirectUrl($this->getCancelUrl());
}
}
@@ -0,0 +1,101 @@
<?php
namespace Drupal\Tests\config_update_ui\Functional;
use Drupal\Tests\BrowserTestBase;
/**
* Verify config reports, reverts, and diffs with profile overrides.
*
* @group config_update
*/
class ConfigProfileOverridesTest extends BrowserTestBase {
/**
* Use the Standard profile, so that there are profile config overrides.
*
* @var string
*/
protected $profile = 'standard';
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'config',
'config_update',
'config_update_ui',
];
/**
* The admin user that will be created.
*
* @var object
*/
protected $adminUser;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Create user and log in.
$this->adminUser = $this->createUser([
'access administration pages',
'administer themes',
'view config updates report',
'synchronize configuration',
'export configuration',
'import configuration',
'revert configuration',
'delete configuration',
]);
$this->drupalLogin($this->adminUser);
}
/**
* Tests that config overrides work as expected.
*/
public function testConfigOverrides() {
// The Standard install profile contains a system.theme.yml file that
// sets up olivero/claro as the default/admin theme. The default config
// from the System module has no admin theme and stark as the default
// theme. So first, run the report on simple configuration and verify
// that system.theme is not shown (it should not be missing, or added,
// or overridden).
$this->drupalGet('admin/config/development/configuration/report/type/system.simple');
$session = $this->assertSession();
$session->responseNotContains('system.theme');
// Go to the Appearance page and change the theme to whatever is currently
// disabled. Return to the report and verify that system.theme is there,
// since it is now overridden.
$this->drupalGet('admin/appearance');
$this->clickLink('Install and set as default');
$this->drupalGet('admin/config/development/configuration/report/type/system.simple');
$session = $this->assertSession();
$session->pageTextContains('system.theme');
// Look at the differences for system.theme and verify it's against
// the standard profile version, not default version. The line for
// default should show olivero as the source; if it's against the system
// version, the word olivero would not be there.
$this->drupalGet('admin/config/development/configuration/report/diff/system.simple/system.theme');
$session = $this->assertSession();
$session->pageTextContains('olivero');
// Revert and verify that it reverted to the profile version, not the
// system module version.
$this->drupalGet('admin/config/development/configuration/report/revert/system.simple/system.theme');
$this->submitForm([], 'Revert');
$this->drupalGet('admin/config/development/configuration/single/export/system.simple/system.theme');
$session = $this->assertSession();
$session->pageTextContains('admin: claro');
$session->pageTextContains('default: olivero');
}
}
@@ -0,0 +1,415 @@
<?php
namespace Drupal\Tests\config_update_ui\Functional;
use Drupal\Tests\BrowserTestBase;
/**
* Verify the config revert report and its links.
*
* @group config_update
*/
class ConfigUpdateTest extends BrowserTestBase {
/**
* Default theme.
*
* @var string
*/
protected $defaultTheme = 'stark';
/**
* Modules to enable.
*
* Use the Search module because it has two included config items in its
* config/install, assuming node and user are also enabled.
*
* @var array
*/
protected static $modules = [
'config',
'config_update',
'config_update_ui',
'search',
'node',
'user',
'block',
'text',
'field',
'filter',
];
/**
* The admin user that will be created.
*
* @var object
*/
protected $adminUser;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Create user and log in.
$this->adminUser = $this->createUser([
'access administration pages',
'administer search',
'view config updates report',
'synchronize configuration',
'export configuration',
'import configuration',
'revert configuration',
'delete configuration',
'administer filters',
]);
$this->drupalLogin($this->adminUser);
// Make sure local tasks and page title are showing.
$this->placeBlock('local_tasks_block');
$this->placeBlock('page_title_block');
}
/**
* Tests the config report and its linked pages.
*/
public function testConfigReport() {
// Test links to report page.
$this->drupalGet('admin/config/development/configuration');
$this->clickLink('Updates report');
$this->assertNoReport();
// Verify some empty reports.
$this->drupalGet('admin/config/development/configuration/report/type/search_page');
$this->assertReport('Search page', [], [], [], []);
// Module, theme, and profile reports have no 'added' section.
$this->drupalGet('admin/config/development/configuration/report/module/search');
$this->assertReport('Search module', [], [], [], [], ['added']);
$this->drupalGet('admin/config/development/configuration/report/theme/stark');
$this->assertReport('Stark theme', [], [], [], [], ['added']);
$inactive = ['locale.settings' => 'Simple configuration'];
$this->drupalGet('admin/config/development/configuration/report/profile');
$this->assertReport('Testing profile', [], [], [], $inactive, ['added']);
// The locale.settings line should show that the Testing profile is the
// provider.
$session = $this->assertSession();
$session->pageTextContains('Testing profile');
// Verify that the user search page cannot be imported (because it already
// exists).
$this->drupalGet('admin/config/development/configuration/report/import/search_page/user_search');
$session = $this->assertSession();
$session->statusCodeEquals(404);
// Delete the user search page from the search UI and verify report for
// both the search page config type and user module.
$this->drupalGet('admin/config/search/pages');
$this->clickLink('Delete');
$this->submitForm([], 'Delete');
$inactive = ['search.page.user_search' => 'Users'];
$this->drupalGet('admin/config/development/configuration/report/type/search_page');
$this->assertReport('Search page', [], [], [], $inactive);
// The search.page.user_search line should show that the User module is the
// provider.
$session = $this->assertSession();
$session->pageTextContains('User module');
$this->drupalGet('admin/config/development/configuration/report/module/user');
$this->assertReport('User module', [], [], [], $inactive, ['added', 'changed']);
// Verify that the user search page cannot be reverted (because it does
// not already exist).
$this->drupalGet('admin/config/development/configuration/report/revert/search_page/user_search');
$session = $this->assertSession();
$session->statusCodeEquals(404);
// Verify that the delete URL doesn't work either.
$this->drupalGet('admin/config/development/configuration/report/delete/search_page/user_search');
$session = $this->assertSession();
$session->statusCodeEquals(404);
// Use the import link to get it back. Do this from the search page
// report to make sure we are importing the right config.
$this->drupalGet('admin/config/development/configuration/report/type/search_page');
$this->clickLink('Import from source');
$this->submitForm([], 'Import');
$session = $this->assertSession();
$session->pageTextContains('has been imported');
$this->assertNoReport();
$this->drupalGet('admin/config/development/configuration/report/type/search_page');
$this->assertReport('Search page', [], [], [], []);
// Verify that after import, there is no config hash generated.
$this->drupalGet('admin/config/development/configuration/single/export/search_page/user_search');
$session = $this->assertSession();
$session->pageTextContains('id: user_search');
$session->pageTextNotContains('default_config_hash:');
// Edit the node search page from the search UI and verify report.
$this->drupalGet('admin/config/search/pages');
$this->clickLink('Edit');
$this->submitForm([
'label' => 'New label',
'path' => 'new_path',
], 'Save search page');
$changed = ['search.page.node_search' => 'New label'];
$this->drupalGet('admin/config/development/configuration/report/type/search_page');
$this->assertReport('Search page', [], [], $changed, []);
// Test the show differences link.
$this->clickLink('Show differences');
$session = $this->assertSession();
$session->pageTextContains('Content');
$session->pageTextContains('New label');
$session->pageTextContains('node');
$session->pageTextContains('new_path');
// Test the Back link.
$this->clickLink("Back to 'Updates report' page");
$this->assertNoReport();
// Test the export link.
$this->drupalGet('admin/config/development/configuration/report/type/search_page');
$this->clickLink('Export', 1);
$session = $this->assertSession();
$session->pageTextContains('Here is your configuration:');
$session->pageTextContains('id: node_search');
$session->pageTextContains('New label');
$session->pageTextContains('path: new_path');
$session->pageTextContains('search.page.node_search.yml');
// Grab the uuid and hash lines from the exported config for the next test.
$text = strip_tags($this->getSession()->getPage()->find('css', 'textarea')->getHtml());
$matches = [];
preg_match('|^.*uuid:.*$|m', $text, $matches);
$uuid_line = trim($matches[0]);
preg_match('|^.*default_config_hash:.*$|m', $text, $matches);
$hash_line = trim($matches[0]);
// Test reverting.
$this->drupalGet('admin/config/development/configuration/report/type/search_page');
$this->clickLink('Revert to source');
$session = $this->assertSession();
$session->pageTextContains('Are you sure you want to revert');
$session->pageTextContains('Search page');
$session->pageTextContains('node_search');
$session->pageTextContains('Customizations will be lost. This action cannot be undone');
$this->submitForm([], 'Revert');
$this->drupalGet('admin/config/development/configuration/report/type/search_page');
$this->assertReport('Search page', [], [], [], []);
// Verify that the uuid and hash keys were retained in the revert.
$this->drupalGet('admin/config/development/configuration/single/export/search_page/node_search');
$session = $this->assertSession();
$session->pageTextContains('id: node_search');
$session->pageTextContains($uuid_line);
$session->pageTextContains($hash_line);
// Add a new search page from the search UI and verify report.
$this->drupalGet('admin/config/search/pages');
$this->submitForm([
'search_type' => 'node_search',
], 'Add search page');
$this->submitForm([
'label' => 'test',
'id' => 'test',
'path' => 'test',
], 'Save');
$this->drupalGet('admin/config/development/configuration/report/type/search_page');
$added = ['search.page.test' => 'test'];
$this->assertReport('Search page', [], $added, [], []);
// Test the export link.
$this->clickLink('Export', 1);
$session = $this->assertSession();
$session->pageTextContains('Here is your configuration:');
$session->pageTextContains('id: test');
$session->pageTextContains('label: test');
$session->pageTextContains('path: test');
$session->pageTextContains('search.page.test.yml');
// Test the delete link.
$this->drupalGet('admin/config/development/configuration/report/type/search_page');
$this->clickLink('Delete');
$session = $this->assertSession();
$session->pageTextContains('Are you sure');
$session->pageTextContains('cannot be undone');
$this->submitForm([], 'Delete');
$session = $this->assertSession();
$session->pageTextContains('has been deleted');
// And verify the report again.
$this->drupalGet('admin/config/development/configuration/report/type/search_page');
$this->assertReport('Search page', [], [], [], []);
// Change the search module config and verify the actions work for
// simple config.
$this->drupalGet('admin/config/search/pages');
$this->submitForm([
'minimum_word_size' => 4,
], 'Save configuration');
$changed = ['search.settings' => 'search.settings'];
$this->drupalGet('admin/config/development/configuration/report/module/search');
$this->assertReport('Search module', [], [], $changed, [], ['added']);
$this->clickLink('Show differences');
$session = $this->assertSession();
$session->pageTextContains('Config difference for Simple configuration search.settings');
$session->pageTextContains('index::minimum_word_size');
$session->pageTextContains('4');
$this->drupalGet('admin/config/development/configuration/report/module/search');
$this->clickLink('Export', 1);
$session = $this->assertSession();
$session->pageTextContains('minimum_word_size: 4');
// Grab the hash line for the next test.
$text = strip_tags($this->getSession()->getPage()->find('css', 'textarea')->getHtml());
$matches = [];
preg_match('|^.*default_config_hash:.*$|m', $text, $matches);
$hash_line = trim($matches[0]);
$this->drupalGet('admin/config/development/configuration/report/module/search');
$this->clickLink('Revert to source');
$this->submitForm([], 'Revert');
// Verify that the hash was retained in the revert.
$this->drupalGet('admin/config/development/configuration/single/export/system.simple/search.settings');
$session = $this->assertSession();
$session->pageTextContains($hash_line);
$this->drupalGet('admin/config/development/configuration/report/module/search');
$this->assertReport('Search module', [], [], [], [], ['added']);
// Edit the plain_text filter from the filter UI and verify report.
// The filter_format config type uses a label key other than 'label'.
$this->drupalGet('admin/config/content/formats/manage/plain_text');
$this->submitForm([
'name' => 'New label',
], 'Save configuration');
$changed = ['filter.format.plain_text' => 'New label'];
$this->drupalGet('admin/config/development/configuration/report/type/filter_format');
$this->assertReport('Text format', [], [], $changed, []);
}
/**
* Asserts that the report page has the correct content.
*
* Assumes you are already on the report page.
*
* @param string $title
* Report title to check for.
* @param string[] $missing
* Array of items that should be listed as missing, name => label.
* @param string[] $added
* Array of items that should be listed as added, name => label.
* @param string[] $changed
* Array of items that should be listed as changed, name => label.
* @param string[] $inactive
* Array of items that should be listed as inactive, name => label.
* @param string[] $skip
* Array of report sections to skip checking.
*/
protected function assertReport($title, array $missing, array $added, array $changed, array $inactive, array $skip = []) {
$session = $this->assertSession();
$session->pageTextContains('Configuration updates report for ' . $title);
$session->pageTextContains('Generate new report');
if (!in_array('missing', $skip)) {
$session->pageTextContains('Missing configuration items');
if (count($missing)) {
foreach ($missing as $name => $label) {
$session->pageTextContains($name);
$session->pageTextContains($label);
}
$session->pageTextNotContains('None: all provided configuration items are in your active configuration.');
}
else {
$session->pageTextContains('None: all provided configuration items are in your active configuration.');
}
}
if (!in_array('inactive', $skip)) {
$session->pageTextContains('Inactive optional items');
if (count($inactive)) {
foreach ($inactive as $name => $label) {
$session->pageTextContains($name);
$session->pageTextContains($label);
}
$session->pageTextNotContains('None: all optional configuration items are in your active configuration.');
}
else {
$session->pageTextContains('None: all optional configuration items are in your active configuration.');
}
}
if (!in_array('added', $skip)) {
$session->pageTextContains('Added configuration items');
if (count($added)) {
foreach ($added as $name => $label) {
$session->pageTextContains($name);
$session->pageTextContains($label);
}
$session->pageTextNotContains('None: all active configuration items of this type were provided by modules, themes, or install profile.');
}
else {
$session->pageTextContains('None: all active configuration items of this type were provided by modules, themes, or install profile.');
}
}
if (!in_array('changed', $skip)) {
$session->pageTextContains('Changed configuration items');
if (count($changed)) {
foreach ($changed as $name => $label) {
$session->pageTextContains($name);
$session->pageTextContains($label);
}
$session->pageTextNotContains('None: no active configuration items differ from their current provided versions.');
}
else {
$session->pageTextContains('None: no active configuration items differ from their current provided versions.');
}
}
}
/**
* Asserts that the report is not shown.
*
* Assumes you are already on the report form page.
*/
protected function assertNoReport() {
$session = $this->assertSession();
$session->pageTextContains('Report type');
$session->pageTextContains('Full report');
$session->pageTextContains('Single configuration type');
$session->pageTextContains('Single module');
$session->pageTextContains('Single theme');
$session->pageTextContains('Installation profile');
$session->pageTextContains('Updates report');
$session->pageTextNotContains('Missing configuration items');
$session->pageTextNotContains('Added configuration items');
$session->pageTextNotContains('Changed configuration items');
$session->pageTextNotContains('Unchanged configuration items');
// Verify that certain report links are shown or not shown. For extensions,
// only extensions that have configuration should be shown.
// Modules.
$session->linkExists('Search');
$session->linkExists('Field');
$session->linkNotExists('Configuration Update Base');
// @todo this test fails since #2922511. Investigate.
// $session->linkNotExists('Configuration Update Reports');
// Themes.
$session->linkNotExists('Stark');
// Profiles.
$session->linkExists('Testing');
// Configuration types.
$session->linkExists('Everything');
$session->linkExists('Simple configuration');
$session->linkExists('Search page');
}
}