hook_smart_trim_link_modify() does not do anything useful

Created on 30 April 2023, about 1 year ago
Updated 20 August 2023, 10 months ago

Problem/Motivation

Smart Trim implements a hook to allow the More link to be modified. However, this is invoked on the $link object, after $project_link = $link->toRenderable(); has been called to generate the renderable link.

Steps to reproduce

Create a module put the following in the hook function:

function my_module_smart_trim_link_modify(FieldableEntityInterface $entity, string &$more, Link &$link): void {
  $link->setText('Even more');
}

This should change the label of the More link, but does not affect it.

Proposed resolution

Either the invokeAll call for smart_trim_link_modify should be moved before $project_link = $link->toRenderable();...

Or $project_link should be passed in rather than $link so the hook can be applied to the link's render array. This could break some modules that are expecting the link object, but since modifying the link has no effect, this seems unlikely.

Remaining tasks

Decide which fix is appropriate.
Implement it.
Test.

User interface changes

None.

API changes

None.

Data model changes

None.

🐛 Bug report
Status

Closed: duplicate

Version

2.0

Component

Code

Created by

🇮🇪Ireland lostcarpark

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

Comments & Activities

  • Issue created by @lostcarpark
  • Assigned to markie
  • 🇺🇸United States ultimike Florida, USA

    I think it makes more sense to move the invokeAll() call to before $link = $entity->toLink($more);. This way, if a hook_smart_trim_link_modify() implementation modifies any of its three arguments, those changes are accounted for.

    So, from:

              $target = $more_settings['target_blank'];
              $link = $entity->toLink($more);
              $project_link = $link->toRenderable();
              $project_link['#attributes']['class'] = [$class];
    
              // Allow other modules to modify the read more link before it's
              // created.
              $this->moduleHandler->invokeAll('smart_trim_link_modify', [
                $entity,
                &$more,
                &$link,
              ]);

    to:

              $target = $more_settings['target_blank'];
    
              // Allow other modules to modify the read more link before it's
              // created.
              $this->moduleHandler->invokeAll('smart_trim_link_modify', [
                $entity,
                &$more,
                &$link,
              ]);
    
              $link = $entity->toLink($more);
              $project_link = $link->toRenderable();
              $project_link['#attributes']['class'] = [$class];

    Assigning to @markie for his thoughts.

    -mike

  • Issue was unassigned.
  • 🇺🇸United States markie Albuquerque, NM

    Makes sense to me.

  • 🇮🇪Ireland lostcarpark

    Is it worth adding a test?

  • 🇺🇸United States ultimike Florida, USA

    @lostcarpark,

    What did you have in mind for a test? Setting some config the usual way, then a test-only module that implements the hook, then confirming the output of formatter include the test module's changes?

    -mike

  • 🇮🇪Ireland lostcarpark

    I think to test you'd probably need a test module that implements the hook, and the test would install the module, trigger a trim operation, and verify that the hook has modified in the expected way.

  • Status changed to Closed: duplicate 10 months ago
  • 🇺🇸United States ultimike Florida, USA

    Duplicate of 🐛 Regression: API Hook No Longer Works Fixed .

    -mike

Production build 0.69.0 2024