Problem statement
PHP Fatal error occurs in Image_Attach submodule when saving a node that has no images attached. In various cases the 'iids' array key is empty (eg node import, biblio module, or custom code that sets the form field '#access' to FALSE.
The error message is:
PHP Fatal error: Cannot unset string offsets in image_attach.module on line 457
Proposed solution
Wrap the unset() in an if (isset()).
Original issue summary:
Please see my description here #589422: PHP fatal error
I think that a change in Image.Module has impacted another module
Comments
Comment #1
sphopkins commentedComment #2
joachim commentedIt helps if you repeat the problem here:
Can you confirm your line 343 is this:
If so, I'm sure there are better places for this than hook_nodeapi -- it's dealing with a quirk of the form, whereas hook_nodeapi is meant to be general. We should move it to a submit handler perhaps?
On the other hand, apparently
"PHP 5 seems to have an error where it tries to treat strings as arrays instead of highlighting the error"
did you run update.php after upgrading to the latest image module?
Comment #3
sphopkins commentedI will confirm tomorrow when I have full access to my server. However as I have not touched anything I am sure it is whatever you guys released ;-)
I did run update.php but I will verify that it has been completely done...
Comment #4
sphopkins commentedConfirmed:
As well I have confirmed that update.php was run, and I reran it to verify.
Hope this helps.
Comment #5
joachim commentedDoes commenting out that line fix the problem?
Comment #6
sphopkins commentedI will test that. I am not normally comfortable changing some of the code without someone who knows what is happening giving me the go-ahead !
Comment #7
sphopkins commentedCommenting the line out worked to allow the Node_import to work properly.
Thanks for reviewing.
Comment #8
joachim commentedThanks for confirming.
Better title.
Comment #9
joachim commentedPlease could you test this patch?
You'll need either a clean copy of beta3 or the dev release to apply it to.
This patch also needs to be tested for regular use of image attach: creating and editing nodes normally, without node_import.
Comment #10
sphopkins commentedI will look at this patch. I only have a production server so I am leery of messing it up ;-)
Comment #11
joachim commentedThe drupal docs have a section on running a test server locally. This is a good idea to have in general, and is a lot easier to set up than it used to be: basically, xampp on Windows and mamp on a mac.
Comment #12
sphopkins commentedYeah I have MAMP Pro and I did use it for testing to get the website up and running but it has gone stale since I implemented the site. I will try and update things to the same level before testing.
Comment #13
joachim commentedI recommend this for cloning your live site to your dev server: http://drupal.org/project/backup_migrate -- does it in about three clicks; an absolute breeze.
Comment #14
magpie5212 commentedSubscribing
Comment #15
bails commentedHi I've tested the above patch on a production server and it seems not to fix the issue.
before
Tue Oct 20 14:09:37 2009] [error] [client 192.168.1.1] PHP Fatal error: Cannot unset string offsets in foobar/drupal-6.14/sites/all/modules/image/contrib/image_attach/image_attach.module on line 343
after
[Tue Oct 20 14:55:09 2009] [error] [client 192.168.1.1] PHP Fatal error: Cannot unset string offsets in foobar/drupal-6.14/sites/all/modules/image/contrib/image_attach/image_attach.module on line 332
So I'm left with a commented out version which does work.
Comment #16
joachim commentedGah, what is node_import doing???
If it's triggering the form submit handler, then surely it's using the form in some way and so the values should be there.
What version of node_import are you using?
And could you post a sample node that causes this problem so I can try the import myself?
Comment #17
sunI can tell - with confidence - that node_import really is a mess. Hence, with a chance of 99% this bug is caused by node_import and the code in there needs to be fixed instead.
Comment #18
krysgeek commentedI was having the same problem with node_import and image_attach. I just added the conditional to test if that exists. Works great now. No more error.
I changed the following in /sites/all/modules/image/contrib/image_attach.module
(Image Module version 6.x-1.0-beta5)
Starting Line 396
To
Comment #19
joachim commentedI'm not going to make that changed because node_import should be calling node_save, not pretending to submit the form. That cleaning up of the 0 key has already been moved from the save hook to the form submit, which is where it belongs.
Comment #20
krysgeek commentedI didn't like having to make the change either, but I do like that node_import is finally working. I kept getting the PHP fatal error of this file at line 402.
Do you see a problem going with the change?
Comment #21
joachim commentedNode import should be Doing the Right Thing and either not pretending to submit a form, or pretending to submit it properly!
Comment #22
sphopkins commentedI agree that Node Import should do the right thing but I think that the maintainer has disappeared and there has been no activity in a long time by him... for a very useful module.
Comment #23
joachim commentedYou can request to take over a project that's been abandoned -- there's a page all about it in the handbooks :)
Comment #24
sphopkins commentedIf I had the skillz I would... I am a user for sure, not a coder. I may look to get someone that is a coder to look at the module!
Comment #25
jriedel commentedI just ran into this same issue, but I don't have node_import on this site.
It happened when a user tried to edit a forum post. Edit as admin didn't triger the problem.
I applied the fix in 18 and now things are working.
Any ideas on how to track down what other module is not playing well with others?
Comment #26
joachim commented> Any ideas on how to track down what other module is not playing well with others?
Try it on a fresh site and then add modules. Or disable modules on your current site.
I can't tell any more -- did the patch above go in or not?
Comment #27
joachim commentedNo sign of 589428 in the codebase.
@jriedel : do your users have the permission to attach images themselves?
Comment #28
jriedel commented@joachim
I put that change in #18 and that fixed the issue. Yes, my users can attach images.
Comment #29
natali commentedHello,
I have the same error "Fatal error: Cannot unset string offsets in /udata/WWW/test_zoi.utia.cas.cz/sites/all/modules/image/contrib/image_attach/image_attach.module on line 343".
Module Image Import is not enabled.
I got this error while tried to create a node programmatically. Node can have image attached, but i'm attaching none.
I tried to use patch, but in my file image_attach.module is not function image_attach_node_form_submit present.
My file version is image_attach.module,v 1.66 2009/09/08.
My user have permition to attach images.
Thanks for help.
Comment #30
joachim commentedCould you give more detail about how you're trying to create a node programmatically?
Comment #31
natali commentedHere is code, for testing i run it from body of node:
Comment #32
joachim commentedYeah, that's wrong. Don't pretend to be a form -- or if you must, put in the values that the code expects; in other words, the ['iids'] key.
Better still, build your node object and do node_save().
Comment #33
natali commentedThank you for your quick response and advise :-)
i was trying to use node_save
but i found somewhere here http://drupal.org/node/178506 , it is working only for drupal 5 (it work fine for 5, but not for 6)
and i use drupal 6 example from same site.
Do you have another example for drupal 6 I can use?
Comment #34
natali commentedMy solution is to add
Thanks again for help!
Comment #35
damienmckenna+1 for the fix in #18. Here it is in patch format.
Comment #37
damienmckenna#35: image-n589428-35.patch queued for re-testing.
Comment #38
damienmckennaCould someone please just commit the patch from #35 so this bug is fixed? The error is still happening in v6.x-1.1.
Comment #39
joachim commentedI need to look into this more, but my gut feeling is that it's Node Import's problem.
Comment #40
damienmckenna@joachim: Whatever the root problem, it should not be trying to unset a variable if it doesn't exist.
Comment #41
sun@DamienMcKenna: The variable does exist. And unsetting a non-existing array key also does not trigger a fatal error.
The fatal error is caused by Node Import passing a completely invalid variable having a different data type (not an array).
Is there a patch for Node Import already? I have commit access.
Comment #42
damienmckennaIn my case I'm not using node_import, I'm making the following adjustment via hook_form_alter() based on certain conditions (only show the fields if the date on a CCK Date field has already elapsed):
With this line in place, when image_attach_node_form_submit() executes the $form['values']['iids'] value shows up as a string - I don't know why it becomes a string, that's just what it ends up as.
Comment #43
longwaveAlso seeing this "cannot unset string offsets" error - this is on a live site and currently I can't debug it easily but Node Import is not installed.
Comment #44
joachim commentedUpdating the title.
Comment #45
jwilson3I've marked #1871218: Cannot unset string offsets in image_attach.module on line 457 as a duplicate of this.
I've also rerolled patch in #35 to use the new git patch format (#35 still applies, but there was also a 60 line offset that is now fixed).
Comment #46
joachim commentedCommitted. Thanks everyone!
I'm going to check a few other patches needing review and then make a release soon.