Skip to content

Commit

Permalink
Merge pull request #900 from City-of-Helsinki/UHF-11026
Browse files Browse the repository at this point in the history
UHF-11026: Update configuration automatically
  • Loading branch information
tuutti authored Feb 26, 2025
2 parents f6ba6c5 + b5866d8 commit e78668d
Show file tree
Hide file tree
Showing 61 changed files with 1,191 additions and 1,696 deletions.
28 changes: 13 additions & 15 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,28 @@

## How to install
* Make sure your instance is up and running on latest dev branch.
* `git pull origin dev`
* `make fresh`
* `git pull origin dev`
* `make fresh`
* Update the Helfi Platform config
* `composer require drupal/helfi_platform_config:dev-UHF-0000_insert_correct_branch`
* `composer require drupal/helfi_platform_config:dev-UHF-0000_insert_correct_branch`
* Run `make drush-updb drush-cr`
* Run `make shell`
* In the shell, run `drush helfi:platform-config:update`
<!-- Running all module updates takes approx. 5 minutes. -->
<!-- To run one module update: `drush helfi:platform-config:update module_name"` -->

## How to test
<!-- Describe steps how to test the features, add as many steps as you want to be tested -->

* [ ] Check that this feature works
* [ ] Check that code follows our standards

## Continuous documentation
<!-- One of the checkboxes below needs to be checked like this: `[x]` (or click when not in edit mode) -->

* [ ] This feature has been documented/the documentation has been updated
* [ ] This change doesn't require updates to the documentation

## Translations
<!-- The checkbox below needs to be checked like this: `[x]` (or click when not in edit mode). Not needed if the translations were not affected. -->

* [ ] Translations have been added to .po -files and included in this PR
<!-- Check list for the developer. Did you update/add/check the -->
<!-- * documentation -->
<!-- * translations -->
<!-- * coding standards -->

## Other PRs
<!-- For example an related PR in another repository -->
<!-- For example a related PR in another repository -->

* Link to other PR
*
78 changes: 30 additions & 48 deletions documentation/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,21 @@ dependencies:
## Drupal permissions
Permissions should be defined in module's `.install` file and the function should be called in `hook_install()` hook:
Permissions should be defined in module's `hook_platform_config_grant_permissions()` hook:

```php
function mymodule_grant_permissions() : void {
$permissions = [
/**
* Implements hook_platform_config_grant_permissions().
*/
function helfi_node_news_article_platform_config_grant_permissions() : array {
return [
'admin' => [
'access content',
'view news_article revisions',
],
'anonymous' => [
'access content',
'content_producer' => [
'view news_article revisions',
],
];
helfi_platform_config_grant_permissions($permissions);
}
/**
* Implements hook_install().
*/
function mymodule_install() : void {
mymodule_grant_permissions();
}
```

Expand Down Expand Up @@ -202,45 +197,15 @@ The command will:

See https://github.com/City-of-Helsinki/drupal-tools/blob/main/HelperCommands.php for more up-to-date information.

### Update permissions

To update permissions, add the new/changed permission in modules' `mymodule_grant_permission()` and call the function in `hook_update_N()` hook:

```php
function mymodule_update_9001(): void {
mymodule_grant_permissions();
}
```
### Update module configuration

### Update all configuration
Run: `drush helfi:platform-config:update`. This will re-import all configuration and trigger necessary hooks, such as `hook_platform_config_grant_permissions()` to update user permissions or `hook_helfi_paragraph_types()` to update Paragraph fields.

Use `helfi_platform_config.config_update_helper` service to replace existing configuration:

```php
function helfi_media_update_9001() : void {
// Re-import 'helfi_media' configuration.
\Drupal::service('helfi_platform_config.config_update_helper')
->update('helfi_media');
}
```
The update hook above will re-import all configuration from `helfi_media` module's `config/install` and `config/rewrite` folders and run necessary post-update hooks.
To update individual modules, run: `drush helfi:platform-config:update {module name}`.

#### Rewrite configuration

The `helfi_platform_config.config_update_helper` invokes `hook_rewrite_config_update`, which allows custom modules to react to config re-importing.

##### In this example we would want to override Text paragraph label with a configuration found in my_module.

To trigger the `hook_rewrite_config_update`, implement the hook to your `my_module.module`:
```php
function my_module_rewrite_config_update(string $module, Drupal\config_rewrite\ConfigRewriterInterface $configRewriter): void {
if ($module === 'helfi_paragraphs_text') {
// Rewrite helfi_paragraphs_text configuration.
$configRewriter->rewriteModuleConfig('my_module');
}
}
```
This hook will trigger when `\Drupal::service('helfi_platform_config.config_update_helper')->update('helfi_paragraphs_text');` is run and it will search for configurations in `my_module/config/rewrite/` folder.
The `drush helfi:platform-config:update` command will automatically rewrite all custom module configurations which are added to `config/rewrite` folder.

