Illegal offset type in isset or empty when rendering flag link for User as anonymous user

Created on 16 July 2025, 28 days ago
Updated 10 August 2025, 3 days ago

Problem/Motivation

When using Flag to allow users to follow User entities with anonymous flagging enabled, rendering the flag link (flag.link_builder->build(...)) as an anonymous user triggers a fatal error:

TypeError: Illegal offset type in isset or empty in Drupal\Core\Entity\EntityTypeManager-

Steps to reproduce

Create a flag on the User entity type.

Set scope: personal, enable anonymous flagging.

In code, call:
$flag = \Drupal::service('flag')->getFlagById('follow_blogger');
$link = \Drupal::service('flag.link_builder')->build($flag, $user, \Drupal::currentUser());

Visit the page as an anonymous user.

Expected result:
The follow/unfollow link is displayed or gracefully hidden for anonymous users.

Actual result:
Fatal error:

TypeError: Illegal offset type in isset or empty in EntityTypeManager->getHandler()

Additional info:

$user is a valid User entity.

\Drupal::currentUser() returns AnonymousUserSession (implementing AccountInterface).

The issue occurs deep in EntityTypeManager->getHandler(), indicating Flag is creating an invalid key.

Disabling explicit rendering for anonymous bypasses the error, but breaks anonymous UX.

Proposed resolution

Add a strict check in Flag’s FlagLinkBuilder::build() to skip or fall back when the current user is not a full entity.

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Active

Version

4.0

Component

Flag core

Created by

🇺🇸United States devgigas

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.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • 🇸🇮Slovenia jernejmramor

    Hello, I've investigated this issue and I think that the flag module is working correctly here.

    Code snippet that is added to description seems incorrect in how parameters are passed to build method:
    $link = \Drupal::service('flag.link_builder')->build($flag, $user, \Drupal::currentUser());

    If you check the interface of FlagLinkBuilder:
    1st parameter should be entity_type_id, which in this case should be 'user'.
    2nd parameter needs to be user id for which this link is being built.
    3rd parameter should be id of the flag you want to built link for, in your case you can use 'follow_blogger' or $flag->id()
    4th parameter would need to be $view_mode in which you want this rendered in.

    Can OP please verify whether he is passing parameters correctly or try using something like
    $link = \Drupal::service('flag.link_builder')->build('user', \Drupal::currentUser()->id(), $flag->id(), 'default');

Production build 0.71.5 2024