Problem/Motivation
When using the | plain text Twig filter, this seems to throw an error if the field does not exist or is hidden via `Manage display`.
The website encountered an unexpected error. Please try again later.
TypeError: Argument 1 passed to Drupal\thunder_article\Twig\FilterExtension::plainText() must be of the type array, null given, called in /vendor/twig/twig/src/Environment.php(418) : eval()'d code on line 41 in Drupal\thunder_article\Twig\FilterExtension::plainText() (line 45 of profiles/contrib/thunder/modules/thunder_article/src/Twig/FilterExtension.php).
This type error seem be introduced since upgrading to Drupal 9.3
Steps to reproduce
- Install Thunder with Drupal 9.3
- Render
{{ content.field_non_existing | plain_text }} somewhere in twig.
Proposed resolution
Remove the strict typing of the filter processor.
From:
public static function plainText(array $value) {
To:
public static function plainText($value) {
Remaining tasks
User interface changes
API changes
Data model changes
Comments
Comment #2
szeidler commentedHere's a patch. It's also debatable if there should be a check for an empty value and early return in the function, but changing the method signature prevents the type error.
Comment #3
shagel commentedI have
$variables[....] = [
'#theme' => '.....',
'#label' => $node->label(),
.....
];
in theme
{% set label = label|plain_text|trim %}
And after updating i get the following error:
TypeError: Argument 1 passed to Drupal\thunder_article\Twig\FilterExtension::plainText() must be of the type array, string given, called in /vendor/twig/twig/src/Environment.php(418) : eval()'d code on line 39 in Drupal\thunder_article\Twig\FilterExtension::plainText() (line 45 of profiles/contrib/thunder/modules/thunder_article/src/Twig/FilterExtension.php).
--------
Changing the FilterExtension.php like #2 leads to another error
The website encountered an unexpected error. Please try again later.
TypeError: Argument 1 passed to Drupal\Core\Render\PlaceholderGenerator::canCreatePlaceholder() must be of the type array, string given, called in ...core/lib/Drupal/Core/Render/Renderer.php on line 326 in Drupal\Core\Render\PlaceholderGenerator->canCreatePlaceholder() (line 35 of core/lib/Drupal/Core/Render/PlaceholderGenerator.php).
Comment #4
daniel.bosenThank you for the patch! I created a PR on GitHub based on your patch. It evolved a bit...
Please take a look, if that would fix both of your issues:
https://github.com/thunder/thunder-distribution/pull/481
Comment #5
szeidler commentedThe changes of the pull requests are fixing the issue for me, thanks!
Since the code changes are following the code of the deprecated
render()method it should be safe to merge.Comment #6
daniel.bosenReviewed on GitHub
Comment #7
daniel.bosenThanks all!