Fatal error: Type of Drupal\anonymous_token\Access\AnonymousCsrfAccessCheck::$csrfToken must be Drupal\Core\Access\CsrfTokenGenerator

Created on 22 September 2025, about 1 month ago
Updated 23 September 2025, about 1 month ago

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

🐛 Bug report
Status

Needs review

Version

3.0

Component

Code

Created by

🇨🇴Colombia andresalvarez Bogota

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

No activities found.

Production build 0.71.5 2024