It's now totally superseded by the new Entity API and it's support for field language.

field_get_items($entity, $field_name, $langcode) --> $entity->getTranslation($langcode)->$field_name
field_get_items($entity, $field_name) --> $entity->$field_name

Comments

plach’s picture

Totally :)

fago’s picture

Agreed - totally ;-)

yched’s picture

Status: Active » Needs review
StatusFileSize
new5.12 KB

Patch, then.

plach’s picture

+++ b/core/modules/node/node.tokens.inc
@@ -137,17 +137,18 @@ function node_tokens($type, $tokens, array $data = array(), array $options = arr
+          if (($items = $node->getTranslation($langcode)->body) && !$items->isEmpty()) {

Currently this is correct. The end goal is passing around the correct translation object, so we don't have to instantiate it here.

yched’s picture

Well, in this case (node_token()), both the $node and the $langcode are received as entries in the $options param.
Are there plans to reshuffle tokens parsing & processing so that the $node directly arrives in the right language ?

Anyway, as you say, this seems the right code for the current state of HEAD ?

Status: Needs review » Needs work

The last submitted patch, field_get_items-2051721-3.patch, failed testing.

plach’s picture

Status: Needs work » Needs review

Well, in this case (node_token()), both the $node and the $langcode are received as entries in the $options param.

I think we should simply get rid of the $langcode options entry: if you can specify it, you can even pass the correct translation object. Otherwise things will apply to the default language which is the intended behavior.

Anyway, as you say, this seems the right code for the current state of HEAD ?

Well, now that I look more closely I think there would be a problem if $langcode is NULL (and right now it can): we can either replace the NULL value in line 101 with Language::LANGCODE_DEFAULT or make EntityNG::getTranslation() interpret a NULL value as Language::LANGCODE_DEFAULT. Actually I am not sure we want to encourage passing unchecked langcode values.

plach’s picture

Status: Needs review » Needs work

crosspost

yched’s picture

Status: Needs work » Needs review
StatusFileSize
new1.9 KB
new5.59 KB

I think we should simply get rid of the $langcode options entry: if you can specify it, you can even pass the correct translation object. Otherwise things will apply to the default language which is the intended behavior.

Hmm. I'm not changing the way tokens work in this patch :-)

make EntityNG::getTranslation() interpret a NULL value as Language::LANGCODE_DEFAULT.

Well, $entity->getTranslation() would look weird, woulnd't it ?

Status: Needs review » Needs work
Issue tags: -API change, -Field API, -Entity Field API

The last submitted patch, field_get_items-2051721-9.patch, failed testing.

yched’s picture

Status: Needs work » Needs review
Issue tags: +API change, +Field API, +Entity Field API

#9: field_get_items-2051721-9.patch queued for re-testing.

plach’s picture

Hmm. I'm not changing the way tokens work in this patch :-)

No, sure, I was just thinking loud :)

+++ b/core/modules/file/file.module
@@ -1975,8 +1975,8 @@ function file_get_file_references(File $file, $field = NULL, $age = FIELD_LOAD_R
+          if (!$match && ($items = $entity->get($field_name))) {

+++ b/core/modules/node/node.tokens.inc
@@ -137,7 +137,7 @@ function node_tokens($type, $tokens, array $data = array(), array $options = arr
+          if (($items = $node->getTranslation($langcode)->get('body')) && !$items->isEmpty()) {

Overall this looks good to me, can you explain why these changes were needed? Aren't __get() and get() functionally the same when we come to regular fields?

yched’s picture

can you explain why these changes were needed?

I was mixing the "$items = $entity->$field_name" syntax (which returns $items as nested arrays) with "$items->method() / $item->$column".
Need to pick one. Matter of taste, I picked the object syntax, since this is what most of the code works with.

I find this quite confusing, BTW ($entity->$field_name returns plain arrays, $entity->get($field_name) returns objects) :-)

plach’s picture

Status: Needs review » Reviewed & tested by the community

I find this quite confusing, BTW ($entity->$field_name returns plain arrays, $entity->get($field_name) returns objects) :-)

I think the BC decorator is the culprit (as usual), EntityNG::get($field_name) and EntityNG::__get($field_name) should return exactly the same value (a field item list object).

yched’s picture

Yes, I wasn't sure about that, which is also why I went with the syntax that consistently returns objects :-)

catch’s picture

Status: Reviewed & tested by the community » Needs work

I think we should mark this @deprecated now we're in API freeze. It's not a one-line wrapper so it'll need to go before release but we should give people some warning.

berdir’s picture

+++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/views/argument_default/Tid.phpundefined
@@ -139,8 +139,8 @@ public function getArgument() {
-            foreach (field_get_items($node, $name) as $item) {
-              $taxonomy[$item['tid']] = $field_info['settings']['allowed_values'][0]['vocabulary'];
+            foreach ($node->{$name} as $item) {
+              $taxonomy[$item->tid] = $field_info['settings']['allowed_values'][0]['vocabulary'];

I wouldn't be surprised if this would be broken too but we just don't have any test coverage...

yched’s picture

Status: Needs work » Needs review
StatusFileSize
new2.13 KB
new5.05 KB

@catch: added @deprecated. How do we keep track of which @deprecated we want to remove before release ?

@Berdir: indeed. Doubly broken, since taxo fields 'tid' property has been renamed 'target_id' a while ago...

Status: Needs review » Needs work
Issue tags: -API change, -Field API, -Entity Field API

The last submitted patch, field_get_items-2051721-18.patch, failed testing.

berdir’s picture

Status: Needs work » Needs review
Issue tags: +API change, +Field API, +Entity Field API

#18: field_get_items-2051721-18.patch queued for re-testing.

catch’s picture

@yched we don't have a way (yet). I opened #2042165: Add a 'deprecated' module that includes deprecated functions only when enabled to discuss stuff that's left in.

plach’s picture

Back to RTBC?

yched’s picture

Heh, yes, why not ;-)

plach’s picture

Status: Needs review » Reviewed & tested by the community
catch’s picture

Title: Get rid of field_get_items() » Change notice: Get rid of field_get_items()
Priority: Normal » Critical
Status: Reviewed & tested by the community » Active
Issue tags: +Needs change record

Committed/pushed to 8.x, thanks!

Needs change notice for the deprecation.

yched’s picture

Title: Change notice: Get rid of field_get_items() » Get rid of field_get_items()
Priority: Critical » Normal
Status: Active » Fixed
Issue tags: -Needs change record

Created a change notice - this mostly links back to the change notice for the Entity Translation API.

plach’s picture

Looks good :)

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