Support multiple entities passed to filter

Created on 21 June 2024, 6 days ago
Updated 23 June 2024, 5 days 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 5 days 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 :)

Production build 0.69.0 2024