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

  1. done by mistake
  2. 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

Command icon 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

anybody created an issue. See original summary.

anybody’s picture

@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_mode parameter like in FieldViewBuilder

/**
   * Returns the render array for a single entity field.
   *
   * @see \Drupal\Core\Entity\EntityViewBuilderInterface::viewField()
   */
  public function build(
    EntityInterface $entity,
    string $field_name,
    string|array $view_mode = 'full',
    ?string $langcode = NULL,
    ?bool $check_access = TRUE,
  ): array

Can you tell?

Otherwise I'd just merge it to keep things compatible with 3.x

anybody’s picture

Title: [4.x] Regression: drupal_field() does not accept array as » [4.x] Regression: drupal_field() does not accept array as $view_mode parameter
akshay kashyap’s picture

Thanks 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:

{{ drupal_field('field_image', 'node', 1, {
  type: 'image_url',
  settings: {
    image_style: 'large'
  }
}) }

}

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.

akshay kashyap’s picture

Status: Active » Needs review
anybody’s picture

Assigned: chi » grevil
Issue tags: +Needs tests

@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?

anybody’s picture

PS: Failing test seems unrelated and should be fixed in a separate issue, right?

chi’s picture

Does `drupal_field` still support inline options? If so it obviously should accept array for them along with string for view mode.

anybody’s picture

Thanks @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 :)

anybody’s picture

Status: Needs review » Needs work
grevil’s picture

Assigned: grevil » Unassigned
Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs tests

LGTM!

Test failures are unrelated. I'll fix them in a quick follow-up

grevil’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • grevil committed 2b91cf37 on 4.x authored by anybody
    fix: #3611411 [4.x] Regression: drupal_field() does not accept array
    
anybody’s picture

@grevil thanks, yes please also add a test for this in the follow-up.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.