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 9cd7babdc7)
merge-requests/8604/merge
Alex Pott 2024-07-01 16:06:46 +01:00
parent 84cebb07ea
commit 55dd331dea
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
20 changed files with 92 additions and 31 deletions

View File

@ -53,6 +53,22 @@ use Drupal\Core\Validation\Plugin\Validation\Constraint\FullyValidatableConstrai
*/
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 @@ class ConfigActionManager extends DefaultPluginManager {
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;
}
}

View File

@ -22,8 +22,8 @@ final class EntityCreateDeriver extends DeriverBase {
// 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');

View File

@ -17,7 +17,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* This API is experimental.
*/
#[ConfigAction(
id: 'simple_config_update',
id: 'simpleConfigUpdate',
admin_label: new TranslatableMarkup('Simple configuration update'),
)]
final class SimpleConfigUpdate implements ConfigActionPluginInterface, ContainerFactoryPluginInterface {

View File

@ -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

View File

@ -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

View File

@ -20,5 +20,5 @@ config:
- block.block.claro_secondary_local_tasks
actions:
system.theme:
simple_config_update:
simpleConfigUpdate:
admin: claro

View File

@ -25,5 +25,5 @@ config:
- core.date_format.olivero_medium
actions:
system.theme:
simple_config_update:
simpleConfigUpdate:
default: olivero

View File

@ -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'

View File

@ -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: ''

View File

@ -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

View File

@ -33,15 +33,15 @@ class ConfigActionTest extends KernelTestBase {
$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 @@ class ConfigActionTest extends KernelTestBase {
/** @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 @@ class ConfigActionTest extends KernelTestBase {
$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 @@ class ConfigActionTest extends KernelTestBase {
$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 @@ class ConfigActionTest extends KernelTestBase {
$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' => '']);
}
/**

View File

@ -77,7 +77,7 @@ name: Config actions making bad decisions
config:
actions:
$config_name:
simple_config_update:
simpleConfigUpdate:
$label_key: ''
YAML;
@ -118,7 +118,7 @@ name: Config actions making bad decisions
config:
actions:
random.config:
simple_config_update:
simpleConfigUpdate:
label: ''
YAML;

View File

@ -208,7 +208,7 @@ install:
config:
actions:
config_test.dynamic.recipe:
ensure_exists:
createIfNotExists:
label: 'Created by recipe'
setBody: 'Description set by recipe'
YAML;
@ -219,6 +219,27 @@ YAML;
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'

View File

@ -264,7 +264,7 @@ install:
config:
actions:
config_test.dynamic.recipe:
ensure_exists:
createIfNotExists:
label: 'Created by recipe'
setProtectedProperty: 'Set by recipe'
YAML,

View File

@ -122,7 +122,7 @@ name: 'Wildcards gone wild...'
config:
actions:
$expression:
simple_config_update:
simpleConfigUpdate:
label: 'Changed by config action'
YAML;
$recipe = $this->createRecipe($contents);

View File

@ -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'

View File

@ -5,5 +5,5 @@ install:
config:
actions:
node.settings:
simple_config_update:
simpleConfigUpdate:
use_admin_theme: true

View File

@ -5,5 +5,5 @@ recipes:
config:
actions:
node.settings:
simple_config_update:
simpleConfigUpdate:
use_admin_theme: true

View File

@ -5,5 +5,5 @@ recipes:
config:
actions:
node.settings:
simple_config_update:
simpleConfigUpdate:
use_admin_theme: true

View File

@ -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!