Problem/Motivation
As part of #3349394: Provide announcement feed as a block a new block was created to show the announcements feed as a block.
This block checks if the user has 'access announcements' to grant access or not to the block. This logic has a bug.
According to BlockPluginTrait documentation, it encourages the use of blockAccess() method instead of access() to "to avoid repeating the handling of the $return_as_object argument."
AnnounceBlock overrides access() method instead of blockAccess(), and it does not handle the $return_as_object argument, so it could break method implementations that expect to get a boolean instead of an AccessResultInterface.
Steps to reproduce
Run this in your php console (drush php):
- $manager = \Drupal::service('plugin.manager.block')
- $user = new \Drupal\Core\Session\AnonymousUserSession()
- $block = $manager->createInstance('announce_block')
- $block->access($user)
It returns an instance of Drupal\Core\Access\AccessResultNeutral instead of FALSE.
Proposed resolution
Implement the access logic in blockAccess() method instead of access().
Remaining tasks
Implement fix
Add test coverage
User interface changes
None
API changes
None
Data model changes
None
Release notes snippet
None
Issue fork drupal-3414800
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
plopescComment #4
plopescMR created and tests are green.
Comment #5
smustgrave commentedThanks for reporting.
Adding test coverage as a remaining task.
Comment #6
plopescThank you for your review @smustgrave
More specific tests were added, they're green in the main pipeline and the test only job fails. https://git.drupalcode.org/issue/drupal-3414800/-/pipelines/77948
Comment #7
smustgrave commentedTest coverage appears to be there.
Change seems simple enough that I can mark now vs waiting.
Comment #8
longwaveNormally adding return types to existing code is not allowed because someone might have extended it. But in this specific case I can't see a reason why anyone would be subclassing the announcement block, so going to let this one slide.
Eligible as a bug fix in 10.2.x so backported there too.
Committed and pushed 7025180d11 to 11.x and 67bb7069a0 to 10.2.x. Thanks!