Under certain circumstances
* a taxonomy term reference field is not displayed
* Watchdog shows the Notice: Undefined index: taxonomy_term in taxonomy_field_formatter_view() (line 1596 of taxonomy.module)

Analysis

It looks that in taxonomy_field_formatter_view() under certain circumstances $item['tid'] is set, but not $item['taxonomy_term'].

Original summary

There are several people reporting this issue in other projects:

The site I'm experiencing the error on doesn't run any contrib taxonomy modules, so it seems like a core issue.
I do run Display Suite on the site, but disabling it has no effect on the error.

I've created a patch that resolves the error on my site. I'll attach after creating this issue.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

agileadam’s picture

Sorry folks... looks like the new issue system automatically creates a comment when you post a new file. I would've named the file ...-1.patch.

agileadam’s picture

Issue summary: View changes
agileadam’s picture

Issue summary: View changes

Adding note about disabling Display Suite

kboopathi’s picture

Status: Active » Needs review
FileSize
551 bytes

taxonomy_term_load($item['tid'];

Status: Needs review » Needs work

The last submitted patch, 4: undefined_term_taxonomy_item-2128265-4.patch, failed testing.

kboopathi’s picture

Status: Needs work » Needs review
FileSize
552 bytes
agileadam’s picture

kboopathi, did you try the patch I posted? It'd be better to only call taxonomy_term_load() if needed, instead of all the time... right?

kboopathi’s picture

Sorry, i didn't read your patch before posting mine. Really your patch is good, and you have used conditional statement. i taught the variable was undefined, so i loaded $item['taxonomy_term']; directly .

agileadam’s picture

FileSize
609 bytes

No worries, kboopathi. I propose we stick with the first patch I posted. I'll repost here to try to trigger automated testing.

__cj’s picture

#9 worked for me.

Thanks.

nicholasThompson’s picture

#9 worked for me too

nicholasThompson’s picture

Status: Needs review » Reviewed & tested by the community

Should have said, this was against 7.26 and with display suite too.

jthorson’s picture

Version: 7.22 » 7.x-dev

Bumping version to ensure we're testing against the latest 7.x version.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 9: undefined_term_index-2128265-9.patch, failed testing.

jthorson’s picture

Status: Needs work » Needs review
jthorson’s picture

Status: Needs review » Reviewed & tested by the community

Triggered for retesting with proper issue version.

jamix’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
2.04 KB

I was experiencing the issue in #1970540: Notice: Undefined index: taxonomy_term in taxonomy_field_formatter_view() (line 1610 of and came across the patch in #9 here. The patch didn't fix the issue but the extended version (attached here) did. What I'm doing is extending the proposed failsafe handling of $item['taxonomy_term'] to the other two cases of the switch.

jorisx’s picture

Tested #17 on production site, #17 and it seemed to solve have my problem!
https://drupal.org/comment/8636171#comment-8636171

Thanks Jamix !!

jkr’s picture

Thanks. #17 Solved my problem too.

When editing a node, the preview didn't work. It threw this error:

EntityMalformedException: Missing bundle property on entity of type taxonomy_term. in entity_extract_ids() (line 7734 of /my/site/includes/common.inc)

chilic’s picture

Thanks. #17 Solved my problem too.

SlyK’s picture

#17 wokrs. thanks.
can't wait in core, I have many separate drupal installations that will need this patch))

geek-merlin’s picture

Title: Notice: Undefined index: taxonomy_term in taxonomy_field_formatter_view() (line 1596 of taxonomy.module) » Term field not displayed + Notice: Undefined index: taxonomy_term in taxonomy_field_formatter_view() (line 1596 of taxonomy.module)
Issue summary: View changes
Priority: Normal » Major
Status: Needs review » Reviewed & tested by the community
Related issues: +#2092883: Term field not displayed & Notice: Undefined index: taxonomy_term in i18n_taxonomy_field_formatter_view() in node preview

Added the main symptom to title & summary: the term name is not shown.
Raising prio as this is clearly a "significant repercussion".
Crosslinking the corresponding I18N issue #2092883: Term field not displayed & Notice: Undefined index: taxonomy_term in i18n_taxonomy_field_formatter_view() in node preview.

I can also confirm patch #17 fixes the issue for me.

Codewise: It looks that in taxonomy_field_formatter_view() under certain circumstances $item['tid'] is set, but not $item['taxonomy_term'].
The code in said patch is well readable and loads term from tid, but only if this is needed so so unnecessary performance penalty.

So RTBC.

EDIT: Verified this is autosolved in 8.x \Drupal\taxonomy\Plugin\Field\FieldFormatter\PlainFormatter::viewElements where $items is a FieldItemListInterface. So can be committed in 7.x

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 17: taxonomy-undefined_term_index-8172739-17.patch, failed testing.

fonant’s picture

Patch #17 worked for me, fixing the error on node preview.

Status: Needs work » Needs review
dcam’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 17: taxonomy-undefined_term_index-8172739-17.patch, failed testing.

Status: Needs work » Needs review
dcam’s picture

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

how do I check for the presence of this bug in 6.33?

None of my taxonomy fields show up in any of my content types or views?

worth saying that this was a theme specific setting I think...

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 17: taxonomy-undefined_term_index-8172739-17.patch, failed testing.

Status: Needs work » Needs review
dcam’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 17: taxonomy-undefined_term_index-8172739-17.patch, failed testing.

Status: Needs work » Needs review
dcam’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 17: taxonomy-undefined_term_index-8172739-17.patch, failed testing.

Status: Needs work » Needs review
dcam’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 17: taxonomy-undefined_term_index-8172739-17.patch, failed testing.

Status: Needs work » Needs review
dcam’s picture

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

Status: Reviewed & tested by the community » Postponed (maintainer needs more info)

$item['taxonomy_term'] should have already been set in taxonomy_field_formatter_prepare_view(). We need to get to the root cause of the bug.

If you're seeing this during a node preview or similar, I think it's likely that #1289336: Calling node_view for the same node with multiple view modes on the same page during node preview does not correctly attach fields (and possibly #2001308: Node preview removes file values from node edit form for non-displayed items) is the root cause...

Can anyone reproduce this in other situations and track down what is actually causing the bug - why $item['taxonomy_term'] isn't there?

dpolant’s picture

So I just ran into this while trying to run field_attach_view on a registration entity from Registration API. I don't think the entity type really matters.

I was trying to display fields on an entity using field_attach_view. It turns out this function does not run the prepare/prepare_formatter field hooks. I switched to using entity_view and the problem went away.

This surprised me because I thought field_attach_view was a good utility function for display all the fields on an entity. But if it doesn't run the prepare hooks, this is definitely not the case.

giupenni’s picture

#17 fix my problem

tedfordgif’s picture

This may be related to lack-of-referential-inegrity after deleting a taxonomy term (see #1281114: Database records not deleted for Term Reference Fields after Term is Deleted). Unfortunately, I don't have a working test case.

giupenni’s picture

Never mind, unfortunately the problem is still present.
Damned

EDIT:
I noticed that with patch the error message is still present but the output of taxonomy terms is shown correctly.

Is possible to remove only the error message?

giupenni’s picture

Well. The patch seems to work correctly, but if I remove a term from Vocabulary the remaining terms are displayed correctly but is still present the error message.
To remove the error message I must resave the node(is not necessary edit the fields).

marcoka’s picture

What i did

1.) created a view that loads fields of a node. the node id is passed by argument so the field url is node_data/%.
2.) if i ascces the view everything is ok when calling www.ulr.de/node_data/2

