Use the BundlePermissionHandlerTrait in the permissions callback

Created on 23 June 2023, over 1 year ago
Updated 2 March 2024, 10 months ago

Problem/Motivation

When using a module that adds view any unpublished NODE_TYPE content inside a hook_entity_bundle_create I'm now getting the following depreciation notice:
Adding non-existent permissions to a role is deprecated in drupal:9.3.0 and triggers a runtime exception before drupal:10.0.0, The incorrect permissions are "view any unpublished test_type_1 content"

Steps to reproduce

Create a hook_entity_bundle_create in a custom module that tries to assign view unpublished permissions for the new type.

function mymoduled_entity_bundle_create($entity_type_id, $bundle) {
  if ($entity_type_id == 'node') {
    $type = \Drupal::entityTypeManager()->getStorage('node_type')->load($bundle);
     user_role_grant_permissions($rid, [
      'view any unpublished ' . $type->id() . ' content',
     ]);
   }
}

Proposed resolution

Using BundlePermissionHandlerTrait in the permissions callback adds the right dependency on the views unpublished permissions which appears to resolve this issue.

Remaining tasks

Write the patch

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇬🇧United Kingdom andybroomfield

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

Comments & Activities

Production build 0.71.5 2024