Allow altering the node link

Created on 5 September 2023, about 1 year ago
Updated 1 August 2024, 4 months ago

Problem/Motivation

We should allow changing the found node links (url & label) using an alter hook, in order to provide more context to the results. Example: for an event content type, append the event date to the label.

Proposed resolution

Add an alter hook, hook_anti_duplicates_node_link_alter.

✨ Feature request
Status

Needs review

Version

4.0

Component

Code

Created by

🇧🇪Belgium dieterholvoet Brussels

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

Merge Requests

Comments & Activities

  • Issue created by @dieterholvoet
  • Status changed to Needs review about 1 year ago
  • 🇧🇪Belgium dieterholvoet Brussels
  • Status changed to Needs work about 1 year ago
  • 🇳🇴Norway matsbla

    Thanks for the effort.

    I think we need to discuss this a little bit. My plan was to make it possible to configure a view mode for the results, in that case you could configure the display anyhow you want through the UI. However it should then be possible to configure different view mode for each content type.

    Any thoughts?

  • 🇧🇪Belgium dieterholvoet Brussels

    Sounds interesting. Would it still be possible to show results inline in a list though?

  • 🇳🇴Norway matsbla

    I guess it would, as that can be done using css, right? But if we have a patch to implement the feature we could spend a little time testing on it to see if different scenarios could be covered.

  • Status changed to Needs review 10 months ago
  • 🇧🇪Belgium dieterholvoet Brussels

    I don't think doing this using view modes is a good idea. View modes are good for rendering HTML, teasers or whole pages, but it isn't exactly made for rendering a simple string. I don't really know another place in core where view modes are used for something like this. Also, I don't see how we could make any rendered entity look good in a simple list with a bit of basic CSS provided by the module.

    How about we just commit the alter hook? It's ready to be committed and it's the simplest, most flexible solution IMO. If we want to improve further and add something like view modes later we can, but it shouldn't stop us from committing this now. Both solutions can exist together.

  • 🇧🇪Belgium dieterholvoet Brussels

    A good middle way could be to add a Node label setting where you can change what's displayed using tokens. What do you think?

  • 🇧🇪Belgium dieterholvoet Brussels

    Thinking about it, that still wouldn't allow people to set different displays per node type. Any chance we could just get this hook merged? I don't see the harm, we could still add another way to control this later. Alter hooks are all over Drupal.

  • 🇳🇴Norway matsbla

    I've started to make an implementation of the view modes, but didn't finish it yet.

    I think we could have a hook, but in that case it should not be just to change the label, but to take complete control of the output (label + link)

  • 🇧🇪Belgium dieterholvoet Brussels

    @matsbla maybe open a new issue/MR for the view mode feature to keep this one focused on the alter hook

  • 🇧🇪Belgium dieterholvoet Brussels

    I changed the hook_anti_duplicates_node_label_alter hook to hook_anti_duplicates_node_link_alter.

  • 🇧🇪Belgium dieterholvoet Brussels
  • 🇳🇴Norway matsbla
      if ($node->getType() === 'person') {
    
        $label .= sprintf(' (%s)', $node->get('field_last_name')->value);
    

    This seems like part of hard-coded custom code, perhaps should be removed from the patch?

  • 🇧🇪Belgium dieterholvoet Brussels

    @matsbla is this one good to go?

Production build 0.71.5 2024