Deprecate magic ENTITY_TYPE_last_viewed functions

Created on 16 July 2025, 11 days ago

Problem/Motivation

\Drupal\comment\CommentManager::getCountNewComments has some magic to find functions with the pattern ENTITY_TYPE_last_viewed.

The code looks like this, and has existed since we added history.module to core , and we removed node_last_viewed.

$function = $entity->getEntityTypeId() . '_last_viewed';
if (function_exists($function)) {
  $timestamp = $function($entity->id());
}

There is a possibility existing contrib or custom modules are relying on this behaviour, but if these functions exist in .module files they will stop working once .module files are deprecated and removed. By deprecating now these can be updated to handle this in a supported way.

Steps to reproduce

Proposed resolution

Add a deprecation if the function_exists call returns true. Unsure what the replacement is, it would probably need to depend on #3267015: Make History storage support any entity type . In the meantime maybe such entity types should implement an interface for this method. Or perhaps we deprecate with no replacement.

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

📌 Task
Status

Active

Version

11.0 🔥

Component

comment.module

Created by

🇦🇺Australia mstrelan

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

Merge Requests

Comments & Activities

  • Issue created by @mstrelan
  • First commit to issue fork.
  • Merge request !12739Update file CommentManager.php → (Open) created by kalpanajaiswal
  • Pipeline finished with Success
    11 days ago
    Total: 526s
    #548705
  • 🇮🇳India kalpanajaiswal

    A MR raised to add the deprecation message.
    Deprecate Procedural Fallback (This Issue) DONE
    Add an @trigger_error() deprecation when function_exists() returns TRUE.

    This will alert contrib and custom module developers that they need to stop using *_last_viewed() functions before Drupal 12.

    No behavior change yet — just the deprecation message.

  • 🇮🇳India kalpanajaiswal

    Future replacement step
    Replace with a Service-based System
    In the long term, replace the dynamic *_last_viewed() fallback with a proper service-based system:

    Introduce a LastViewedTimestampProviderInterface.

    Allow entity-type-specific services using tagged service discovery (e.g., for nodes, media, custom entities).

    Make the logic injectable, overrideable, and fully testable — following modern Drupal architecture.
    Example

    $timestamp = $this->lastViewedManager->getLastViewedTimestamp($entity);
    

    Where lastViewedManager is a new core service that aggregates entity-type-specific providers.

  • 🇨🇭Switzerland berdir Switzerland

    The whole section is in a module exists check for history, there's an existing issue to move this out: 📌 Remove uage of history module from comment module Active as history is being moved out of core, this will need to be pluggable somehow, through a hook or something.

    I'd consider making this a duplicate of that issue.

  • 🇨🇭Switzerland berdir Switzerland

    crosspost on the status change, but the deprecation message doesn't make sense. Someone else cant just come up with a service to replace his, it's something that needs to be initiated by comment module.

  • 🇦🇺Australia mstrelan

    The whole section is in a module exists check for history, there's an existing issue to move this out

    That's a great point, perhaps we can let history.module handle this in contrib, e.g. by providing a hook.

    I'd consider making this a duplicate of that issue.

    Instead I've postponed it so it doesn't get lost, and moved it to history.module component. I think the sequence should go something like this:

    1. Move the existing code to history.module
    2. Deprecate history.module in core
    3. Move issues for history.module over to contrib
    4. Create a hook or other path forward
    5. Deprecate relying on the magic named functions
Production build 0.71.5 2024