Support multiple entities passed to filter

Created on 21 June 2024, 6 months ago
Updated 4 July 2024, 6 months ago

One of the common issues in the queue seems to be people looking for the ability to add an entity from some other context to the filter, whether that be a paragraphs parent [PP-1] Support for Paragraphs module Postponed , the current route , or a parent node . The response to these requests is that it's outside the scope of the project, which I agree with. However, that does not mean we can't support an easy way for users to alter/add to what entities get passed to the filter.

Since the module is already making use of drupal_static() to set the entities passed to the token service, we already have a nice way of altering the data that ends up in the filter. My proposal is that we update that variable to be a simple array of entities instead of a single entity. This should provide a way to solve all of the above issues via custom code.

For example, if a user wanted to add the entity from the current route to the filter, and didn't want to patch the token module to include something like the suggested [current-page:object:*] Add a [current-page:object:?] dynamic token Needs work , they could do the following in a custom module:

/**
 * Implements hook_preprocess_field().
 */
function hook_preprocess_field(&$variables) {
  if (isset($variables['element']['#object']) && $variables['element']['#object'] instanceof ContentEntityInterface) {
    $route = \Drupal::routeMatch();
    $token_entities = &drupal_static('token_filter_entity', []);

    // Add route entities to a static cache for use in token replacement.
    foreach ($route->getParameters() as $parameter) {
      if ($parameter instanceof ContentEntityInterface) {
        $token_entities[] = $parameter;
      }
    }
  }
}

Supporting this is a pretty minor change (patch incoming) and doesn't go against the intent of the module in my opinion as we're essentially just supporting an alter for the entities that end up being passed to the filter.

Feature request
Status

Needs work

Version

2.0

Component

Code

Created by

🇨🇦Canada johnjw59@gmail.com

Live updates comments and jobs are added and updated live.
  • Needs documentation

    A documentation change is requested elsewhere. For Drupal core (and possibly other projects), once the change has been committed, this status should be recorded in a change record node.

  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

  • Issue created by @johnjw59@gmail.com
  • 🇨🇦Canada johnjw59@gmail.com

    Here's the promised patch. I left the drupal_static key as-is as others may already be targeting it and I didn't want to introduce any breakages.

  • Status changed to Needs work 6 months ago
  • 🇦🇺Australia darvanen Sydney, Australia

    Yeah sure, looks good to me and happy to support that. If you could just document it in the readme please?

    Ideally this would have a test too now that we're finally starting to build out a suite of them.

    Also patches are becoming a thing of the past, please feel free to make a merge request :)

  • 🇨🇦Canada johnjw59@gmail.com

    After using the patch I provided in #2 for a bit, I don't think it's the right approach. Making the data in the cached variable an array leads to it bloating as each field just adds to the array, never cleaning it out. This can lead to some content having the wrong entity passed to the token method.

    I think a better approach is to just add an alter there allowing other modules to tweak the data array before it gets passed to the token method. Attached is a patch doing just that (no interdiff as it's a completely different approach). This is also just a must simpler solution I feel.

    @darvanen - I will add further documentation and roll a proper MR with this tweak once I have some spare time!

  • 🇨🇦Canada johnjw59@gmail.com

    Whoops, missed instantiating the module handler service in that last patch. Here's the correct one!

  • 🇦🇺Australia darvanen Sydney, Australia

    Wonderful, thanks for following up with that :)

Production build 0.71.5 2024