drupal_alter('field_attach_view', $result, $context) gets executed in function field_view_field() and in function field_attach_view().

Compared to function field_attach_view() the langcode is missing in $context in function field_view_field():

function field_attach_view($entity_type, EntityInterface $entity, $view_mode, $langcode = NULL) {

  // Let other modules alter the renderable array.
  $context = array(
    'entity_type' => $entity_type,
    'entity' => $entity,
    'view_mode' => $view_mode,
    'display' => $view_mode,
    'langcode' => $langcode,
  );
  drupal_alter('field_attach_view', $output, $context);
}

function field_view_field($entity_type, $entity, $field_name, $display = array(), $langcode = NULL) {

      // Invoke hook_field_attach_view_alter() to let other modules alter the
      // renderable array, as in a full field_attach_view() execution.
      $context = array(
        'entity_type' => $entity_type,
        'entity' => $entity,
        'view_mode' => '_custom',
        'display' => $display,
      );
      drupal_alter('field_attach_view', $result, $context);
}

This bug already exists in drupal 7 and breaks the indexing of nodes in apachesolr if the entity translation module is installed: #1820742: parameter $langcode of core function node_view() ignored
(The issues within Entity Translation have been fixed now, except this remaining bug in core.)

Attached you'll find a patch against drupal 8 where the bug still exists. After it gets committed a backport is required. But watch out, in drupal 7 the array key needs to be 'language' instead of 'langcode'.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

plach’s picture

Title: missing langcode in $context breaks module Entity Translation » Missing langcode in $context breaks module Entity Translation
Status: Needs review » Needs work

I am afraid we need some tests here.

plach’s picture

Issue tags: +Needs tests
mkalkbrenner’s picture

hm, I'm not familar enough with drupal 8 testing right now to create a reasonable test for that.

On the other hand the bug is obvious:

// Invoke hook_field_attach_view_alter() to let other modules alter the
// renderable array, as in a full field_attach_view() execution.
$context = array(
    'entity_type' => $entity_type,
    'entity' => $entity,
    'view_mode' => '_custom',
    'display' => $display,
    );
drupal_alter('field_attach_view', $result, $context);

According to the comment, function field_view_field() has do do the same as function field_attach_view(), but misses the langcode in context.

mkalkbrenner’s picture

FileSize
458 bytes

Meanwhile, here's the patch for d7.

webflo’s picture

Title: Missing langcode in $context breaks module Entity Translation » Missing langcode in $context in hook_ field_attach_view_alter()
Status: Needs work » Needs review
FileSize
1.15 KB

How about that?

webflo’s picture

Title: Missing langcode in $context in hook_ field_attach_view_alter() » Missing langcode in $context in hook_field_attach_view_alter()

Status: Needs review » Needs work

The last submitted patch, hook_field_attach_view_alter-langcode-1823306-5.patch, failed testing.

webflo’s picture

Status: Needs work » Needs review
FileSize
1.27 KB

Maybe field_valid_language($context['langcode'], FALSE); should be in field_view_field(). I am not sure.

Status: Needs review » Needs work

The last submitted patch, hook_field_attach_view_alter-langcode-1823306-8.patch, failed testing.

nils.destoop’s picture

This test should work.

nils.destoop’s picture

Status: Needs work » Needs review
swentel’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
719 bytes
2.68 KB

What's even worse is that field.api.php actually documents a langcode (previously language, renamed it in the patch) was passed :)

Reroll + RTBC

swentel’s picture

Issue tags: -Needs tests +Field API

Cleaning tags

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 9a53a21 and pushed to 8.x. Thanks!

David_Rothstein’s picture

Version: 8.x-dev » 7.x-dev
Status: Fixed » Patch (to be ported)
mkalkbrenner’s picture

Status: Patch (to be ported) » Needs review
FileSize
846 bytes

re-test my patch from #4

swentel’s picture

Status: Needs review » Reviewed & tested by the community

yes

David_Rothstein’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Needs work

The tests are missing from the D7 patch.

p-neyens’s picture

Test for the backport to D7.

plach’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 19: 1823306_19_field_attach_view_alter_langcode.patch, failed testing.

p-neyens’s picture

New patch that includes previous patches from #16 and #19.

p-neyens’s picture

Status: Needs work » Needs review
plach’s picture

Status: Needs review » Reviewed & tested by the community

Thanks!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 22: field-field_view_attach_alter_langcode-1823306-22.patch, failed testing.

Status: Needs work » Needs review
David_Rothstein’s picture

Status: Needs review » Reviewed & tested by the community

That was a bogus test failure, so back to RTBC for now. (I didn't review the patch myself.)

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 22: field-field_view_attach_alter_langcode-1823306-22.patch, failed testing.

Status: Needs work » Needs review
dcam’s picture

Status: Needs review » Reviewed & tested by the community

Back to RTBC.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 22: field-field_view_attach_alter_langcode-1823306-22.patch, failed testing.

Status: Needs work » Needs review
David_Rothstein’s picture

Status: Needs review » Reviewed & tested by the community

Testbot fluke.

David_Rothstein’s picture

Title: Missing langcode in $context in hook_field_attach_view_alter() » Language code is missing from $context when hook_field_attach_view_alter() is invoked from field_view_field()
Status: Reviewed & tested by the community » Fixed
Issue tags: +7.36 release notes

Committed to 7.x - thanks!

  • David_Rothstein committed 9ddfc46 on 7.x
    Issue #1823306 by mkalkbrenner, p-neyens, webflo, swentel, zuuperman:...

Status: Fixed » Closed (fixed)

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