Problem/Motivation
The Anonymous Token module is incompatible with Drupal 11.2 due to a PHP fatal error caused by improper property type inheritance in the AnonymousCsrfAccessCheck class.
Steps to reproduce
Install Drupal 11.2 with PHP 8.3
Install and enable the Anonymous Token module
Clear cache using drush cr or through the admin interface
Observe the following fatal error:
PHP Fatal error: Type of Drupal\anonymous_token\Access\AnonymousCsrfAccessCheck::$csrfToken must be Drupal\Core\Access\CsrfTokenGenerator (as in class Drupal\Core\Access\CsrfAccessCheck) in /var/www/html/web/modules/contrib/anonymous_token/src/Access/AnonymousCsrfAccessCheck.php on line 12
Root Cause: The AnonymousCsrfAccessCheck class extends CsrfAccessCheck but attempts to redeclare the inherited $csrfToken property with a different type (AnonymousCsrfTokenGenerator instead of CsrfTokenGenerator). This violates PHP's property inheritance rules, which became stricter in PHP 8.3.
Proposed resolution
Remove the property redeclaration from AnonymousCsrfAccessCheck class - the property is already inherited from the parent class
Ensure AnonymousCsrfTokenGenerator extends CsrfTokenGenerator to maintain type compatibility
Update the constructor to properly inject the anonymous token generator while respecting the parent class's property typing
Code changes needed:
In src/Access/AnonymousCsrfAccessCheck.php:
Remove the protected $csrfToken property declaration
Keep only the constructor that assigns the injected service to the inherited property
In src/Access/AnonymousCsrfTokenGenerator.php:
Ensure the class extends Drupal\Core\Access\CsrfTokenGenerator
Override necessary methods to provide anonymous user functionality
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | AfterMR.png | 63.43 KB | anirudhsingh19 |
| #5 | BeforeMR.png | 66.51 KB | anirudhsingh19 |
Issue fork anonymous_token-3548070
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 #3
andres alvarez commentedComment #4
anirudhsingh19 commentedreviewing it
Comment #5
anirudhsingh19 commentedI was able to reproduce the issue on Drupal 11.2.4 with PHP 8.3.23. After installing the module, the reported error does appear. After applying the MR, the error is no longer present. I’ve attached before and after screenshots, which confirm that the MR resolves the issue. Hence, moving it to RTBC.
Comment #6
baikho commentedComment #8
baikho commented