3365328: Add a config action to modify an array of configuration
Add a new config action "simpleConfigArray" with 3 derivatives to alter simple array properties.
- simpleConfigArray:append - a wrapper around array_push()
- simpleConfigArray:prepend - a wrapper around array_unshift()
- simpleConfigArray:splice - a wrapper around array_splice()
Closes #3365328
Merge request reports
Activity
added 1 commit
- 389c0609 - Remove docblock from SimpleConfigArray constructor.
17 /** 18 * {@inheritdoc} 19 */ 20 public function getDerivativeDefinitions($base_plugin_definition): array { 21 $this->derivatives['append'] = $base_plugin_definition + [ 22 'function' => 'array_push', 23 'required_arguments' => 'values', 24 ]; 25 $this->derivatives['prepend'] = $base_plugin_definition + [ 26 'function' => 'array_unshift', 27 'required_arguments' => 'values', 28 ]; 29 $this->derivatives['splice'] = $base_plugin_definition + [ 30 'function' => 'array_splice', 31 'required_arguments' => ['offset', 'length', 'replacement'], 32 ]; - Comment on lines +21 to +32
These need to be inverted so that
$base_plugin_definition
can't override the derivative-specific stuff:21 $this->derivatives['append'] = $base_plugin_definition + [ 22 'function' => 'array_push', 23 'required_arguments' => 'values', 24 ]; 25 $this->derivatives['prepend'] = $base_plugin_definition + [ 26 'function' => 'array_unshift', 27 'required_arguments' => 'values', 28 ]; 29 $this->derivatives['splice'] = $base_plugin_definition + [ 30 'function' => 'array_splice', 31 'required_arguments' => ['offset', 'length', 'replacement'], 32 ]; 21 $this->derivatives['append'] = [ 22 'function' => 'array_push', 23 'required_arguments' => 'values', 24 ] + $base_plugin_definition; 25 $this->derivatives['prepend'] = [ 26 'function' => 'array_unshift', 27 'required_arguments' => 'values', 28 ] + $base_plugin_definition; 29 $this->derivatives['splice'] = [ 30 'function' => 'array_splice', 31 'required_arguments' => ['offset', 'length', 'replacement'], 32 ] + $base_plugin_definition; 100%, but I did model this after
core/lib/Drupal/Core/Config/Action/Plugin/ConfigAction/Deriver/CreateForEachBundleDeriver.php
andcore/lib/Drupal/Core/Config/Action/Plugin/ConfigAction/Deriver/EntityCreateDeriver.php
so I wonder if those should be updated too. Lmk if that's the case and I should file an issue.changed this line in version 6 of the diff
8 use Drupal\Core\Config\Action\ConfigActionException; 9 use Drupal\Core\Config\Action\ConfigActionPluginInterface; 10 use Drupal\Core\Config\Action\Plugin\ConfigAction\Deriver\SimpleConfigArrayDeriver; 11 use Drupal\Core\Config\ConfigFactoryInterface; 12 use Drupal\Core\Plugin\ContainerFactoryPluginInterface; 13 use Drupal\Core\StringTranslation\TranslatableMarkup; 14 use Symfony\Component\DependencyInjection\ContainerInterface; 15 16 /** 17 * @internal 18 * This API is experimental. 19 */ 20 #[ConfigAction( 21 id: 'simpleConfigArray', 22 admin_label: new TranslatableMarkup('Simple Configuration Array Update'), 23 deriver: SimpleConfigArrayDeriver::class changed this line in version 6 of the diff
13 use Drupal\Core\StringTranslation\TranslatableMarkup; 14 use Symfony\Component\DependencyInjection\ContainerInterface; 15 16 /** 17 * @internal 18 * This API is experimental. 19 */ 20 #[ConfigAction( 21 id: 'simpleConfigArray', 22 admin_label: new TranslatableMarkup('Simple Configuration Array Update'), 23 deriver: SimpleConfigArrayDeriver::class 24 )] 25 final class SimpleConfigArray implements ConfigActionPluginInterface, ContainerFactoryPluginInterface { 26 27 public function __construct( 28 protected readonly ConfigFactoryInterface $configFactory, changed this line in version 6 of the diff
35 */ 36 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static { 37 return new static( 38 $container->get(ConfigFactoryInterface::class), 39 $plugin_definition 40 ); 41 } 42 43 /** 44 * {@inheritdoc} 45 */ 46 public function apply(string $configName, mixed $value): void { 47 $config = $this->configFactory->getEditable($configName); 48 49 if ($config->isNew()) { 50 throw new ConfigActionException(sprintf('Config %s does not exist so can not be updated.', $configName)); changed this line in version 6 of the diff
42 43 /** 44 * {@inheritdoc} 45 */ 46 public function apply(string $configName, mixed $value): void { 47 $config = $this->configFactory->getEditable($configName); 48 49 if ($config->isNew()) { 50 throw new ConfigActionException(sprintf('Config %s does not exist so can not be updated.', $configName)); 51 } 52 53 if (!is_array($value)) { 54 throw new ConfigActionException(sprintf('Config %s can not be updated because $value is not an array.', $configName)); 55 } 56 57 if (!isset($value['property'])) { Also, we shouldn't use
isset()
, because this will allow$value['property']
to be an empty string or FALSE or any other invalid, falsy value. This should beif (empty($value['property']))
.Edited by Adam G-Hchanged this line in version 6 of the diff
- Resolved by Eric Volkernick
- Resolved by Adam G-H
- Resolved by Adam G-H
added 10 commits
-
db9d6cdf...27788820 - 8 commits from branch
project:11.x
- 96d0a541 - Merge branch '11.x' into 3365328-simple-config-array
- c73e3d57 - Normalize this a bit and split the tests up
-
db9d6cdf...27788820 - 8 commits from branch
added 1 commit
- a57f48c5 - Allow multiple invocations (needs test coverage)