Problem/Motivation

I get the error

Notice: Undefined index: display_field in file_field_widget_value() (line 582 of /module/file/file.field.inc).

Steps to reproduce (fresh 7.x install)

  1. Create a node of type article. Only add a title, leave the rest blank.
  2. Call the following code, either using devel or a custom module:
    function my_test_form($form, &$form_state, $entity_type, $entity) {
    
    field_attach_form($entity_type, $entity, $form, $form_state);
    
    return $form;
    }
    
    $node = node_load(1);
    
    $form_state = array('build_info' => array('args' => array('node', &$node)));
    
    drupal_build_form('my_test_form', $form_state);
    drupal_form_submit('my_test_form', $form_state);
    
  3. Notice error will be thrown as per issue title

Proposed resolution

DamienMcKenna suggested a patch that removes the ternary operator and checks for existence of the index using empty.

Remaining tasks

Contributor tasks needed
Task Novice task? Contributor instructions Complete?
Add steps to reproduce the issue Novice Instructions Y

User interface changes

N/A

API changes

N/A

Original report by @LTech

To Reproduce the issue:
When a user clicked the "add another" twice for a text field, i.e. for a total of three values on one text field, this error would display.

Tests:
I don't believe that we can create automated tests for this case.

Comments

audster’s picture

Have a look here...

http://drupal.org/node/714350

orkutmuratyilmaz’s picture

I had a look there and found nothing useful unfortunately.

That patch already had adapted to the drupal core, as Dries said.

But I'm still experiencing the same error.

audster’s picture

You might want to try flushing the cache (again) and then manually deleting the referenced index from your DB.

see http://drupal.org/node/1351506#comment-5428118

I was untangling and indexing error for comment_node_panel (and several other indexing errors)

I worked through this suggestion and it has entirely ended indexing errors on my site..

Let me know...

larsbo’s picture

I am not sure, but this issue might have something in common with http://drupal.org/node/1463860 which I am struggling with.

berdir’s picture

Version: 7.12 » 7.x-dev
Component: node system » file.module
Category: support » bug
Status: Active » Needs review

The attached patch fixes the notice for me.

There might be a better way to do it (enforcing that the setting exists), but this at least allows to get rid of the notice.

berdir’s picture

StatusFileSize
new603 bytes
LTech’s picture

I also got this error on the upgrade from 7.12 -> 7.14.
The patch in #6 took away the error.
Thanks

nor4a’s picture

I do not like patching the core files.
But seams that sometimes it is just necessary.

Thanks for the patch!

viseser’s picture

Thanks for the patch !!!

droplet’s picture

Version: 7.x-dev » 8.x-dev
Status: Needs review » Needs work
Issue tags: +Novice, +Needs backport to D7
zerolab’s picture

Status: Needs work » Needs review
StatusFileSize
new623 bytes

Attaching the patch against 8.x-dev.

jherencia’s picture

Status: Needs review » Reviewed & tested by the community

Both patches worked.

webchick’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

It would be nice to get some tests for this. Any idea how to trigger the error?

najim’s picture

Version: 8.x-dev » 7.16
Status: Needs work » Reviewed & tested by the community

had a problem with undefined index patch #6 worked for me, d7.16

chlarkkirby’s picture

Updated my installation to 7.19 and i got a problem on line 579, and i tried patch from #6, it worked! :-)

mathankumarc’s picture

