Problem/Motivation
We have a major regression in 4.x:
The following example is still in 4.x
{# Render field_image from node 1 and instead of a view mode, provide an array of display options. #}
{# @see https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!EntityViewBuilderInterface.php/function/EntityViewBuilderInterface%3A%3AviewField #}
{{ drupal_field('field_image', 'node', 1, {type: 'image_url', settings: {image_style: 'large'}}) }}but the documentation says:
The Twig function previously declared `$view_mode` without a type; it is now
typed `string`. Passing anything other than a string (e.g. an integer) will
trigger a `TypeError`. Display options arrays go through the underlying
`twig_tweak.field_view_builder` service, which still accepts `string|array`.
https://git.drupalcode.org/project/twig_tweak/-/commit/8ef084f56d57a1d61...
so the example from above does NOT work any more, while I think it's widely used.
So it seems this was either
- done by mistake
- forgotten to add an alternative to
drupal_field()with an array parameter
but at least documentation mentions this change but without telling how to solve this correctly then and correcting the documentation.
Steps to reproduce
Implement the example and see an exception
{# Render field_image from node 1 and instead of a view mode, provide an array of display options. #}
{# @see https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!EntityViewBuilderInterface.php/function/EntityViewBuilderInterface%3A%3AviewField #}
{{ drupal_field('field_image', 'node', 1, {type: 'image_url', settings: {image_style: 'large'}}) }}Proposed resolution
Remaining tasks
User interface changes
API changes
Data model changes
Issue fork twig_tweak-3611411
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
anybody@chi sorry for the ping. I'd like to know if this was unfinished or done by mistake and what the final plan was... I've prepared a MR to add
array|to the$view_modeparameter like inFieldViewBuilderCan you tell?
Otherwise I'd just merge it to keep things compatible with 3.x
Comment #4
anybodyComment #5
akshay kashyap commentedThanks for working on this.
I reviewed the proposed fix and also tested it locally.
Before applying the MR, I was able to reproduce the issue using the example from the documentation:
}
As expected, it resulted in a TypeError because drupal_field() only accepted a string for the $view_mode parameter.
After applying the MR, the same example worked correctly without any changes, and I also verified that passing a standard view mode such as "full" continues to work as expected.
The change restores compatibility with the underlying twig_tweak.field_view_builder service, which already supports both a view mode string and a display options array, so this looks like the right approach to address the regression.
The only thing I'd suggest is adding a regression test covering both supported input types (string and array). Since the regression was introduced by a method signature change, having test coverage would help prevent it from happening again in the future.
Other than that, the fix looks good to me.
Comment #6
akshay kashyap commentedComment #7
anybody@chi any feedback from your side?
If not, I'd say let's add a test for this case and merge this @grevil?
The documentation can then stay as it is, or should we extend it?
Comment #8
anybodyPS: Failing test seems unrelated and should be fixed in a separate issue, right?
Comment #9
chi commentedDoes `drupal_field` still support inline options? If so it obviously should accept array for them along with string for view mode.
Comment #10
anybodyThanks @chi yes, then it's simply a bug, which I think you can also see from the comment here: https://git.drupalcode.org/project/twig_tweak/-/commit/8ef084f56d57a1d61...
That makes no sense to me. So possibly AI crap :)
Comment #11
anybodyComment #12
grevil commentedLGTM!
Test failures are unrelated. I'll fix them in a quick follow-up
Comment #13
grevil commentedComment #16
anybody@grevil thanks, yes please also add a test for this in the follow-up.