Tag altering is not represented in caching (= wrong ads targeting shown)

Created on 13 March 2024, 3 months ago

Problem/Motivation

Altering dfp tags like in the dfp.api.php:

/**
 * Alter the tag object just after it is loaded for rendering in a block.
 *
 * @param object $tag
 *   The tag object.
 */
function hook_dfp_tag_alter(&$tag) {
  // Adjust a tags add targeting:
  $customTargetDefinition = [
    'target' => 'my_target',
    'value' => 'my_value',
  ];
  $tagAddTargetting = $tag->targeting();
  $tag->set('targeting', array_merge($tagAddTargetting, [$customTargetDefinition]));
}

doesn't affect the cache, so even with different targeting Drupal thinks it's the same dfp tag and returns it from cache?

Steps to reproduce

See above.

Alter a dfp tag to add custom targeting. After cache rebuild only the first returned ad is being rendered with its targeting. All following ads from this tag are returned with that targeting instead of the right one.

This can for example be tested using a block.

Proposed resolution

Cache implementation in TagViewBuilder.php needs to take the different targeting into account for the hook to work as expected.
Current code doesn't seem to do that?

  /**
   * {@inheritdoc}
   */
  public function viewMultiple(array $entities = [], $view_mode = 'full', $langcode = NULL) {
    /** @var \Drupal\dfp\Entity\TagInterface[] $entities */
    $build = [];
    foreach ($entities as $tag) {
      // @todo Ensure a tag is only once on the page.
      // @todo Get cache-ability based on tokens used in TagView...
      $global_settings = $this->configFactory->get('dfp.settings');
      $tag_view = new TagView($tag, $global_settings, $this->token, $this->moduleHandler());

      $tag_id = $tag->id();
      $build[$tag_id] = [
        '#cache' => [
          'keys' => ['entity_view', 'dfp_tag', $tag_id],
        ],
      ];

      // Sort out the cache tags and contexts.
      $cacheable_metadata = CacheableMetadata::createFromObject($global_settings);
      $cacheable_metadata->merge(CacheableMetadata::createFromObject($tag));
      $cacheable_metadata->addCacheTags($this->getCacheTags());
      $cacheable_metadata->applyTo($build[$tag_id]);

      $build[$tag_id] += static::buildPreTag($tag_view);

    }

    return $build;
  }

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Needs work

Version

2.0

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

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

Merge Requests

Comments & Activities

Production build 0.69.0 2024