I think issue has been hijacked from 8.x to 7.x :(

berdir’s picture

Version: 7.16 » 8.x-dev
Status: Reviewed & tested by the community » Needs work

Yes, this is still needs work for tests for 8.x

kevin morse’s picture

Status: Needs work » Needs review

#11: use-not-empty-1430934-11.patch queued for re-testing.

tregismoreira’s picture

The patch #6 works perfectly for me (D 7.22). Thanks! :D

damienmckenna’s picture

Regarding webchick's question of how to trigger it, the error was showing on a D7.22 site when a user clicked the "add another" twice for a text field, i.e. for a total of three values on one text field. How would that be tested?

damienmckenna’s picture

StatusFileSize
new595 bytes

Rerolled. Also changed the logic to get rid of the trinary operator and use intval(empty()) instead.

helmo’s picture

Issue summary: View changes

I've marked #2037549: Notice: Undefined index: display_field in file_field_widget_value() (line 587 of */modules/file/file.field.inc) as a duplicate of this issue.

I've had the same issue in D7.26, the patch from #6 fixed it for me.

danepowell’s picture

StatusFileSize
new602 bytes

Here's the D7 version of #21, for anyone who needs it.

milesw’s picture

Thanks, #23 eliminates the notices in 7.26.

robert.laszlo’s picture

Status: Needs review » Needs work

Applied this against HEAD, and it no longer applies.

johnish’s picture

Assigned: Unassigned » johnish
Issue summary: View changes

I'm looking in to rerolling the patch and updating the issue summary.

johnish’s picture

Issue tags: +Needs reroll
johnish’s picture

Issue summary: View changes
Issue tags: -Needs reroll
StatusFileSize
new721 bytes

Rerolled the patch. Uploading for testing.

johnish’s picture

Issue summary: View changes
Issue tags: -Needs tests
johnish’s picture

Status: Needs work » Needs review
johnish’s picture

Assigned: johnish » Unassigned
Issue summary: View changes
littledynamo’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Amsterdam2014

This bug doesn't appear to exist in D8, or at least I can't reproduce it.

Review carried out:

- Latest dev of D8 (clean install)
- Added a field of type "Text (plain)" to the article content type and set values to "unlimited".
- Add node of type article. There are 3 textfields showing initially.
- Added text to all 3 textfields. Added another textfield with some dummy text. Added a fifth textfield with dummy text and saved the node.
- No error messages.

I then applied the patch in #28 and carried out the above steps - also no errors.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

@littledynamo looking at the code and issue summary this appears to be a file field issue - can someone confirm that we still have the issue in 8.0.x? Thanks.

jsobiecki’s picture

Issue tags: -Amsterdam2014 +dcwroc2014
rpayanm’s picture

Agree with @littledynamo I can't reproduce this issue.
And:

To Reproduce the issue:
When a user clicked the "add another" twice for a text field, i.e. for a total of three values on one text field, this error would display.

Is impossible clicked the "add another" twice for a text field, because the button become disable until the next text field is add.

zaporylie’s picture

Status: Needs work » Closed (cannot reproduce)

I also suggest that we should change status of issue to "cannot reproduce". I've tried to reproduce it under D8 and D7 without any success.

jaydub’s picture

Status: Closed (cannot reproduce) » Needs work

FWIW it seems that was only ever an issue on D7 and that patches in this thread fixed it. However those patches were never committed to D7 and closing the ticket since the problem doesn't/never did appear in D8 would seem to leave D7 w/o a fix.

We encounter this notice in our logs now with the latest version of Drupal 7.32.

jaydub’s picture

Version: 8.0.x-dev » 7.x-dev
pushpinderchauhan’s picture

Status: Needs work » Needs review
StatusFileSize
new602 bytes

Patch for D7.

nachenko’s picture

Patch from #39 worked like a charm. Thanks!

damienmckenna’s picture

FYI the patch in #39 is identical to the one from #23.

monaw’s picture

When will this patch be released from dev? Unfortunately I cannot patch our core as I don't have admin privileges. We are running the latest core, 7.38. I'm getting this error on a regular file field.

damienmckenna’s picture

@monaw: This will be committed once it gets to RTBC and one of the core maintainers commits it. To help it on its way you could try doing a local test of the patch and provide a review, even if you're not able to run it on a production site.

monaw’s picture

@DamienMcKenna: thanks you for your reply! I look forward to the release.

Unfortunately I don't have time to setup a local test of the patch and provide a review, I'm very sorry. I have a crazy deadline coming up and my boss will give me a lot of grief for taking "his" time to do that as we have a rather complicated system and it will take quite awhile to setup a local replicate. Actually the system I'm working on is our "dev" and still I don't have the privilege to patch core. It is very unlikely that the sysadmin will patch core as the copy of Drupal is shared among many other sites at our organization.

das-peter’s picture

Status: Needs review » Needs work

I think #23 / #39 miss to invert the return of empty().
We want $input['display'] = intval(!empty($field['settings']['display_field']));.
We had that already in #6.

monaw’s picture

Ok, I figured out the solution without having to hack core (because I don't have permission to do so)…

The problem was caused by a hidden image field. When I changed the image field to not hidden in the content type settings, then the error went away. But for our application, we don't want to show that field so in my custom module, I set the field's #access to FALSE:

$form['field_is_poster']['#access'] = FALSE;

and that worked…field is not shown and there is no error (:

By the way, I'm using Drupal 7.38.

Hope this helps someone else!

cilefen’s picture

@monaw It would be great if you could please provide a review. That is how the issues move toward completion. Is the critique in #45 from @das-peter valid? If so, provide a new patch with the change instead.

SGhosh’s picture

Status: Needs work » Closed (cannot reproduce)

Could not replicate.

akosipax’s picture

Status: Closed (cannot reproduce) » Needs review
StatusFileSize
new603 bytes

This error not only appears for textfield with multiple values. In my case, I have a Price Table field (from commerce_price_table) with unlimited values and when I click "Add another" more than twice, this error starts appearing.

This has been tested in 7.x-dev and the errors disappear. This is simply patch #39 with das-peter's correction in comment #45.

njbarrett’s picture

Assigned: Unassigned » njbarrett
Issue tags: +Needs tests

I am getting this error when programatically submitting and validating node edit form. e.g.

A form callback with:
field_attach_form($entity_type, $entity, $form, $form_state);

$form = drupal_build_form('form_callback', $form_state);
drupal_form_submit('form_callback', $form_state);
drupal_validate_form($form['#id'], $form, $form_state);

The patches in #49 fixes the issue.

If I get some time I will write a test.

malcomio’s picture

The patch in #49 would introduce a bug similar to #2593377: "Include file in display" has no effect with media browser widget fields where the display setting would have no effect.

Here's an alternative patch - I haven't been able to reproduce the original issue, though.

njbarrett’s picture

Component: file.module » image.module
Assigned: njbarrett » Unassigned
Status: Needs review » Reviewed & tested by the community
Issue tags: -dcwroc2014, -Needs tests

I believe I have tracked down the cause of the issue.
It seems to be related to image fields, which piggyback on many of the file field functions.
The thing is, in `image_field_info` there is no `'display_field' => 0,` in the settings array, where in `file_field_info` there is.
Now because the image field uses `file_field_widget_settings_form`, which expects `display_field` to be set, we get this notice error.

The patch in #49 is the correct fix for this issue.

njbarrett’s picture

Component: image.module » file.module
malcomio’s picture

Status: Reviewed & tested by the community » Needs review

I think that this still needs more review - as I mentioned in #51, applying the patch from #49 would introduce the following bug:

Steps to reproduce

1. Add a File field to a content type, using the "File" widget, and enable the Display field
2. Create a node with a file in that field, and uncheck "Include file in display"

Expected result

The file is not displayed (to admins or anonymous users)

Actual result

The file is displayed.

njbarrett’s picture

Hey @malcomio

Sorry, I have tested #49 and you are right, it ignores the "Include file in display" checkbox.
I then tested your patch in #51 and confirm it works.

njbarrett’s picture

Issue summary: View changes

I have updated the issue summmary with steps to reproduce.

eangulo’s picture

Patch from #39 worked for me! Thanks

cilefen’s picture

Can someone provide a review? That is a required step to get this bug fixed in Drupal core.

g33kg1rl’s picture

I had this issue happen on a clean install. #51 fixed the issue for me. :)

alesr’s picture

Priority: Normal » Major
Status: Needs review » Reviewed & tested by the community

I did a extensive research on this.
Started with image module where I set the default 'display_field' value. Also tried to add 'display_field' => 0 into $form 'my_image_file_field' where it is expected so it's there before the render kicks in and still the error persisted.

If we look at the original lines in file.field.inc file:

if (empty($input['display'])) {
  $input['display'] = $field['settings']['display_field'] ? 0 : 1;
}

By my means this is bad coding. PHP is quite strict about undefined array indexes and if there's a slight chance 'display_field' is not set, this line will produce a notice.

The outcome of my review is, even if there's an alternative solution to this issue, this line in the if statement should be replaced with:

$input['display'] = !empty($field['settings']['display_field']) ? 0 : 1;

In this case, file module doesn't rely that 'display_field' is set. Using empty() function in deep arrays with dynamic indexes makes our lives better.
I suggest we accept the patch in #51 as a confirmed solution.

alesr’s picture

Bump.
Can this issue get some D7 core contributors attention?

elijah lynn’s picture

#51 Appears to work for the case I was having.

fabianx’s picture

Issue tags: +Pending Drupal 7 commit

Straightforward, marking for commit and very probable inclusion in 7.50.

David_Rothstein’s picture

Priority: Major » Normal
Status: Reviewed & tested by the community » Needs review
Issue tags: -Pending Drupal 7 commit +Needs tests

While the patch is certainly harmless, I think we should be careful with these because hiding a PHP notice sometimes hides an actual bug with deeper consequences...

Bottom line, reading through the issue the root cause of the problem is not clear.

Naively, it should not be a problem at all:

  • file_field_widget_process() sets $element['display'] as a hidden form element with value = 1 whenever the display field is not present in the field settings (e.g. for image fields).
  • Therefore, when that field is submitted, file_field_widget_value() should always have $input['display'] equal to 1 also, so the error should never occur.

The steps to reproduce in the issue summary do work, but require doing this:

drupal_build_form('my_test_form', $form_state);
drupal_form_submit('my_test_form', $form_state);

If you call drupal_build_form() first, that is going to put all sorts of things in $form_state['values'] that aren't normally there. I don't think it's a documented or supported way to call drupal_form_submit()... normally you would just build $form_state['values'] manually with the values you're actually trying to submit. If you do that I don't think this bug occurs.

The duplicate issue at #2037549: Notice: Undefined index: display_field in file_field_widget_value() (line 587 of */modules/file/file.field.inc) mentions something with the Field Collection module as a way to reproduce, but I wasn't able to figure it out.

So does anyone understand a way to reproduce the problem in the real world? If there is one and it's not due to other code doing something else wrong, I'd be in favor of this issue.

Also, this issue originally got the "Needs tests" tag in #13 but it's not clear why it was later removed. There are no automated tests in the patch, and automated tests would help (among other things) figure out if it's an issue in Drupal 8 also. People said above they couldn't reproduce it in Drupal 8, but I'm not sure any of those same people could reproduce it in Drupal 7 either! The code is very similar in Drupal 8 so I'm not really convinced this is actually a Drupal 7-only bug, if it's a bug at all.

stefan.r’s picture

Issue tags: -Novice
Anonymous’s picture

+1 for the patch in #51.

I'm running into this issue with a site using paragraphs, and I can't open the modal to edit because of it.

My guess is there is an older file field that doesn't have the proper default settings, or some other bug deleted it.

Either way, it's still an error that can happen.

And 51 isn't hiding any php notices, it is correcting poor coding that was assuming $field['settings']['display_field'] will always be set.

I think this is good to go back to RTBC

aporie’s picture

#51 works for me with File field path and File resup

Why not publishing this patch with the next update ?

alesr’s picture

@Aporie because it requires tests. Mentioned in #64.

hkirsman’s picture

Worked for me. Tx!

pol’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs backport to D7, -Needs tests +Drupal 7.60 target

Patch #51 does the job for me as well.

joseph.olstad’s picture

Issue tags: -Drupal 7.60 target +Drupal 7.61 target

Bumping to 7.61, this didn't make it into 7.60

joseph.olstad’s picture

opgobee’s picture

Patch #51 worked for me. I encountered the error on uploading an image on the edit form of a node, on Drupal 7.61.
Reading around, the error seems to occur for images as they have no display_field according to https://www.drupal.org/project/drupal/issues/714350 #2 and #9.
The patch that checks the presence of the display_field prior to calling it, appears sensible.

joseph.olstad’s picture

fabianx’s picture

Category: Bug report » Task

I have decided to commit this without Tests as hardening of the code instead of a bug report:

- Several people had been able to reproduce it using contrib modules (there is an impact)

- Core itself ran into this problem in Drupal 7.9 and fixed it for the image formatter specifically -> This means every other (contrib) implementation would need to fix it again and again, which is a "red flag" in itself

- If something is empty, it's okay to assume that it's meant to be 0

While there could be bugs hidden by hiding the notice, the probability that they would like that to show is very unlikely (and would be noticed in manual testing) and behavior for already broken code does not change

Therefore => Committing

Only need to check if Drupal 8 needs something similar to harden first

fabianx’s picture

Okay, in Drupal 8 the code is the same:

      // Checkboxes lose their value when empty.
      // If the display field is present make sure its unchecked value is saved.
      if (empty($input['display'])) {
        $input['display'] = $element['#display_field'] ? 0 : 1;
      }

However in this case $element['#display_field'] is always set as code before that adds it extra:

      $field_settings = $this->getFieldSettings() + ['display_field' => NULL];
      $elements['#display_field'] = (bool) $field_settings['display_field'];

But because we never use $field['settings'] again in D7 at that point, the fix is good to go as is, e.g. there is no win for us to add the 'display_field' => NULL to the settings over the fix here.

=> Ready to be Committed

fabianx’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 7.x! Thanks all!

  • Fabianx committed 9b1a957 on 7.x authored by Pol
    Issue #1430934 by johnish@gmail.com, DamienMcKenna, Berdir, malcomio,...
pol’s picture

Status: Fixed » Closed (fixed)

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

pavel.zheldak’s picture

StatusFileSize
new610 bytes

Stops working with core version 7.69. Updating patch to be applicable for latest core.

alesr’s picture

Isn't that going to return the same value in integer for both cases?

  $input['display'] = !empty($field['settings']['display_field']) ? 0 : 1;
  $input['display'] = intval(empty($field['settings']['display_field']));