Implement hook_dfp_tag_load_alter() and hook_dfp_tag_alter()

Created on 22 June 2021, over 3 years ago
Updated 6 February 2024, 11 months ago

Problem/Motivation

The Drupal 7 version of this module supported tag alteration:

  • Just after the tag object is loaded from the database.
  • Just after it was loaded for rendering.

This feature is currently missing in the current 2.0.x version of the module.

Steps to reproduce

Proposed resolution

Reimplement both hooks in the current 2.0.x branch.

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Needs work

Version

2.0

Component

Code

Created by

🇮🇳India Dinesh18

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

Merge Requests

Comments & Activities

Not all content is available!

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

  • 🇩🇪Germany Grevil

    I don't think hook_dfp_tag_load_alter(), makes sense in D10.

  • Issue was unassigned.
  • Status changed to Needs review 11 months ago
  • 🇩🇪Germany Grevil

    Done. Please review the provided MR!

  • Status changed to RTBC 11 months ago
  • 🇩🇪Germany Anybody Porta Westfalica

    Thanks @Grevil! Looks and works great!

  • 🇩🇪Germany Grevil

    Current MR as a static patch until it is merged.

  • Status changed to Needs work 9 months ago
  • 🇩🇪Germany Anybody Porta Westfalica

    Just found an issue with this implementation: 🐛 Tag altering is not represented in caching (= wrong ads targeting shown) Needs work

    While it's super important to have the flexibility this hook provides, it doesn't care for caching.
    I'm not even sure if the issue described doesn't also happen in other cases already, but at least for this MR the differences in targeting for the same tag need to be taken into account!

    We may decide to merge that other issue in here and fix it here. But first we need to find a correct solution.

    Any ideas everyone here?

  • Pipeline finished with Failed
    3 months ago
    Total: 160s
    #290285
  • Pipeline finished with Failed
    3 months ago
    Total: 168s
    #290372
  • 🇩🇪Germany Grevil

    I added a test trying to replicate the caching issue, but everything seems fine, maybe checking the response isn't the correct approach.

    FYI: The ci-pipeline is a complete mess in dev. The pipeline errors are unrelated, the added test succeeds.

  • 🇩🇪Germany Grevil

    Nice, now we have a failing test reproducing this issue!

  • Pipeline finished with Failed
    3 months ago
    Total: 228s
    #290431
  • 🇩🇪Germany Grevil

    Ok just found this: https://www.drupal.org/docs/drupal-apis/cache-api/cache-max-age#s-what:

    \Drupal\Core\Cache\Cache::PERMANENT (value -1) means cacheable forever, i.e. this will only ever be invalidated due to cache tags. (In other words: ∞, or infinite seconds.)

  • Pipeline finished with Failed
    3 months ago
    Total: 174s
    #290544
  • Pipeline finished with Failed
    3 months ago
    Total: 168s
    #290551
  • Pipeline finished with Failed
    3 months ago
    Total: 170s
    #291143
  • Pipeline finished with Canceled
    3 months ago
    #291147
  • Pipeline finished with Created
    3 months ago
    #291151
  • Pipeline finished with Failed
    3 months ago
    Total: 160s
    #291148
  • Pipeline finished with Failed
    3 months ago
    Total: 157s
    #291149
  • Pipeline finished with Failed
    3 months ago
    Total: 159s
    #291156
  • Pipeline finished with Failed
    3 months ago
    Total: 167s
    #291202
  • 🇩🇪Germany Grevil

    @Anybody and I took a deeper look into this whole endeavour. The problem to begin with was, that "TagViewBuilder" did not add cache keys based on the dfp tag targeting (as this wasn't necessary before). Even after adding them, the caching wasn't fixed.

    Then we found out, that the TagBlock "build()" wasn't executed again after reloading the page.

    Turns out, the caching logic on the block hindered us to even reach the TagViewBuilder "build()" method, as the caching keys set in the TagViewBuilder do not bubble up to the block (caching keys do not bubble up in general, where as cache context does). We tried to simply add caching keys to "TagBlock" build(), but this only added cache keys to the block content, not to the block itself. We then tried to implement "hook_block_build_BASE_BLOCK_ID_alter" to add the proper caching keys to the block (see here).
    This worked perfectly, but unfortunately the hook fires before the content gets passed to the block, meaning we do not have any information about the dfp tag in the alter hook.

    Now in the final approach, we simply set the block cache max age to "0", IF the alter hook is implemented. Otherwise, the original max age is used. This way (if the alter hook is implemented) we delegate the block level caching (TagBlock) to the content level caching (TagViewBuilder).
    Both @Anybody and I feel like this is the best approach, (since the approach through "hook_block_build_BASE_BLOCK_ID_alter" isn't working).

    Another approach would be, to simply create a new "dfp_tag_targeting" cache CONTEXT, as cache contexts simply bubble up compared to cache KEYS. But a cache context for this specific scenario seems like the incorrect approach:

    Cache contexts = (request) context dependencies

    ( https://www.drupal.org/docs/drupal-apis/cache-api/cache-contexts )

    Dynamic tag targeting through the hook implementation doesn't have much to do with "REQUEST context dependencies". As it has nothing to do with the request itself (unlike other core contexts like "theme","cookies","language", etc.) and there can be multiple seemingly identical blocks with different tag targeting in one request. Maybe this is an edge case, where it is fine to create a dedicated cache context, even if it isn't linked to a "Vary header" (request cache context), but this isn't documented anywhere, whether there are edge cases like this.

  • 🇩🇪Germany Grevil

    Please review.

  • 🇩🇪Germany Anybody Porta Westfalica

    Whao that was pain... but with happy end! :)

    Thank you for the great investigations we had together @grevil! So I can happily confirm this is RTBC!
    This is tested, tests fail without the cache fixes and so we can be sure it works as expected.

    If we'll ever come to a better solution, which I currently can't see by hard, we can do that.

    Other failing tests / broken pipeline is unrelated, as written above.

  • 🇩🇪Germany Anybody Porta Westfalica

    anybody changed the visibility of the branch 3220030-approach-bubble-up-keys-to-block to hidden.

  • 🇩🇪Germany Anybody Porta Westfalica

    Static patch attached, until this is merged!

Production build 0.71.5 2024