Wrong usage of trim() in entity_print_entity_view_alter()

Created on 21 May 2024, 11 months ago

Problem/Motivation

In entity_print_entity_view_alter() we see a call to trim($export_type, '_engine').

The goal is to remove a '_engine' suffix from a an export type name.
However, what happens instead is that any of the characters that occur in '_engine' are removed from the end of the export type name.

E.g. trim('tree_engine', '_engine') is 'tr'.
https://3v4l.org/dVnuK

For now this is not a problem because none of the known export types end with any of these characters.

Steps to reproduce

You would have to define an export type plugin with an id that ends with "e", or another one of the characters in "_engine".

However, a much easier way to replicate the problem that _would_ occur is by changing the code in entity_print_entity_view_alter().
In the trim() call, simply add another character: '_enginef' instead of '_engine'.

      $route_params = [
        'entity_type' => $entity->getEntityTypeId(),
        'entity_id' => $entity->id(),
        'revision_id' => $entity->getRevisionId() ?? '',
        'export_type' => trim($export_type, '_enginef'),
      ];

Now visit a node page that would normally have the pdf link.

The link is not shown, until we remove the 'f' from the trim parameter.

Proposed resolution

I see two options:
1. Replace the `trim()` call with something that actually removes a suffix. E.g. `basename()` would work, although it seems strange in this context. Otherwise str_ends_with() + substr().
2. Check if the `trim()` if actually needed, and possibly remove it.

Remaining tasks

I also see another bad usage of trim() in FilenameGenerator::generateFilename().
This would go into a follow-up issue.

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇩🇪Germany donquixote

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

Comments & Activities

Production build 0.71.5 2024