Unverified Commit fe059b39 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3552023 by phenaproxima, bircher, alexpott, thejimbirch: Recipes should...

Issue #3552023 by phenaproxima, bircher, alexpott, thejimbirch: Recipes should support skipping config actions on entities that don't exist
parent 8b522adc
Loading
Loading
Loading
Loading
Loading
+12
Changes for core/lib/Drupal/Core/Config/Action/ConfigActionManager.php: 12 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -131,6 +131,18 @@ public function __construct(
   * @see \Drupal\Core\Config\Action\ConfigActionManager::getConfigNamesMatchingExpression()
   */
  public function applyAction(string $action_id, string $configName, mixed $data): void {
    if (str_starts_with($configName, '?')) {
      if (str_contains($configName, '*')) {
        throw new ConfigActionException("The '$configName' configuration name is optional because it starts with a question mark, and therefore cannot contain wildcards.");
      }
      $configName = trim($configName, '?');
      if (!$this->configStorage->exists($configName)) {
        // The config action is optional and the config doesn't exist.
        // So there is nothing to do.
        return;
      }
    }

    if (!$this->hasDefinition($action_id)) {
      // Get the full plugin ID from the shorthand map, if it is available.
      $entity_type = $this->configManager->getEntityTypeIdByName($configName);
+23
Changes for core/tests/Drupal/KernelTests/Core/Config/Action/ConfigActionTest.php: 23 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -129,6 +129,15 @@ public function testEntityMethod(): void {
    $config_test_entity = $storage->load('dotted.default');
    $this->assertSame([['a', 'b', 'c'], ['a']], $config_test_entity->getArrayProperty());

    // Ensure that we can affect an optional config entity that actually exists.
    $manager->applyAction(
      'entity_method:config_test.dynamic:setArray',
      '?config_test.dynamic.dotted.default',
      ['x', 'y', 'z'],
    );
    $config_test_entity = $storage->load('dotted.default');
    $this->assertSame(['x', 'y', 'z'], $config_test_entity->getArrayProperty());

    $config_test_entity->delete();
    try {
      $manager->applyAction('entity_method:config_test.dynamic:setProtectedProperty', 'config_test.dynamic.dotted.default', 'Test value');
@@ -138,6 +147,20 @@ public function testEntityMethod(): void {
      $this->assertSame('Entity config_test.dynamic.dotted.default does not exist', $e->getMessage());
    }

    // Do the same with the optional modifier in the name does not throw an exception.
    $manager->applyAction('entity_method:config_test.dynamic:setProtectedProperty', '?config_test.dynamic.dotted.default', 'Test value');
    // The configuration also didn't get created with and without the modifier.
    $this->assertNull($storage->load('config_test.dynamic.dotted.default'));
    $this->assertNull($storage->load('?config_test.dynamic.dotted.default'));

    try {
      $manager->applyAction('entity_method:config_test.dynamic:setProtectedProperty', '?config_test.dynamic.*.default', 'Test value');
      $this->fail('Expected exception not thrown');
    }
    catch (ConfigActionException $e) {
      $this->assertSame("The '?config_test.dynamic.*.default' configuration name is optional because it starts with a question mark, and therefore cannot contain wildcards.", $e->getMessage());
    }

    // Test custom and default admin labels.
    $this->assertSame('Test configuration append', (string) $manager->getDefinition('entity_method:config_test.dynamic:append')['admin_label']);
    $this->assertSame('Set default name', (string) $manager->getDefinition('entity_method:config_test.dynamic:defaultProtectedProperty')['admin_label']);