Undefined array key "id" in content_moderation_action_info_alter

Created on 7 April 2025, 5 months ago

Problem/Motivation

After updating Views Bulk Operations to the 4.3.4 there are warning on core content_moderation module complaining about undefined array key "id".

Views Bulk Operations is now filtering out the actions the user doesn't have permission to use. The problem comes when

function administerusersbyrole_action_info_alter(array &$definitions) {
  $definitions['user_add_role_action']['class'] = AddRoleUser::class;
  $definitions['user_remove_role_action']['class'] = RemoveRoleUser::class;
}

goes to replace the classes, it doesn't check if the action exists first.

Steps to reproduce

Install Views Bulk Operations to 4.3.2 or heigher and Administer Users by Role to 3.5.0, go to an admin view with VBO in it with a user with limited permissions. Warnings will be displayed.

Proposed resolution

To check if the action definition is set before modifying it.

function administerusersbyrole_action_info_alter(array &$definitions) {
  if (isset($definitions['user_add_role_action'])) {
    $definitions['user_add_role_action']['class'] = AddRoleUser::class;
  }
  if (isset($definitions['user_remove_role_action'])) {
    $definitions['user_remove_role_action']['class'] = RemoveRoleUser::class;
  }
}
🐛 Bug report
Status

Active

Version

3.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @altcom_alfonso
  • 🇬🇧United Kingdom altcom_neil

    This is still an issue when using Drupal 10.5.1, Views Bulk Operations 4.3.4, and Administer Users by Role 3.5.0

    Just expanding on what is going on here.

    Content Moderation does this:

    function content_moderation_action_info_alter(&$definitions) {
    
      // The publish/unpublish actions are not valid on moderated entities. So swap
      // their implementations out for alternates that will become a no-op on a
      // moderated entity. If another module has already swapped out those classes,
      // though, we'll be polite and do nothing.
      foreach ($definitions as &$definition) {
        if ($definition['id'] === 'entity:publish_action' && $definition['class'] == PublishAction::class) {
          $definition['class'] = ModerationOptOutPublish::class;
        }
        if ($definition['id'] === 'entity:unpublish_action' && $definition['class'] == UnpublishAction::class) {
          $definition['class'] = ModerationOptOutUnpublish::class;
        }
      }
    }
    

    it is not expecting the array to be missing the id - which I guess it shouldn't be.

    The code in Views Bulk Operations removing the action definitions is in the Actions Permissions sub module - https://git.drupalcode.org/project/views_bulk_operations/-/blob/4.3.x/mo...

    Because this code runs before the administerusersbyrole_action_info_alter(), the definitions are removed before this module tries to change the class.

Production build 0.71.5 2024