Skip to content
Snippets Groups Projects

3365328: Add a config action to modify an array of configuration

5 unresolved threads

Add a new config action "simpleConfigArray" with 3 derivatives to alter simple array properties.

  1. simpleConfigArray:append - a wrapper around array_push()
  2. simpleConfigArray:prepend - a wrapper around array_unshift()
  3. simpleConfigArray:splice - a wrapper around array_splice()

Closes #3365328

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
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:

    Suggested change
    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 and core/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.

  • Adam G-H changed this line in version 6 of the diff

    changed this line in version 6 of the diff

  • Please register or sign in to reply
  • 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
  • 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,
  • 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));
  • 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'])) {
  • Adam G-H
  • Adam G-H
  • Adam G-H
  • Adam G-H added 10 commits

    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

    Compare with previous version

  • Adam G-H added 1 commit

    added 1 commit

    • a57f48c5 - Allow multiple invocations (needs test coverage)

    Compare with previous version

  • Adam G-H added 1 commit

    added 1 commit

    • ee7a11c8 - Add test of multiple invocations"

    Compare with previous version

  • Adam G-H added 1 commit

    added 1 commit

    Compare with previous version

  • Please register or sign in to reply
    Loading