Problem/Motivation

Drupal entity ids, even when defined as auto-increment id, notoriously get returned as string int like "123" instead of a real int like 123.

A simple fix is to at least have the ->id() methods return true integer.
This will help with phpstan, and also allows to use proper parameter types.

Similar attempts are happening in Drupal core, see #3224376: UserInterface::id(), AccountInterface::id() should return int to match typehints.

Steps to reproduce

Proposed resolution

Create a trait and interface for entities where ->id() returns real integer (or NULL).

Mark as internal, so that we can later replace it when a solution is provided by Drupal core.

Remaining tasks

User interface changes

API changes

Data model changes

Command icon 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

donquixote created an issue. See original summary.

donquixote’s picture

Status: Active » Needs review

Should be not disruptive at all.

fmb made their first commit to this issue’s fork.

teebeecoder’s picture

Status: Needs review » Closed (won't fix)

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

teebeecoder’s picture

Hi @donquixote,

Thanks for the work on this and for opening the MR.

At this stage, we’d prefer not to merge this change yet. Since similar efforts are already ongoing in Drupal core (see the related issue as you spotted it), we want to avoid duplicating the work and potentially having to refactor everything again once a core solution is available.

For the PHPStan concerns, we suggest handling them on purpose at the analysis level by ignoring the relevant cases where needed. In the code itself, we can add explicit checks for empty or non-existent IDs where it makes sense, to ensure safety without enforcing a custom interim API.

This approach should keep things stable for now while allowing us to align more cleanly with the core solution once it lands.

Thomas

teebeecoder’s picture

Issue tags: +drupalcampFR2026
donquixote’s picture

At this stage, we’d prefer not to merge this change yet. Since similar efforts are already ongoing in Drupal core (see the related issue as you spotted it), we want to avoid duplicating the work and potentially having to refactor everything again once a core solution is available.

The difference is that Drupal needs to worry about BC and about edge cases like AccountInterface::id().
In this module we do not have these concerns.
No matter how this will be solved in core, I don't see how it would conflict with the solution proposed here.

donquixote’s picture

Status: Closed (won't fix) » Active
fmb’s picture

I am afraid I am not convinced we should remedy this kind of core typing issues at our level by adding this kind of layer (trait and so on). I agree with @TeeBeeCoder about «add[ing] explicit checks for empty or non-existent IDs where it makes sense».

donquixote’s picture

Status: Active » Closed (won't fix)

Alright let's close it by majority.
I would do it if it was my own module that I maintain myself, but it is ok.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.