If a Node is created in German but the field is not translatable it looks like this:
$node->langcode = "de";
$node->field_text['und'][0]['value'] = "value";
Now if we use the Entity Getters:
$node = node_load(1);
print $node->get('field_text');
There is nothing returned, because in "Entity::getFieldLangcode" the langcode returned for the field is "de" (the language of the node), but actually the array key for the field is "und".
So my suggestion would be to return LANGUAGE_NOT_SPECIFIED all the time (see patch).
Comment | File | Size | Author |
---|---|---|---|
#46 | drupal8.entity-getfieldlangcode.46.interdiff.do_not_test.patch | 1.08 KB | plach |
#46 | drupal8.entity-getfieldlangcode.46.patch | 6.94 KB | plach |
#39 | drupal8.entity-getfieldlangcode.39.patch | 6.87 KB | Gábor Hojtsy |
#31 | interdiff.txt | 1.95 KB | Gábor Hojtsy |
#31 | drupal8.entity-getfieldlangcode.31.patch | 6.84 KB | Gábor Hojtsy |
Comments
Comment #1
Schnitzel CreditAttribution: Schnitzel commentedadding tags
Comment #3
Schnitzel CreditAttribution: Schnitzel commenteduups
Comment #4
Gábor HojtsyHere is a figure I did before to help people understand the meaning of not defined language. For this scenario, the "non-translatable field" stands, which is the rightmost one, where it says "All shared/non-translatable fields LANGUAGE_NONE only, cannot have multiple variants". Since this condition is where you end up with a non-translatable field, it should not inherit the node language because it is in fact stored with no language per definition. That is the background of this patch.
Still needs tests.
Comment #5
Schnitzel CreditAttribution: Schnitzel commentedjust one small addition
LANGUAGE_NONE was renamed to LANGUAGE_NOT_SPECIFIED
Comment #6
Schnitzel CreditAttribution: Schnitzel commentedas discussed with @fago he agrees, that it should return LANGUAGE_NOT_SPECIFIED.
Also discussed with @fago and @gabor, we throw an exception when getFieldLangcode() is called on a non-translatable field with a langcode other than NULL or LANGUAGE_NOT_SPECIFIED.
Updated tests for this exception
Comment #7
Schnitzel CreditAttribution: Schnitzel commentedmhh slightly wrong patch.
this is the right onw
Comment #8
Gábor HojtsyShould this be thrown from set() only? Sounds like if you request some language that is not present, we should not freak out. The code above falls back on default language for example, no?
an field => a field
} and catch should be separated by newline :)
Comment #9
Schnitzel CreditAttribution: Schnitzel commentedas discussed with @gabor, we only throw an exception in the setters not in the getters, in the getters we return NULL.
also fixed the small issues.
Comment #10
plachLooks good, thanks.
Comment #11
tim.plunkettSorry to set this back, but
This should use format_string().
Comment #12
Gábor HojtsyTagging.
Comment #13
c31ck CreditAttribution: c31ck commentedAdded format_string().
Comment #14
tim.plunkettLooks good, thanks.
Comment #15
Gábor HojtsyThis deals with the rapidly changing entity landscape and is at a definite risk of commit conflicts.
Comment #16
Gábor Hojtsy#13: 1738368-13.patch queued for re-testing.
Comment #17
webchickHere's my review; mostly docs stuff. In general, my concern is that there's a bunch of crazy-assed stuff in #4 that's not communicated at all here. I don't think we need to move all of that into this code, but we certainly should move enough of it in so someone coming into these tests/functions without the benefit of having seen this issue will have an idea what's going on.
Ok, I can mostly make that out from reading that line of code. Why do we do that?
I guess this wasn't introduced in this patch, but we need PHPDoc of this new parameter. I have no idea why I might or might not want to set that to FALSE. Might as well document all of the parameters while we're here, and shorten that description to 80 chars.
"and only works with" rather than "does only work with"
Minor, but is there a reason to pull $field['field_name'] into a variable if you're only using it once?
Comment #18
YesCT CreditAttribution: YesCT commentedAttempted addressing webchick's comments from #17. Docs/comments need review by someone who really understands this to see if I got them right.
Comment #19
YesCT CreditAttribution: YesCT commentedoops. a little trailing whitespace problem. Ignore the patch in #18 and look at this one.
Comment #20
Gábor HojtsySubtagging for D8MI.
Comment #21
c31ck CreditAttribution: c31ck commentedThis looks good to me, a few minor remarks:
"Determine the language code" should be "Determines the language code". See http://drupal.org/node/1354#functions for more information.
I was thinking of something like this:
If the entity is language-specific and the field is translatable, the langcode is used to access the field value. Otherwise LANGUAGE_NOT_SPECIFIED should be used to access the field value.
When the field is not translatable and the langcode attempted is not LANGUAGE_NOT_SPECIFIED an exception is thrown in strict mode, or NULL is returned in non-strict mode.
This param is optional and should be documented as such:
(optional) The language code attempting to be applied to the field.
This param is optional so the description should start with (optional).
I was thinking of:
(optional) TRUE if an exception should be thrown when an invalid langcode is used on a non-translatable field, FALSE if NULL should be returned. Depending on the context in which this function is used, we want to be more strict. For example, the EntityInterface set() uses $strict TRUE, and the EntityInterface get() uses $strict FALSE.
The langcode if appropriate, LANGUAGE_NOT_SPECIFIED for non-translatable fields, or NULL when an invalid langcode was used in non-strict mode.
It might be a good idea to document the data types on the @param and @return directives. For example:
@return string|null
Comment #22
YesCT CreditAttribution: YesCT commentedThis patch includes most of the suggestions from #21. I moved ... as in the case of a field shared among all language version of an entity... to the general description comment. Keeping that because I think it is part of what webchick asked for when she said to try and include some of the information from the graphic in #4. I looked at being more specific with specifying the types (http://drupal.org/node/1354#functions) but I wasn't clear on if most of the @param were just "strings"... and when I looked elsewhere in core for examples, like: core/modules/field/field.info.inc, I didn't find types specified for some of the non trivial params.
Comment #23
Gábor HojtsyI agree we don't need to fix all of the problems of existing docs in a bugfix patch :) I think the documentation updates look good, so should be back at RTBC.
Comment #24
Gábor HojtsyExplained this in IRC in more detail to webchick, sun and YesCT:
Comment #25
Gábor HojtsyIt also does not apply due to the entity system being moved around.
Comment #26
Gábor HojtsySame patch rerolled.
Comment #27
Gábor HojtsyRetitled for the problem and elevating bug since this is a pretty big deficiency, it would mean the entity getter does not work on single language sites for untranslatable fields, which is a pretty big portion of Drupal's user base.
Comment #28
sunTried my best to improve the docs, but I'm not sure whether everything is correct.
Comment #29
Gábor HojtsyGreat improvements!
"field's values are shared among all language versions" is the same as "non-translatable field". We might want to use this wording even though its used again in the next paragraph just so that its absolutely clear.
I think its a great improvement either way, and you might just wanted to avoid repetition, so its fine as-is too.
Comment #31
Gábor HojtsyUpdated patch to fix the exception name in the test as well and correct the phpdoc description as explained above.
Comment #33
Gábor HojtsyThe fail is due to #1779638: Unexplained test failure in LocaleFileImportStatus->testBulkImportUpdateExisting(), testing again.
Comment #34
Gábor Hojtsy#31: drupal8.entity-getfieldlangcode.31.patch queued for re-testing.
Comment #36
Gábor Hojtsy#31: drupal8.entity-getfieldlangcode.31.patch queued for re-testing.
Comment #37
Gábor HojtsyFinally no unrelated fails, should be back to RTBC.
Comment #38
plach@Gabor:
I think we are missing "when no language is explicitly provided."
Comment #39
Gábor HojtsyThat was easy :)
Comment #40
plachThanks :)
Comment #41
Lars Toomre CreditAttribution: Lars Toomre commentedSmall nit to be incorporated if this patch is re-rolled.
Both of these @param directives for optional variables need to document what their default values are.
Comment #42
Gábor Hojtsy@Lars: Thanks, can you help with that?
Comment #43
Lars Toomre CreditAttribution: Lars Toomre commentedAt the moment, I cannot roll any patches @Gabor.
Comment #44
Gábor HojtsyLet's get this in then and people can tweak this however they want in the future.
Comment #45
plachI'll do
Comment #46
plachComment #47
Lars Toomre CreditAttribution: Lars Toomre commentedThanks @platch. Those additions look good.
Comment #48
webchickAwesome; great work on the docs in this patch, folks. That's much clearer now.
Committed and pushed to 8.x.
Comment #49
Gábor HojtsyNot on sprint anymore. Thanks all!
Comment #51
chx CreditAttribution: chx commentedRemoving Avoid commit conflicts tag
Comment #53
chx CreditAttribution: chx commentedtry #2