Then i took the module insert_view: https://www.drupal.org/project/insert_view
which inserts the output of a view into a node and is pretty useful.
Here is where the problems started because the error was thrown here.

in my case i embed a view inside a node with insert_views. in this case taxonomy_field_formatter_prepare_view is never called but taxonomy_field_formatter_view is and is missing the $term = $item['taxonomy_term']; the term id instead is avaliable in the array.

this error goes away in the most cases if you do the following:
go here: /admin/structure/types/manage/product_review/display
and display all taxonomy fields you have, chose link.

i found that the problem is that

 if (empty($entity->_field_view_prepared[$view_mode])) {

this is already set and then taxonomy_field_formatter_prepare_view is never called and the needed data isnt loaded.
i tried debugging this for 5 hours now. but i give up and will just use the patch as it fixes the problem

stevieb’s picture

the patch in #17 seems to have worked for me --- Thanks

fonant’s picture

Patch in #17 needed again for me, following Drupal core update, and it fixes the error when previewing a node, again.

fonant’s picture

Patch #17 needed to be applied again for me. Would be lovely to get this released properly...

Suspect it's only a problem for sites that have upgraded from Drupal 6 to Drupal 7.

Colin @ PCMarket’s picture

#17 patch worked for me.

I was receiving the following error when performing a search:

Notice: Undefined index: taxonomy_term in taxonomy_field_formatter_view() (line 1607 of /home/public_html/modules/taxonomy/taxonomy.module).

EntityMalformedException: Missing bundle property on entity of type taxonomy_term. in entity_extract_ids() (line 7773 of /home/public_html/includes/common.inc).

weri’s picture

Status: Postponed (maintainer needs more info) » Needs review
FileSize
2.35 KB

I got the same notice, when I switched the view mode with hook_entity_view_mode_alter(). The full view mode does not display a taxonomy term reference field (is deactivated), but then I switch to another view mode, on which the taxonomy term reference field is displayed. Then I get the notice, because only the tid is available, as described in the issue.

It seems that only the activated fields are loaded completely. I'm not sure is this a core or a Display Suite problem.

The patch #17 solves this problem.

I updated the patch to solve the problem, when a term is deleted (see comment #48).

giupenni’s picture

Thank you Weri, the patch #54 seems to work correctly.

cilefen’s picture

@giupenni Would you like to offer a review so this can be committed?

GStegemann’s picture

I have tested patch #54 and it fixes the problem on my test site.

knalstaaf’s picture

I second that (#57).

BD3’s picture

#54 works for me as well. Applied it on a live site that was not using Display Suite at all.

cilefen’s picture

It is great that this patch is working for most. Would anyone like to offer a review so this can be committed?

GStegemann’s picture

Would anyone like to offer a review so this can be committed?

I will try.

Before I mark this patch as RTBC, one question:

+++ b/modules/taxonomy/taxonomy.module
@@ -1604,7 +1604,11 @@ function taxonomy_field_formatter_view($entity_type, $entity, $field, $instance,
+          // Be aware of deleted terms
+          if($term === FALSE) {
+            continue;
+          }

Wouldn't it be save enough as well just to change the above if statement to:

+++ b/modules/taxonomy/taxonomy.module
@@ -1604,7 +1604,11 @@ function taxonomy_field_formatter_view($entity_type, $entity, $field, $instance,
+          // Be aware of deleted terms
+          if(!$term) {
+            continue;
+          }
cilefen’s picture

+++ b/modules/taxonomy/taxonomy.module
@@ -1618,7 +1622,17 @@ function taxonomy_field_formatter_view($entity_type, $entity, $field, $instance,
+          // Be aware of deleted terms
+          if($term === FALSE) {
+            continue;

In addition to thinking about #61, if's need one space before the opening parenthesis.

weri’s picture

FileSize
2.26 KB

I updated the patch #54 to meet the code conventions (sorry about that) and as GStegemann mentioned in #61, "if($term)" is save enough and better as to check only NULL values.

Status: Needs review » Needs work

The last submitted patch, 63: taxonomy-undefined_term_index-8172739-63.patch, failed testing.

weri’s picture

Status: Needs work » Needs review
FileSize
2.33 KB

I fixed the wrong diff file path.

Status: Needs review » Needs work

The last submitted patch, 65: taxonomy-undefined_term_index-8172739-65.patch, failed testing.

weri’s picture

Status: Needs work » Needs review
FileSize
2.33 KB

So, I made a mistake in the if clause. It's fixed and the tests passed locally.

GStegemann’s picture

Thanks for updating your patch. I have re-tested it under core 7.39 and it still works.

However, the patch was probably created against core 7.38 or earlier.

[cm7]# patch -p1 < taxonomy-undefined_term_index-8172739-67.patch 
patching file modules/taxonomy/taxonomy.module
Hunk #1 succeeded at 1604 (offset -37 lines).
Hunk #2 succeeded at 1622 (offset -37 lines).
Hunk #3 succeeded at 1641 (offset -37 lines).

Shouldn't the patch rerolled againt core 7.39?

cilefen’s picture

It must be rolled against 7.x.

weri’s picture

FileSize
2.33 KB

Patch is rerolled against 7.x.

GStegemann’s picture

Status: Needs review » Reviewed & tested by the community

Re-tested and works for me. Thanks.

fonant’s picture

Works here, would be lovely to get this included in the next release!

cilefen’s picture

Status: Reviewed & tested by the community » Needs work

A new patch is needed with these documentation nitpicks:

  1. +++ b/modules/taxonomy/taxonomy.module
    @@ -1604,7 +1604,11 @@ function taxonomy_field_formatter_view($entity_type, $entity, $field, $instance,
    +          $term = isset($item['taxonomy_term']) ? $item['taxonomy_term'] : taxonomy_term_load($item['tid']);
    +          // Be aware of deleted terms
    +          if (!$term) {
    

    Inline comments must end with a period.

  2. +++ b/modules/taxonomy/taxonomy.module
    @@ -1618,7 +1622,17 @@ function taxonomy_field_formatter_view($entity_type, $entity, $field, $instance,
    +          $term = isset($item['taxonomy_term']) ? $item['taxonomy_term'] : taxonomy_term_load($item['tid']);
    +          // Be aware of deleted terms
    +          if (!$term) {
    

    Also, here.

  3. +++ b/modules/taxonomy/taxonomy.module
    @@ -1627,9 +1641,20 @@ function taxonomy_field_formatter_view($entity_type, $entity, $field, $instance,
    +          // Be aware of deleted terms
    +          if (!$term) {
    +            continue;
    +          }
    

    And here.

cilefen’s picture

Re: #73, I think those comments are unnecessary anyway.

ron_s’s picture

Thank you for patch #70, excellent work!

pthornhi6’s picture

Patch from #70 fixed this issue for me.

dalearyous’s picture

what directory do i need to be in to apply this patch?

fonant’s picture

@dalearyous - Drupal root directory. If you look at the patch contents you can see the pathnames of the files it will patch.

vegansupreme’s picture

Patch in @70 worked for me.

Rishi Kulshreshtha’s picture

Status: Needs work » Needs review

It seems that patch from #70 worked for most of us, hence marking this as "Needs review" for more tests.

abdhomsi’s picture

Patch #70 worked for me too with 7.42

Fabioc79’s picture

Patch #70 worked for me too with 7.43.
Thank you for your work!

ron_s’s picture

Per comments from @cilefen in #73 and #74, I've created a new version that removes the comments. Hopefully this allows us to move more quickly to having the patch included in a future release.

RgnYLDZ’s picture

All good with #70 on 7.43

Great work!

hitfactory’s picture

Seeing this on a site recently upgraded from Drupal 6.

Confirming patch in #83 also works.

dqd’s picture

Issue tags: +Performance

Hm, since this patch (thanks for the hard work!) is implementing 3 more conditional checks, it should be tested under heavy load of terms which is common on sites using terms regulary. A term form with Hierarchical select module and 500 nested terms with depth multiplied by this 3 addiitional condition checks should be checked against performance impact maybe.

ron_s’s picture

@diqidoq, I'm certainly open to anyone who would like to test. I don't have a site available with that quantity of nested terms, so if someone wants to give it a go and post results that would be great.

dqd’s picture

@ron_s: you can set up a test page @ Drupal simply test me. You can even apply patches there. It doesn't make sense that I do this for you because the site will expire maybe before you log in. I will do it once from my side and report back, but I would suggest to have more than one feedback on testing this.

Ahmed_Samir’s picture

#83 working for me on 7.44

weri’s picture

Status: Needs review » Reviewed & tested by the community

I expected this problem after upgrading to 7.50 and could apply the patch #83 without problems on 7.50.

I reviewed the patch and I think it's ready to commit.

fonant’s picture

Confirming patch #83 works for me.

Yazzbe’s picture

#83 works for me too.
D7.50 (+ display suite)

David_Rothstein’s picture

Status: Reviewed & tested by the community » Postponed (maintainer needs more info)

The scenario described in #54 sounds like the bug that was already fixed for core entities via #1968348: (Change notice update) hook_field_formatter_prepare_view does not make use of hook_entity_view_mode_alter causing major errors (and that entities provided by contrib modules can fix using the steps described in the change notice linked from that issue).

So this still seems to be fixing the symptom rather than the root cause, and I think my question in #43 still stands.

The most likely cause of this issue in other scenarios is code that called field_attach_view() without properly calling field_attach_prepare_view() first.... at least that's the case unless someone can provide clear steps to reproduce that show otherwise.

David_Rothstein’s picture

The scenario described in #54 sounds like the bug that was already fixed for core entities via #1968348: (Change notice update) hook_field_formatter_prepare_view does not make use of hook_entity_view_mode_alter causing major errors (and that entities provided by contrib modules can fix using the steps described in the change notice linked from that issue).

To clarify, that bug should be fixed for taxonomy terms attached to entities provided by Drupal core (e.g. nodes) in Drupal 7.33 and higher. For taxonomy terms attached to entities provided by contrib modules, it depends on whether they've updated their code as described in the change notice.

Yazzbe’s picture

#83 also works on D7.51 (+ display suite)

ron_s’s picture

@yazzybe, the comments from @David_Rothstein in #93 and #94 note that patch #83 is postponed without further information.

Someone needs to investigate if this problem is due to Display Suite not supporting the #1968348 change notice, or if there is a different issue with the taxonomy module that needs to be fixed.

dqd’s picture

Not sure yet, but it seems this patch from #83 causes an error on HS here in our testing enviroment lately: https://www.drupal.org/node/1160694#comment-11765371

ron_s’s picture

@diqidoq, please re-read comments #93, #94, and #96. Patch #83 is postponed without further information.

dqd’s picture

@ron_s: I know. I just wanted to confirm that this patch isn't confirmed yet to be working.

Ahmed_Samir’s picture

#83 Works on drupal 7.54

giupenni’s picture

#83 Works on drupal 7.54

The issue is not depend to Display Suite: https://www.drupal.org/node/1970540

ianwesty’s picture

#83 Works on Drupal 7.56

bobburns’s picture

There is a simple patch at https://www.drupal.org/project/taxonomy_formatter/issues/934726 which appears to work and is the first four lines of the patch here alone basically

I am using it right now

Since this is tested - and the issue is still present in 7.64, can someone commit this to core so this does not need to patched over and over agfain each Drupal core is updated ??

ron_s’s picture

@bobburns, have you read the earlier comments?

@diqidoq, please re-read comments #93, #94, and #96. Patch #83 is postponed without further information.

marcoka’s picture

Works in Drupal 7.65
In my case it makes "an unknown error" when using the search and finding an exact match that would redirect directly to the node.

bobburns’s picture

"ron_s ron_s commented 2 months ago at #104
@bobburns, have you read the earlier comments?

@diqidoq, please re-read comments #93, #94, and #96. Patch #83 is postponed without further information."

I read them and looked at the patch and it is in core - however that does not change the fact that the issue is still there and patch 96 fixes it - and this is the fifth time I have had to re=patch this each time a new version of Drupal core comes down - now on 7.66

jenlampton’s picture

From reading comments #93, #94, and #96, it sounds like the problem is most likely not in core. It's probably due to a contrib module that is not supporting the #1968348 change notice. Patching core will hide the symptom, but won't fix the problem, so it's not likely #96 will be committed.

@bobburns You can help us get to the bottom of this problem faster if we can find out which contrib module is causing the problem on your site. Would you be willing to provide some more information here in this issue?

  • What taxonomy or field modules are you using? (Are you using taxonomy formatter module? I saw your issue in that queue as well...)
  • Are you using Display Suite?
  • Can you reproduce this error in other situations than a node preview?
bobburns’s picture

Again this needs to be comitted to core - the issue re-appears with each core upgrade and needs to be re-patched again - it is still there is 7.91 having to be re-patched AGAIN

czigor’s picture

Reroll for 7.95.

czigor’s picture