ConfigOverrideInterface is part of the configuration system in Drupal 8. It may be used to alter
configurations and settings if the written module is activated.
But first an old example to get the same result by implementing hook_install.
In this example we add the modules paragraph to the node page type.
<?php
/**
* Implements hook_install().
*/
function my_custom_module_install() {
// Add paragraph to page node type.
$bundle_fields = \Drupal::getContainer()->get('entity_field.manager')->getFieldDefinitions('node', 'page');
// Skip if entity type or field does not exists.
if (empty($bundle_fields) || empty($bundle_fields['field_with_paragraphs_refs'])) {
return;
}
$field_definition = $bundle_fields['field_with_paragraphs_refs'];
$field_settings = $field_definition->getSetting('handler_settings');
$field_settings['target_bundles']['my_custom_module_paragraph'] = 'my_custom_module_paragraph';
$field_settings['target_bundles_drag_drop']['my_custom_module_paragraph']['enabled'] = TRUE;
$field_definition->setSetting('handler_settings', $field_settings);
$field_definition->save();
}
There are some downsites to it. It is barely testable and it cannot be altered.
A new way is to implement ConfigOverrideInterface.
First you need to define in your my_custom_module.services.yml the service class for implementing the interface.
services:
my_custom_module.overrider:
class: \Drupal\my_custom_module\MyCustomModuleConfigOverrides
arguments: ['@config.factory']
tags:
- {name: config.factory.override, priority: 5}
I will pass the configuration manager as a dependency to access it directly instead of using the \Drupal container.
The priority may be used to override it from other modules with the same service name.
The implementation of the ConfigFactoryOverrideInterface interface happens in my_custom_module/src/MyCustomModuleConfigOverrides.php
<?php
namespace Drupal\my_custom_module;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryOverrideInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\StorageInterface;
/**
* My Custom module configuration override.
*/
class MyCustomModuleConfigOverrides implements ConfigFactoryOverrideInterface {
/**
* Configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* {@inheritdoc}
*
* Set config manager as protected property.
*/
public function __construct(ConfigFactoryInterface $config_factory) {
$this->configFactory = $config_factory;
}
/**
* {@inheritdoc}
*/
public function loadOverrides($names) {
$overrides = [];
if (in_array('field.field.node.page.field_with_paragraphs_refs', $names)) {
$target_bundles = $this->configFactory
->getEditable('field.field.node.page.field_with_paragraphs_refs')
->getOriginal('settings.handler_settings.target_bundles');
$target_bundles_drag_drop = $this->configFactory
->getEditable('field.field.node.page.field_with_paragraphs_refs')
->getOriginal('settings.handler_settings.target_bundles_drag_drop');
$overrides['field.field.node.page.field_parafield_with_paragraphs_refsgraphs']
['settings']['handler_settings']['target_bundles'] =
array_merge($target_bundles, ['my_custom_module_paragraph' => 'my_custom_module_paragraph']);
$overrides['field.field.node.page.field_with_paragraphs_refs']
['settings']['handler_settings']['target_bundles_drag_drop'] =
array_merge($target_bundles_drag_drop, ['my_custom_module_paragraph' => ['enabled' => TRUE]]);
}
return $overrides;
}
/**
* {@inheritdoc}
*/
public function getCacheSuffix() {
return 'MyCustomModuleConfigOverrides';
}
/**
* {@inheritdoc}
*/
public function getCacheableMetadata($name) {
return new CacheableMetadata();
}
/**
* {@inheritdoc}
*/
public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION) {
return NULL;
}
}
When the module is enabled the custom paragraph will be activated for the node type page and may exported into the configuration.
The magic happens in the method loadOverrides(). We catch the current configuration of the paragraph reference field for node page and merge them with our new paragraph we want to provide from our custom module.
Sources:
back