How to alter Core Existing Actions using hook_action_info_alter
Table of contents
No headings in the article.
In Drupal 9 /10 We have a core Actions Plugin defined in docroot/core/lib/Drupal/Core/Action/ActionManager.php
which is providing a hook to alter Action annotations, I used the below code to alter the existing Action Class with a New one
In your custom module add the below code in your my_module.module file
function module_name_action_info_alter(&$definitions) {
// Overwrite Drupal Core Add Role class with CustomClass
foreach ($definitions as &$definition) {
// user_add_role_action is a core action, check docroot/core/modules/user/src/Plugin/Action/AddRoleUser.php
if ($definition['id'] === 'user_add_role_action' && $definition['class'] == AddRoleUser::class) {
$definition['class'] = YourOwnClass::class; // we need to create YourOwnClass in docroot/modules/custom/module_name/src/Plugin/Action/YourOwnClass.php
}
}
}
If we print $definition
for any action we see the below information
array:6 [▼
"confirm_form_route_name" => ""
"type" => "user"
"id" => "user_add_role_action"
"label" => Drupal\Core\StringTranslation\TranslatableMarkup {#33044 ▶}
"class" => "Drupal\user\Plugin\Action\AddRoleUser"
"provider" => "user"
]