To override configurations for your Drupal instance, follow the instructions found in [Rewrite module project page](https://www.drupal.org/project/config_rewrite).

Expand All @@ -253,6 +218,23 @@ The label change for the Finnish translation would be implemented in a configura
label: Teksti (ylikirjoitettu)
```

#### How to fix recursive errors during site install?

In some cases there can be recursive errors during site install which will slow down the site installation process.
Here are two examples and instructions how to fix the issues.

##### Deleted and replaced configuration entity
```
[error] Deleted and replaced configuration entity "field.field.paragraph.event_list.field_filter_keywords"
```
This error occurs when the configuration translation has been added to `config/install/language/[langcode]/` folder. To fix the error, move the translation to `module/config/optional/language/[langcode]/` folder.
##### Unexpected error during import with operation create
```
[error] Unexpected error during import with operation create for core.entity_form_display.helfi_news_groups.helfi_news_groups.default: The "helfi_news_groups" entity type does not exist.
```
This error occurs when the entity type is not installed yet, but the configuration for its form display is being imported. Drupal should import the configuration dependencies before this form display configuration, but in some cases it does not. To fix the error, enforce the dependency on the configuration dependencies. See example: [core.entity_form_display.helfi_news_groups.helfi_news_groups.default](https://github.com/City-of-Helsinki/drupal-helfi-platform-config/blob/893e2ae5981898d409a67f3ddb879e6a771a3217/modules/helfi_paragraphs_news_list/config/install/core.entity_form_display.helfi_news_groups.helfi_news_groups.default.yml#L7).
The error gets fixed once the configurations have been added to Drupal repository's `conf/cmi/` folder.
## Tokens
Expand Down
11 changes: 0 additions & 11 deletions drush.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,3 @@ services:
arguments: ['@entity_type.manager', '@database']
tags:
- { name: drush.command }

helfi_platform_config.major_update_command:
class: Drupal\helfi_platform_config\Commands\MajorUpdateCommands
arguments:
- '@update.update_hook_registry'
- '@database'
- '@module_handler'
- '@extension.list.module'
- '@keyvalue'
tags:
- { name: drush.command }
29 changes: 9 additions & 20 deletions helfi_platform_config.module
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\block\Entity\Block;
use Drupal\helfi_api_base\Environment\Project;
use Drupal\helfi_platform_config\ConfigUpdate\ConfigUpdater;
use Drupal\helfi_platform_config\DTO\ParagraphTypeCollection;
use Drupal\helfi_platform_config\EntityVersionMatcher;
use Drupal\helfi_platform_config\Entity\PublishableRedirect;
use Drupal\helfi_platform_config\RedirectCleaner;
use Drupal\paragraphs\Entity\ParagraphsType;
use Drupal\user\Entity\Role;

/**
* Implements hook_entity_type_build().
Expand All @@ -56,6 +56,14 @@ function helfi_platform_config_modules_installed(array $modules, bool $is_syncin
if ($moduleHandler->moduleExists('locale')) {
locale_system_set_config_langcodes();
}

foreach ($modules as $module) {
$permissions = $moduleHandler->invoke($module, 'platform_config_grant_permissions');

\Drupal::service(ConfigUpdater::class)
->updatePermissions($permissions ?? []);
}

helfi_platform_config_update_paragraph_target_types();
}

Expand Down Expand Up @@ -310,25 +318,6 @@ function helfi_platform_config_block_access(Block $block, $operation, AccountInt
return AccessResult::neutral();
}

/**
* Grants permissions for given role.
*
* @param array $map
* The role => permissions map.
*
* @throws \Drupal\Core\Entity\EntityStorageException
* @throws \InvalidArgumentException
*/
function helfi_platform_config_grant_permissions(array $map): void {
foreach ($map as $rid => $permissions) {
if (!$role = Role::load($rid)) {
throw new InvalidArgumentException("Role ($rid) not found.");
}
array_map(fn (string $permission) => $role->grantPermission($permission), $permissions);
$role->save();
}
}

/**
* Removes permissions from all roles.
*
Expand Down
4 changes: 1 addition & 3 deletions modules/hdbt_admin_tools/hdbt_admin_tools.install
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,5 @@ function hdbt_admin_tools_update_9005(): void {
* UHF-11363: Updated pager labels in views.
*/
function hdbt_admin_tools_update_9008(): void {
// Re-import 'hdbt_admin_tools' configuration.
\Drupal::service('helfi_platform_config.config_update_helper')
->update('hdbt_admin_tools');
// This is now automated.
}
26 changes: 0 additions & 26 deletions modules/hdbt_cookie_banner/hdbt_cookie_banner.install
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,6 @@

declare(strict_types=1);

/**
* Grants required permissions.
*/
function hdbt_cookie_banner_grant_permissions() : void {
$permissions = [
'admin' => [
'administer hdbt_cookie_banner',
],
];
helfi_platform_config_grant_permissions($permissions);
}

/**
* Implements hook_install().
*/
function hdbt_cookie_banner_install($is_syncing) : void {
// Do not perform following steps if the module is being installed as part
// of a configuration import.
if ($is_syncing) {
return;
}

// Grant required permissions.
hdbt_cookie_banner_grant_permissions();
}

/**
* Implements hook_uninstall().
*/
Expand Down
11 changes: 11 additions & 0 deletions modules/hdbt_cookie_banner/hdbt_cookie_banner.module
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ use Drupal\Core\Language\LanguageInterface;
use Drupal\config_ignore\ConfigIgnoreConfig;
use Drupal\hdbt_cookie_banner\Form\HdbtCookieBannerForm;

/**
* Implements hook_platform_config_grant_permissions().
*/
function hdbt_cookie_banner_platform_config_grant_permissions() : array {
return [
'admin' => [
'administer hdbt_cookie_banner',
],
];
}

/**
* Implements hook_config_ignore_ignored_alter().
*/
Expand Down
1 change: 1 addition & 0 deletions modules/helfi_base_content/helfi_base_content.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies:
- drupal:system
- drupal:taxonomy
- drupal:text
- drupal:toolbar
- easy_breadcrumb:easy_breadcrumb
- entity_reference_revisions:entity_reference_revisions
- hdbt_admin_tools:hdbt_admin_tools
Expand Down
Loading

0 comments on commit e78668d

Please sign in to comment.