Can logic be moved to post_save hook?

Created on 14 March 2024, 10 months ago
Updated 15 March 2024, 10 months ago

Problem/Motivation

I perhaps have a unique setup that causes this module to disrupt content saving actions in a Drupal site that contains:

  • Drupal 10.1
  • Acquia Cohesion (Site Studio) > 7.3
  • estimated_read_time:dev-1.0.x

The estimated read time module allows you to select the view mode that should be used when calculating read time. If any view mode is select, that has a template that contains a token that displays node data, such as [node:url], that uses computed node data like the node id, then:

You get white-screen of death errors when creating new content

I believe the reason why this is occurring is that you're trying to compute your value in an entity's pre_save hook. If a template that is used for the content has a tokens processed in templates (as is possible in Cohesion/Site Studio or many other contexts) then I don't see how it's possible to avoid this bug.

Steps to reproduce

  1. Include this line into a template for the view mode you're using for content that has an estimated_read_time field
    with cohesion:
    {% set cohvar0 = processtoken("[node:url]", {"node": node}, _context, true) %}
    
    with twig_tweak:
    {% set url = drupal_token('node:url') %}
    
  2. Next, try to create a new piece of content of that type.
  3. On save, you'll observe that you get a WSOD error that eventually refers to the token use that you just added.

Proposed resolution

There are a couple of solutions to consider

Can we move this logic to post_save?

The logic in EntityReadTimeEstimator::setEstimatedReadTime is currently executed during a pre_save hook. This logic accesses the active theme / template for the content time. The EntityReadTimeEstimator::doEstimate method does have some logic that appears to provide protection around this specific kind of error. I wonder if a rethinking of logic to execute post_save would dodge this issue completely.

Remaining tasks

Agree on a fix.

User interface changes

None

API changes

None

Data model changes

None

πŸ› Bug report
Status

Closed: won't fix

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States cosmicdreams Minneapolis/St. Paul

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

Comments & Activities

  • Issue created by @cosmicdreams
  • πŸ‡ΊπŸ‡ΈUnited States mtalt Maryland

    @cosmicdreams

    At one point, we were using hook_entity_insert, but the documentation says "Note that hook implementations may not alter the stored entity data.". Saving the entity again was causing other issues, such as webform handlers to be called more than once πŸ› Estimated Read Time module make webform send emails twice Fixed .

    Does your site also break on previewing a node? I've run into issues in the past where a NID is not always available in the template and we had to wrap code in a condition that checked if the NID was available. The NID will not be present when previewing a new entity. The code you see where we set that we are in a preview is because of similar functionality in core for nodes. This occurs in the links field where those links will not be rendered while previewing.

    Is Cohesion using that node ID to render content that would have a meaningful difference on the read time? If not, could you only render that content if a node ID is present? Alternatively, could you create a view mode dedicated for read time calculations that does not require a node ID in the template?

  • πŸ‡ΊπŸ‡ΈUnited States cosmicdreams Minneapolis/St. Paul

    I've found that condition to cover "in_preview" modes. Perhaps the code needs handle $entity->isNew() conditions.

    I see that Drupal 11 is considering including a entity post save hook: https://www.drupal.org/project/drupal/issues/2221347 ✨ Add hook_entity_postsave hook Needs work

    That would be cool, but it sounds like the guidance would remain to be that entities aren't re-saved during this event / hook.

    I think I'm going to pursue a fix that doesn't compile templates if the entity is new.

  • Status changed to Closed: won't fix 10 months ago
  • πŸ‡ΊπŸ‡ΈUnited States mtalt Maryland

    Sounds good @cosmicdreams. I'm going to close this issue, but feel free to re-open if you think of a viable alternative.

Production build 0.71.5 2024