Hi, I am new in Drupal. I got php notice in log message like below
Notice: Undefined index: uri in theme_image_formatter() (line 602 of /home/lensaker/public_html/ojual.com/modules/image/image.field.inc).
and
Notice: Undefined index: uri in image_field_formatter_view() (line 572 of /home/lensaker/public_html/ojual.com/modules/image/image.field.inc).
every time i upload an image.

Currently I fix it by a condition:
$item['uri'] = isset($item['uri']) ? $item['uri'] : "";
the log message now silence, but is this correct or is there other good solution? ie update to some version?
I use Drupal 7.14

Thanks

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Spleshka’s picture

Version: 7.14 » 7.16

I got this message too (Notice: Undefined index: uri in theme_image_formatter() (line 602 of path-to-drupal/modules/image/image.field.inc).

See code:

function theme_image_formatter($variables) {
  $item = $variables['item'];
  $image = array(
    'path' => $item['uri'],
    'alt' => $item['alt'],
  );

No check for empty $item['uri'];

Spleshka’s picture

Sorry, I found a bug. I called node_view() on hook_node_update(). That was my mistake.
Hope that it will help someone.

Anonymous’s picture

Me too (Notice: Undefined index: uri in theme_image_formatter() (line 602 of path-to-drupal/modules/image/image.field.inc).

Though unlike Spleshka, I am not using any theming hooks.

I think that it's causing my image token to output

http://dev.portside.org/sites/default/files/styles/large/public

when it should go to

http://dev.portside.org/sites/default/files/styles/large/public/field/image/image-file

catch’s picture

Title: Notice: Undefined index: uri in image_field_formatter_view(), Undefined index: uri in theme_image_formatter() » Notice: Undefined index: uri in image_field_formatter_view()
Version: 7.16 » 8.x-dev
Priority: Minor » Normal

Still a valid bug in 7.x. Code hasn't changed in 8.x either. Just reproduced when uploading an image on a very simple install.

swentel’s picture

Assigned: Unassigned » swentel

Looking

swentel’s picture

Assigned: swentel » Unassigned

hmm image_field_formatter_view doesn't even exist anymore in D8 .. catch, could your error be related to #1885644: ImageStyle::uri() is broken and has no coverage

claudiu.cristea’s picture

Version: 8.x-dev » 7.x-dev

I cannot reproduce this error in D8. Feel free to move it back to D8 if I was wrong.

yingtho’s picture

Issue summary: View changes
Status: Active » Needs review
FileSize
597 bytes

Please see enclosed fix to this issue. It seems that the uri is empty somehow.

Niremizov’s picture

This bug can be also reproduced when using views. Try to output Image field, and group it by FID... But patch, solves this.

ShaneOnABike’s picture

For me the patch didn't resolve my issue mainly because it was occuring in theme_image_formatter...

function theme_image_formatter($variables) {
  $item = $variables['item'];
  $image = array(
    'path' => $item['uri'],
  );

But verifying that a proper FID exists within the $item array seems to have reduced the problem... by changing the code to

function theme_image_formatter($variables) {
  $item = $variables['item'];
  if (empty($item['fid'])) return '';
  $image = array(
    'path' => $item['uri'],
  );

I combined both my patch fix and the other one to keep some consistancy

Status: Needs review » Needs work

The last submitted patch, 11: image-preview-missing-1678686-11.patch, failed testing.

vaartio’s picture

I have the same case with theme_image_formatter(). However, I recommend that we should check the existence of $item['uri'] rather than fid, since uri is the property that theme_image_formatter uses for path.

dcam’s picture

Status: Needs work » Needs review

Activating Testbot.

Christopher Riley’s picture

Any idea when this patch will become "Official"?

vaartio’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 13: image-preview-missing-1678686-12.patch, failed testing.

Status: Needs work » Needs review
David_Rothstein’s picture

Seems like we need to look into the root cause of this bug more. What is the fundamental reason that $item['uri'] is missing in the first place?

jcfiala’s picture

If it helps, I'm getting it when I add an image field to a comment, and then preview the comment. Throwing in a dpm, here's what's in my item:

Array
(
    [alt] => Pig Eating Ice Cream
    [title] => Ice Cream Pig
    [fid] => 2691
    [display] => 1
    [width] => 640
    [height] => 572
    [description] => 
    [upload_button] => Upload
    [remove_button] => Remove
    [upload] => 
)

The fid points to a valid row in file_managed, but the file information just hasn't been loaded.

formatC'vt’s picture

Confirm bug with image field and comment preview. it's are easy to reproduce:
1) Install drupal (without any contrib module)
2) add image field to comment
3) upload an image to comment and click 'preview'
4) Home » Administration » Reports
Notice: Undefined index: uri in theme_image_formatter() (line 602 of /home/s800caa97026f0d7/www/modules/image/image.field.inc).

Patch fix this.

Dave Reid’s picture

Status: Needs review » Closed (duplicate)
Related issues: +#920840: Broken images displayed and PHP notices when file/image field values are missing

This would be resolved by the patch in #920840: Broken images displayed and PHP notices when file/image field values are missing which would remove the missing file item from the field value array completely.

ShaunDychko’s picture

Status: Closed (duplicate) » Needs review

This is not a duplicate issue I don't think. #920840: Broken images displayed and PHP notices when file/image field values are missing deals with the case where the file doesn't load. In my use case there is a valid image which loads just fine, and the $item['fid'] was populated correctly, although $item['uri'] wasn't for some reason. The patch in #13 removes the error message nicely.

Perhaps what makes my use case a bit different is that, given an image field in user accounts, there's a user_view and drupal_render called for a particular display mode each time the user account is updated in order to update a custom cached version of their profile for a map marker popup. Maybe this unconventional way of calling those functions from within hook_user_update() causes the $item['fid'] to get populated, but not $item['uri']? The profile image field uses the private file system, if that makes a difference.

In any case #13 solves the pesky error message. Thank you!

Chris Matthews’s picture

Is this a duplicate of #920840 or does this still need review for the patch in #13 to be committed?

Nathan Tsai’s picture

Encountered this today. Fixed using the patch in #8.

Jun 18, 2018:
The patch was overridden when the core was updated and used an alternate solution.

To fix error when aggregating images fields, (or possibly file fields), in views: change "Group column" from Fid to Entity ID.

poker10’s picture

I digged into this a little bit deeper and regarding #20 and #21 use-cases, there seems to be a bug in function comment_preview($comment).

This function takes a $comment as a parameter, but this object contains only values from the $form_state data.

If we compare that with function node_preview($node) which is the same function for the node preview and where the preview with image is working, we will see, that in this function there is this code used:

_field_invoke_multiple('load', 'node', array($cloned_node->nid => $cloned_node));

This code will ensure that all fields gets all values correctly populated. This call is missing in comment_preview() function. If we add this to comment_preview() function (for example after the node_load call), the problem with the broken image preview should be gone:

_field_invoke_multiple('load', 'comment', array($comment->cid => $comment));

But it seems like there are more different scenarios mentioned - from views, user images, up to these comments. Personally I do not like the approach of introducing a file_load() call to the image_field_formatter_view(), when URI is missing. This will mask all potential problems, not solve them. I think the better approach should be to not render the image at all, if the URI is missing (like it is done in theme_image_formatter() in the patch).

poker10’s picture

Adding that _field_invoke_multiple() will also fix a usecase mentioned here: #1967176-4: Multiple stdClass::$filemime notices related to file fields and email display mode.