Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| InstallOptionalConfigHooks | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| modulesInstalled | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Drupal\crm\Hook; |
| 4 | |
| 5 | use Drupal\Core\Hook\Attribute\Hook; |
| 6 | use Drupal\Core\Config\ConfigInstallerInterface; |
| 7 | use Drupal\Core\Config\FileStorage; |
| 8 | use Drupal\Core\Extension\ModuleExtensionList; |
| 9 | |
| 10 | /** |
| 11 | * Hooks relating to installing optional config. |
| 12 | */ |
| 13 | class InstallOptionalConfigHooks { |
| 14 | |
| 15 | /** |
| 16 | * Constructs a new InstallOptionalConfigHooks object. |
| 17 | * |
| 18 | * @param \Drupal\Core\Config\ConfigInstallerInterface $configInstaller |
| 19 | * The config installer. |
| 20 | * @param \Drupal\Core\Extension\ModuleExtensionList $moduleExtensionList |
| 21 | * The module extension list. |
| 22 | */ |
| 23 | public function __construct( |
| 24 | protected ConfigInstallerInterface $configInstaller, |
| 25 | protected ModuleExtensionList $moduleExtensionList, |
| 26 | ) {} |
| 27 | |
| 28 | /** |
| 29 | * Implements hook_modules_installed(). |
| 30 | */ |
| 31 | #[Hook('modules_installed')] |
| 32 | public function modulesInstalled(array $modules) { |
| 33 | if (!in_array('search', $modules)) { |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | // Restrict to only your module's optional config. |
| 38 | $storage = new FileStorage($this->moduleExtensionList->getPath('crm') . '/config/optional'); |
| 39 | foreach ($modules as $module) { |
| 40 | // Restrict to configs that depend on the module just installed. |
| 41 | $this->configInstaller->installOptionalConfig($storage, [ |
| 42 | 'module' => $module, |
| 43 | ]); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | } |