Skip to content
Snippets Groups Projects
Verified Commit 55dd331d authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3455113 by b_sharpe, ankitv18, alexpott, pooja_sharma, phenaproxima,...

Issue #3455113 by b_sharpe, ankitv18, alexpott, pooja_sharma, phenaproxima, thejimbirch: Rename ensure_exists to createIfNotExists, and camel-case simpleConfigUpdate for consistency

(cherry picked from commit 9cd7babd)
parent 84cebb07
No related branches found
No related tags found
4 merge requests!11958Issue #3490507 by alexpott, smustgrave: Fix bogus mocking in...,!11769Issue #3517987: Add option to contextual filters to encode slashes in query parameter.,!11185Issue #3477324 by andypost, alexpott: Fix usage of str_getcsv() and fgetcsv() for PHP 8.4,!9944Issue #3483353: Consider making the createCopy config action optionally fail...
Showing
with 92 additions and 31 deletions
......@@ -53,6 +53,22 @@
*/
class ConfigActionManager extends DefaultPluginManager {
/**
* Information about all deprecated plugin IDs.
*
* @var string[]
*/
private static array $deprecatedPluginIds = [
'entity_create:ensure_exists' => [
'replacement' => 'entity_create:createIfNotExists',
'message' => 'The plugin ID "entity_create:ensure_exists" is deprecated in drupal:10.3.1 and will be removed in drupal:12.0.0. Use "entity_create:createIfNotExists" instead. See https://www.drupal.org/node/3458273.',
],
'simple_config_update' => [
'replacement' => 'simpleConfigUpdate',
'message' => 'The plugin ID "simple_config_update" is deprecated in drupal:10.3.1 and will be removed in drupal:12.0.0. Use "simpleConfigUpdate" instead. See https://www.drupal.org/node/3458273.',
],
];
/**
* Constructs a new \Drupal\Core\Config\Action\ConfigActionManager object.
*
......@@ -218,4 +234,28 @@ protected function getShorthandActionIdsForEntityType(string $entityType): array
return $map;
}
/**
* {@inheritdoc}
*/
public function alterDefinitions(&$definitions): void {
// Adds backwards compatibility for plugins that have been renamed.
foreach (self::$deprecatedPluginIds as $legacy => $new_plugin_id) {
$definitions[$legacy] = $definitions[$new_plugin_id['replacement']];
}
parent::alterDefinitions($definitions);
}
/**
* {@inheritdoc}
*/
public function createInstance($plugin_id, array $configuration = []) {
$instance = parent::createInstance($plugin_id, $configuration);
// Trigger deprecation notices for renamed plugins.
if (array_key_exists($plugin_id, self::$deprecatedPluginIds)) {
// phpcs:ignore Drupal.Semantics.FunctionTriggerError
@trigger_error(self::$deprecatedPluginIds[$plugin_id]['message'], E_USER_DEPRECATED);
}
return $instance;
}
}
......@@ -22,8 +22,8 @@ public function getDerivativeDefinitions($base_plugin_definition) {
// These derivatives apply to all entity types.
$base_plugin_definition['entity_types'] = ['*'];
$this->derivatives['ensure_exists'] = $base_plugin_definition + ['constructor_args' => ['exists' => Exists::ReturnEarlyIfExists]];
$this->derivatives['ensure_exists']['admin_label'] = $this->t('Ensure entity exists');
$this->derivatives['createIfNotExists'] = $base_plugin_definition + ['constructor_args' => ['exists' => Exists::ReturnEarlyIfExists]];
$this->derivatives['createIfNotExists']['admin_label'] = $this->t('Create entity if it does not exist');
$this->derivatives['create'] = $base_plugin_definition + ['constructor_args' => ['exists' => Exists::ErrorIfExists]];
$this->derivatives['create']['admin_label'] = $this->t('Entity create');
......
......@@ -17,7 +17,7 @@
* This API is experimental.
*/
#[ConfigAction(
id: 'simple_config_update',
id: 'simpleConfigUpdate',
admin_label: new TranslatableMarkup('Simple configuration update'),
)]
final class SimpleConfigUpdate implements ConfigActionPluginInterface, ContainerFactoryPluginInterface {
......
......@@ -5,7 +5,7 @@ config:
actions:
user.role.administrator:
# If this role already exists, then this action has no effect. If it doesn't exist, we'll create it with the following values.
ensure_exists:
createIfNotExists:
id: administrator
label: Administrator
weight: 3
......
......@@ -5,7 +5,7 @@ config:
actions:
user.role.content_editor:
# If this role already exists, then this action has no effect. If it doesn't exist, we'll create it with the following values.
ensure_exists:
createIfNotExists:
id: content_editor
label: 'Content editor'
weight: 2
......
......@@ -20,5 +20,5 @@ config:
- block.block.claro_secondary_local_tasks
actions:
system.theme:
simple_config_update:
simpleConfigUpdate:
admin: claro
......@@ -25,5 +25,5 @@ config:
- core.date_format.olivero_medium
actions:
system.theme:
simple_config_update:
simpleConfigUpdate:
default: olivero
......@@ -36,7 +36,7 @@ config:
# mapped to the \Drupal\user\Entity\Role::grantPermission() method.
actions:
user.role.editor:
ensure_exists:
createIfNotExists:
label: 'Editor'
grantPermissions:
- 'delete any article content'
......
......@@ -11,7 +11,7 @@ config:
- system.menu.footer
actions:
core.menu.static_menu_link_overrides:
simple_config_update:
simpleConfigUpdate:
definitions.contact__site_page:
menu_name: footer
parent: ''
......
......@@ -64,10 +64,10 @@ config:
- views.view.who_s_online
actions:
node.settings:
simple_config_update:
simpleConfigUpdate:
use_admin_theme: true
system.site:
simple_config_update:
simpleConfigUpdate:
page.front: /node
user.role.anonymous:
grantPermission: 'access content'
......@@ -96,7 +96,7 @@ config:
- 'delete own %bundle content'
- 'edit own %bundle content'
user.settings:
simple_config_update:
simpleConfigUpdate:
verify_mail: true
register: visitors_admin_approval
cancel_method: user_cancel_block
......@@ -33,15 +33,15 @@ public function testEntityCreate(): void {
$this->assertCount(0, \Drupal::entityTypeManager()->getStorage('config_test')->loadMultiple(), 'There are no config_test entities');
/** @var \Drupal\Core\Config\Action\ConfigActionManager $manager */
$manager = $this->container->get('plugin.manager.config_action');
$manager->applyAction('entity_create:ensure_exists', 'config_test.dynamic.action_test', ['label' => 'Action test']);
$manager->applyAction('entity_create:createIfNotExists', 'config_test.dynamic.action_test', ['label' => 'Action test']);
/** @var \Drupal\config_test\Entity\ConfigTest[] $config_test_entities */
$config_test_entities = \Drupal::entityTypeManager()->getStorage('config_test')->loadMultiple();
$this->assertCount(1, \Drupal::entityTypeManager()->getStorage('config_test')->loadMultiple(), 'There is 1 config_test entity');
$this->assertSame('Action test', $config_test_entities['action_test']->label());
$this->assertTrue(Uuid::isValid((string) $config_test_entities['action_test']->uuid()), 'Config entity assigned a valid UUID');
// Calling ensure exists action again will not error.
$manager->applyAction('entity_create:ensure_exists', 'config_test.dynamic.action_test', ['label' => 'Action test']);
// Calling createIfNotExists action again will not error.
$manager->applyAction('entity_create:createIfNotExists', 'config_test.dynamic.action_test', ['label' => 'Action test']);
try {
$manager->applyAction('entity_create:create', 'config_test.dynamic.action_test', ['label' => 'Action test']);
......@@ -244,11 +244,11 @@ public function testSimpleConfigUpdate(): void {
/** @var \Drupal\Core\Config\Action\ConfigActionManager $manager */
$manager = $this->container->get('plugin.manager.config_action');
// Call the simple config update action.
$manager->applyAction('simple_config_update', 'config_test.system', ['foo' => 'Yay!']);
$manager->applyAction('simpleConfigUpdate', 'config_test.system', ['foo' => 'Yay!']);
$this->assertSame('Yay!', $this->config('config_test.system')->get('foo'));
try {
$manager->applyAction('simple_config_update', 'config_test.system', 'Test');
$manager->applyAction('simpleConfigUpdate', 'config_test.system', 'Test');
$this->fail('Expected exception not thrown');
}
catch (ConfigActionException $e) {
......@@ -257,7 +257,7 @@ public function testSimpleConfigUpdate(): void {
$this->config('config_test.system')->delete();
try {
$manager->applyAction('simple_config_update', 'config_test.system', ['foo' => 'Yay!']);
$manager->applyAction('simpleConfigUpdate', 'config_test.system', ['foo' => 'Yay!']);
$this->fail('Expected exception not thrown');
}
catch (ConfigActionException $e) {
......@@ -273,7 +273,7 @@ public function testShorthandActionIds(): void {
$this->assertCount(0, $storage->loadMultiple(), 'There are no config_test entities');
/** @var \Drupal\Core\Config\Action\ConfigActionManager $manager */
$manager = $this->container->get('plugin.manager.config_action');
$manager->applyAction('ensure_exists', 'config_test.dynamic.action_test', ['label' => 'Action test', 'protected_property' => '']);
$manager->applyAction('createIfNotExists', 'config_test.dynamic.action_test', ['label' => 'Action test', 'protected_property' => '']);
/** @var \Drupal\config_test\Entity\ConfigTest[] $config_test_entities */
$config_test_entities = $storage->loadMultiple();
$this->assertCount(1, $config_test_entities, 'There is 1 config_test entity');
......@@ -299,7 +299,7 @@ public function testDuplicateShorthandActionIds(): void {
$manager = $this->container->get('plugin.manager.config_action');
$this->expectException(DuplicateConfigActionIdException::class);
$this->expectExceptionMessage("The plugins 'entity_method:config_test.dynamic:setProtectedProperty' and 'config_action_duplicate_test:config_test.dynamic:setProtectedProperty' both resolve to the same shorthand action ID for the 'config_test' entity type");
$manager->applyAction('ensure_exists', 'config_test.dynamic.action_test', ['label' => 'Action test', 'protected_property' => '']);
$manager->applyAction('createIfNotExists', 'config_test.dynamic.action_test', ['label' => 'Action test', 'protected_property' => '']);
}
/**
......
......@@ -77,7 +77,7 @@ public function testConfigActionsAreValidated(string $entity_type_id): void {
config:
actions:
$config_name:
simple_config_update:
simpleConfigUpdate:
$label_key: ''
YAML;
......@@ -118,7 +118,7 @@ public function testConfigActionMissingDependency(): void {
config:
actions:
random.config:
simple_config_update:
simpleConfigUpdate:
label: ''
YAML;
......
......@@ -208,7 +208,7 @@ public function testInvalidConfigAction() :void {
config:
actions:
config_test.dynamic.recipe:
ensure_exists:
createIfNotExists:
label: 'Created by recipe'
setBody: 'Description set by recipe'
YAML;
......@@ -219,6 +219,27 @@ public function testInvalidConfigAction() :void {
RecipeRunner::processRecipe($recipe);
}
/**
* Tests that renamed plugins are marked as deprecated.
*
* @group legacy
*/
public function testRenamedConfigActions(): void {
$recipe_data = <<<YAML
name: Renamed config action
install:
- config_test
config:
actions:
config_test.dynamic.recipe:
ensure_exists:
label: 'Created by recipe'
YAML;
$recipe = $this->createRecipe($recipe_data);
$this->expectDeprecation('The plugin ID "entity_create:ensure_exists" is deprecated in drupal:10.3.1 and will be removed in drupal:12.0.0. Use "entity_create:createIfNotExists" instead. See https://www.drupal.org/node/3458273.');
RecipeRunner::processRecipe($recipe);
}
public function testRecipesAreDisambiguatedByPath(): void {
$recipe_data = <<<YAML
name: 'Recipe include'
......
......@@ -264,7 +264,7 @@ public static function providerRecipeValidation(): iterable {
config:
actions:
config_test.dynamic.recipe:
ensure_exists:
createIfNotExists:
label: 'Created by recipe'
setProtectedProperty: 'Set by recipe'
YAML,
......
......@@ -122,7 +122,7 @@ public function testInvalidExpression(string $expression, string $expected_excep
config:
actions:
$expression:
simple_config_update:
simpleConfigUpdate:
label: 'Changed by config action'
YAML;
$recipe = $this->createRecipe($contents);
......
......@@ -5,9 +5,9 @@ install:
config:
actions:
config_test.dynamic.recipe:
ensure_exists:
createIfNotExists:
label: 'Created by recipe'
setProtectedProperty: 'Set by recipe'
config_test.system:
simple_config_update:
simpleConfigUpdate:
foo: 'not bar'
......@@ -5,5 +5,5 @@ install:
config:
actions:
node.settings:
simple_config_update:
simpleConfigUpdate:
use_admin_theme: true
......@@ -5,5 +5,5 @@ recipes:
config:
actions:
node.settings:
simple_config_update:
simpleConfigUpdate:
use_admin_theme: true
......@@ -5,5 +5,5 @@ recipes:
config:
actions:
node.settings:
simple_config_update:
simpleConfigUpdate:
use_admin_theme: true
......@@ -15,5 +15,5 @@ config:
# This will cause a validation error, which will trigger a rollback.
# The rollback should fail, since the Media module can't be uninstalled
# now that the plain_text format is using one of its filters.
simple_config_update:
simpleConfigUpdate:
non_existent_key: whatever!
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment