Problem/Motivation
On Drupal 11.4 every attempt to create a moderation note fails with a 500:
TypeError: Drupal\Core\Access\AccessResult::allowedIf(): Argument #1 ($condition)
must be of type bool, Drupal\Core\Access\AccessResultAllowed given, called in
.../moderation_note/src/Controller/ModerationNoteController.php on line 59 in
Drupal\Core\Access\AccessResult::allowedIf() (line 82 of
core/lib/Drupal/Core/Access/AccessResult.php).
_moderation_note_on_entity() returns an AccessResult, not a bool, but two
call sites pass its return value into AccessResult::allowedIf():
- src/Controller/ModerationNoteController.php:59 (createNoteAccess())
- src/AccessControlHandler.php:31 (checkAccess(), "create" operation)
Drupal 11.3 declared allowedIf($condition) without a type hint, so the object
was silently cast and the calls happened to work. Drupal 11.4 declares
allowedIf(bool $condition, ?string $reason = NULL), which turns this into a
fatal error. See the change record:
https://www.drupal.org/node/3579037
Steps to reproduce
1. Install Drupal 11.4.x and Moderation Note 1.0.0-beta6.
2. Grant a user "create moderation notes" and view an entity with a notable field.
3. Click "Add note".
4. The AJAX request to `/moderation-note/add/...` returns a 500 and the TypeError above is logged.
Proposed resolution
`_moderation_note_on_entity()` already returns an `AccessResult`, so wrapping it
is unnecessary. Chain `orIf()` on it directly:
return _moderation_note_on_entity($entity, $account)
->orIf(AccessResult::allowedIfHasPermission($account, 'administer moderation notes'))
->cachePerPermissions()->cachePerUser()->addCacheableDependency($entity);
This also preserves the access result's own cacheability, which the previous
code discarded.
Patch attached below. It applies to 1.0.0-beta6 and works on both 11.3 and 11.4.
Remaining tasks
- Review
- Test coverage for `createNoteAccess()` and the `create` operation?
| Comment | File | Size | Author |
|---|---|---|---|
| moderation_note-fix-access-result-bool-type-hint.patch | 1.13 KB | baysaa |
Comments