Unlike hook_field_attach_*() prepare/view hooks, hook_field_*() prepare/view hooks have no access to the requested language, they just get the actual language to be used for the current field. In the case of an untranslatable field, this prevents to implement a localized formatter, e.g. a date formatted with the current locale settings.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

plach’s picture

Status: Active » Needs review
FileSize
11.63 KB

The attached patch adds the parameter $request_language to the hook_field_attach_*() prepare/view hooks.

Status: Needs review » Needs work

The last submitted patch, tf-1178500-1.patch, failed testing.

plach’s picture

Status: Needs work » Needs review
sun’s picture

#1: tf-1178500-1.patch queued for re-testing.

Status: Needs review » Needs work
Issue tags: +translatable fields, +API addition

The last submitted patch, tf-1178500-1.patch, failed testing.

Damien Tournoud’s picture

Status: Needs work » Needs review
FileSize
11.63 KB

Re-uploading the same patch.

Another use case: a localized taxonomy term referenced by a untranslatable taxonomy term reference field.

Status: Needs review » Needs work

The last submitted patch, tf-1178500-1.patch, failed testing.

plach’s picture

@DamZ

a localized taxonomy term referenced by a untranslatable taxonomy term reference field.

This is a more valid use case: in mine we should be using the current interface language instead of the requested content language. I'll update the PHP docs accordingly once the bot has reviewed the patch.

Damien Tournoud’s picture

Status: Needs work » Needs review
FileSize
11.63 KB

New try.

plach’s picture

FileSize
12.1 KB

Replaced the example in the PHP docs with the use case from #6.

plach’s picture

FileSize
10.87 KB

Removed a couple of unnecessary changes.

Status: Needs review » Needs work
Issue tags: -translatable fields, -API addition

The last submitted patch, tf-1178500-11.patch, failed testing.

plach’s picture

Status: Needs work » Needs review
Issue tags: +translatable fields, +API addition

#11: tf-1178500-11.patch queued for re-testing.

No failures on my box.

Damien Tournoud’s picture

Status: Needs review » Reviewed & tested by the community

Issue summary

When rendering a field, the Field API calls field_language() to select the best language to render the field based on both (1) the requested rendering language, (2) the languages the field is defined in. The field is then rendered, but the requested rendering language is not passed to the field formatter.

As a consequence, there is no way for the formatter to take the correct decision when rendering sub-objects. For example: if you have a "taxonomy term reference" field attached to a node, you might want this field not to be translatable (because the terms are always the same in every language) but you still want to render the terms in the display language of the node.

API changes

This patch is fully backward compatible.

It introduces small API addition by adding a new $requested_language parameter to a bunch of hooks.

Why is this patch important

It allows building proper formatters, thus unlocking more of the i18n niceties of Drupal 7 :)

plach’s picture

Issue tags: +Needs backport to D7

Obviously this should be backported to D7.

webchick’s picture

Status: Reviewed & tested by the community » Needs work

Same question as the other issue; if this is fixing a bug, we ought to be able to test it, right?

(It's fine to push back on this, btw. I just get nervous when there's this much code flinging around and no safety net. We ended up introducing notices into 7.2 for a similar thing.)

webchick’s picture

Issue tags: +Needs tests
plach’s picture

Well, I did not add tests exactly because, as in the other issue you mentioned, I thought it was overkill.

Let me just note that IMO my mistake in #1089174: Prepare view hooks do not receive the language parameter has been not to split the issue into one fixing the bug and another one adding the extra language parameter. Notices arose from the former, the latter was totally harmless as this one should be.

That said, I'll try to provide tests, but I guess this is a D7.4 issue now...

ytsurk’s picture

any news ?
can i help in a way ?

here a clear test case is offering: #1121722: Improve DX of instance translation

muschpusch’s picture

subscribing. When it's backported to D7 i'm willing to test it!

andypost’s picture

Status: Needs work » Needs review

Should we close this issue? language parameter been removed in #1089174: Prepare view hooks do not receive the language parameter

I think the formatter should know language to pass it to sub-objects. Another example a node back reference field which renders referred nodes - probably this case could be solved on load stage, seems the same for taxonomy reference field

valthebald’s picture

Status: Needs review » Closed (works as designed)

I second #21