Add support for Paragraph entities

Created on 31 December 2024, 21 days ago

Problem/Motivation

Assume we have a generic search index containing Nodes (Article, Advanced Page) and Paragraphs (various bundles containing title & body fields). Articles have a body field, Advanced Page has a multivalue paragraphs field. Articles are indexed. Advanced Page nodes are not indexed; their paragraphs though are indexed.
Upon clicking an article node, the user should get redirected to the node page; already supported by this module.
Upon clicking the paragraph entity, the user should get redirected to the advanced page node with the specific paragraph somehow set as active (eg. via a query argument). This functionality is supported by implementing in a custom module the hook_search_api_autocomplete_suggestions_alter() function... This is not enough though since the Live Results processor skips anything that doesn't have a canonical URL.
So to sum up, even though the paragraphs do come as a result from the index, the suggester doesn't display them.

Proposed resolution

From my point of view, there are two possible ways of solving this issue:

  • Add explicit support for Paragraph entities
    This has the downside that it opens the road for other module requests in the future which you might not be willing to do.
  • Introduce a new hook to modify the URL retrieved for the entity.
    This is much more simple and dynamic and with a proper description in the *.api.php file, nothing more should be required from your side.

Remaining tasks

Decide on the approach to use.

User interface changes

None.

API changes

Possibly new hook introduced.

Data model changes

None.

Feature request
Status

Active

Version

1.0

Component

Plugins

Created by

🇬🇷Greece vensires

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

Merge Requests

Comments & Activities

  • Issue created by @vensires
  • Merge request !39Resolve #3496743 "Hook implementation" → (Open) created by vensires
  • 🇬🇷Greece vensires

    In MR!39, I went on with the second proposed solution and introduced hook_search_api_autocomplete_suggester_item_url_alter().

    Feel free to change the hook's name if you desire it; I tried to avoid using the "live_results" name somehow in case it's used somewhere else in the future too.

  • Pipeline finished with Success
    21 days ago
    Total: 337s
    #382675
  • 🇦🇹Austria drunken monkey Vienna, Austria

    drunken monkey made their first commit to this issue’s fork.

  • 🇦🇹Austria drunken monkey Vienna, Austria

    Thanks for suggesting this improvement!
    You are right that I’d say adding explicit support for Paragraph entities would be a bit too specific a solution for my taste. So, making this easier for custom code to fix on its own seems like the better approach.

    I wanted to comment and say that we’ve moved to using events instead of hooks and to please create one of those instead – however, I then realized that we haven’t actually made that move in this module. I created 📌 Convert all hooks to events Active for that – in case this gets merged, we’d just have to port one more hook to an event. (Though it seems less than ideal to introduce a new hook and then perhaps deprecate it a few weeks later.)

    However, introducing a new hook for that also seems like a bit of an overkill, at least unless more people express interest in this functionality.
    A simpler approach would be to extract the $datasource->getItemUrl($object) call into its own method on the LiveResults suggester – that way, you could simply change the plugin’s class to a custom subclass where you override that method.
    Would that also work? It seems to me like that would be a less invasive solution to the problem, even though a bit more custom code would be needed as a downside.

  • 🇬🇷Greece vensires

    @drunken-monkey thank you for your really detailed response!

    I wasn't aware of 📌 Convert all hooks to events Active ; you are correct though that it would be better not to introduce a hook and then deprecate it. We could have this as an event directly instead. This way it would be ready for merging.

    I had already thought of overriding the original $datasource's class or the LiveResults class using hook_search_api_autocomplete_search_info_alter(&$info) {...} but it really was too much of a code to be duplicated just for this change. Your proposal from MR!40 is much simpler and future-proof. Let's go on with this one then.

    PS: I also update the issue summary with your solution.

Production build 0.71.5 2024