Hi. This is my first try to submit a bug to Drupal, so please forgive me if i'm not doing in the right way.
I've just added a new field to my content type. this is a commerce_price field (so, maybe this can be a problem of that module too) and also i choose "Read-only" as widget.
Trying to add a node of this type i get a notice in my screen reading
Notice: String offset cast occurred en _field_invoke_multiple() (line 322 of [...]/modules/field/field.attach.inc).
Line reads like
$language = !empty($options['language'][$id]) ? $options['language'][$id] : $options['language'];
I used dpm for debugging and found that indeed ($options['language'] ) is not an array but an string ('und') in this case.
I modified that line adding to check if that $options['language'] is an array and that solved my problem. I'll submit the patch asap for your review.
As said, sorry if i'm not doing right.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | string-offset-cast-1824820-2.patch | 1002 bytes | crevillo |
Comments
Comment #1
crevillo commentedSorry. Let me add that the notice only appearing with php 5.4.
Comment #2
crevillo commentedAttached patch for your review
Comment #3
crevillo commentedComment #4
Anonymous (not verified) commentedDoes this issue exist for D8. PHP 5.4 isn't recommended at this time.
Comment #5
crevillo commentedHaven't tested D8 yet sorry. Feel free to close the issue if php5.4 is not the right choice for D7 then
Comment #6
krabbe commentedPatch #2 worked for me in 7.16
Comment #7
parrapa commentedPatch worked successfully in 7.17. Thank you!
Comment #8
Anonymous (not verified) commented#2: string-offset-cast-1824820-2.patch queued for re-testing.
Comment #10
pere orgaThe patch should be updated for Drupal 8
(It's the workflow to fix bugs that happen in both 7 and 8 versions)
Comment #11
crevillo commentedexcuse me, but as said, i'm a bit newbie with this... do you mean i should provide a patch for d8 too?
Thanks.
Comment #12
rbayliss commentedWelcome crevillo. Yes, it's standard policy to fix things in the development version (Drupal 8) first, then backport to the current version (Drupal 7). If you can provide a patch for Drupal 8 that would be great. Thanks!
Comment #13
pere orgaYes, and this is why the current patch cannot be applied: it's a D7 patch and the issue is now marked for 8.x-dev.
Comment #14
crevillo commentedok. i see. sorry about the missunderstanding. i'll try to provide a d8 patch.
Comment #15
crevillo commentedhere's d8 patch for review
Comment #16
crevillo commentedComment #17
pere orgaI think the patch is wrong.
If $options is not an array, then $langcode is set to $options['langcode'], which does not exist.
Comment #18
crevillo commentedBut, as far i can see, $options should be always an array.
at the begining of the function we have
$options is not re-assigned anymore in the function. so, it seems to me that $options will be always an array...
Please correct me if i'm wrong.
Anyway, it seems patch failed, so i'm digging on it.
Comment #19
pere orgaOk, I assumed that $options may not be an array because what your patch does is check if it's an array.
Maybe you wanted to put:
is_array($options['langcode'])instead of:
is_array($options)?
Comment #20
crevillo commentedok to that. you're right.
attaching new patch
Comment #21
crevillo commentedI run test locally and fails throwing some warnings at line Drupal\translation_entity\EntityTranslationController->entityFormAlter().
Warning thrown reads
it's this piece of code
In my opinion, warning is thrown because in some cases $form_langcode is an string and not array.
Any recomendations on how to deal with this?
Thanks
Comment #22
pere orgaSeems like test's fault ?
Comment #23
crevillo commentedyep. seems so. attached new patch for review
thanks for your help.
Comment #24
pere orgaLooks good to me.
Comment #25
crevillo commentedCool. Let me know if you need something more on my side.
Thanks for your help.
Comment #26
yched commentedHard to say for sure, but this very much looks like some contrib module doing something wrong, which then triggers errors down the line in a core function. The core policy is to not add opportunistic validity checks to swallow errors coming from contrib.
In order to get anything done here, precise (and if possible simple) steps to reproduce the bug should be provided.
Does this happen on a Drupal Commerce site ? Is it based on Commerce Kickstart ?
If so, I'd suggest moving the issue over there so that their team can try to refine this.
Comment #27
Anonymous (not verified) commentedWhy is the EntityTranslationController part of the patch needed?
I understand the field.attach.inc piece: @yched, the fact that the code is already giving "$options['langcode']" as a value lends to the fact that this is a core issue and needs resolved by "is_array($options['langcode'])".
Comment #28
swentel commentedWe still don't know where this is coming from, so keeping the status.
Comment #29
yched commentedSo, looking at this a little closer :
_field_invoke_multiple() accepts $options['langcode'] ($options['language'] in D7) either as a langcode or as a 2-level array of langcodes keyed by entity id and field name.
Contrary to what I wrote in #26, It might well be true that the current check is a bit sloppy on that side - using isset($options['langcode'][$id]) to determine whether $options['langcode'] is one form or the other.
Still, we'd really need some detailed steps to reproduce to determine what's happening exactly, and add a proper test in our test suite so that this doesn't happen again.
Comment #30
crevillo commentedHi all
@yched: i understand the policy of nor modifying drupal core if not entirely necessary.
As said in the intro, issue appeared just after adding a commerce_price field to a existing content type. So indeed, yes. Drupal Commerce is being used in my site.
Anyway, and not being a Drupal expert at all, i think @earnie is right too.
Let me remember that no error appeared. not even a warning. it was just a notice and it was only when i switched from php 5.3 to php 5.4.
Other than that, the question it's if there is some documentation or some place where drupal tell developers that $options['language'] MUST be always an array.
And, as far i can see in the inline doc for the method this is not the case.
http://drupalcode.org/project/drupal.git/blob/HEAD:/core/modules/field/f...
So, or is the key here. Drupal Code, at this point, is "allowing" other to provide that $options['language'] as a lang code (string) or as an array.
As $options['language'] CAN BE sometimes a string, it's normal we can get a notice when why try $options['language'][$id] we ge the notice.
So, i still think patch should be in the drupal core. If not, you should warn all developers and update the documentation.
Thank you!.
Comment #31
yched commentedCrosspost :-) See my #29.
Comment #32
crevillo commentedyep, you were quicking.
trying to reproducing so you can test it in your side.
Thanks
Comment #33
Anonymous (not verified) commentedOk, however, it doesn't matter where it is coming from. As mentioned in #1 this is an issue for PHP 5.4 which seems to be a bug in that version of PHP. The empty() should be enough for this and we tend not to work around buggy PHP either.
However 5.4.0 has this change for empty:
5.4.0 Checking non-numeric offsets of strings returns TRUE.
Does this change cause a need for change in this scenario?
Comment #34
Anonymous (not verified) commentedI created #1850770: PHP 5.4.0 change for empty() may require changes in Drupal. for consideration of all of Drupal code based on my discovery of the change for empty().
Comment #35
crevillo commentedhere is a "similar" case to reproduce the notice. please test this in a php 5.4 install
The result i get is
Tested in a 5.3 install and no notices at all.
Admiting is quite strange and probably coming from a contrib module, now i'm searching where that $id it's null or bool.
Comment #36
bluesky_still commented#20: string-offset-cast-1824820-20.patch queued for re-testing.
Comment #37
jmaties commented#2: string-offset-cast-1824820-2.patch queued for re-testing.
Comment #38
7wonders commentedcross post to http://drupal.org/node/1900456 where I just posted this bug with some perhaps helpful debug info.
Comment #39
Tinnka commentedPatch worked successfully in 7.17 (Commerce Kickstart). Thank you!
Comment #40
pere orgaOk @Tinnka but please don't change this as we still need that tag, and the version the patches are applied to is 'dev'
Comment #41
zkday commentedThis patch at http://drupal.org/node/1824820#comment-6746564 also working with 7.x-dev.
Thanks. :)
Comment #42
crevillo commentedyou're welcome. so, can i say this is my first contrib to drupal core? :)
Comment #43
13rac1 commentedI'm using EntityWrapper to programmatically create a node. I confirm this notice occurs in PHP 5.4, but not in PHP 5.3.
Comment #44
yoroy commentedI run into this notice when using the Automatic Nodetitles module.
When I leave the Pattern for the title empty, no notice.
When I insert a token for a field of the content type to be used as the title, I get the notice.
On D7, PHP 5.4.4
*Edit* and: no issues when using PHP 5.3.14
Comment #45
aspilicious commentedAnything I can do to move this forward? Client project with auto nodetitle is affected.
Comment #46
yched commentedRight, sorry for the delay :-(
Thanks a lot @earline for the explanations in #33, makes total sense.
Patch #2 is the correct fix, then.
Doesn't need to be fixed in D8, _field_invoke_multiple() is gone. and D8's EntityNG + Entity translation API makes the corresponding code completely different and not affected by this.
Comment #47
David_Rothstein commentedIn Drupal 8 I see the following code in field_invoke_method_multiple():
Are we sure Drupal 8 isn't affected?
And could this use a test?
Comment #48
matsbla commentedDoesn't patch #2 hack the core? Should I do it or wait for this to be ported to a new version of D7 core?
Any progress on this issue? If this is fixed, why not simply commit it?
Comment #49
siliconmind commentedComment #50
siliconmind commentedIt seems that the problem is caused by line 303 that calls entity_extract_ids for a node that has not been saved yet. Such node has it's nid set to NULL. Hence the $id in line 303 is also set to NULL.
Now take a look at this:
This is new for PHP 5.4
The problem is not the fact that $options['language'] is not always an array, but the way the check is performed. Current code clearly allows $options['language'] to be a string, so the patch from #2 is not a hack and should be applied.
Comment #51
dlu commentedDo we need to add tags for STR and tests to this? Are they blocking this issue?
Comment #52
pdcarto commentedI'm totally not an expert on automated patch testing, but it looks to me like field.test has unit tests that should catch any problems with this patch. And the patch passed testing. Am I missing something?
Comment #53
dema501 commentedAccording #50
I've changed
And alert is gone.
Comment #54
NaX commentedI have been able to trace this problem from a contrib module to core and it looks like SiliconMind in #50 is correct about
entity_extract_ids().I am currently setting up a Commerce site running PHP5.4 and that the inline_entity_form module is previewing an entity before it is saved with an ID so the ID is returned as NULL when creating new product variants.
I was able to work around the problem by overriding the theme function
theme_inline_entity_form_entity_table($variables)and added the following.This make the language options index an array rather than a string and by making the ID property on the entity object a ZERO it is easily to reference in the options array.
I hope that makes sense and is helpful.
Comment #55
muriqui commentedI think #50 is right on. Like yoroy's #44, I ran into this with the Automatic Node Title module. If I set the node title to use a field token and then create a new node, the result is that this method gets called with an unsaved entity. Thus, the $id value is NULL, and the
!empty($options['langcode'][$id])check produces the "string offset cast" notice. Adding a!empty($id)condition to that statement fixes the problem. See attached patch.Also: updating the issue title to reflect the D8 function name and downgrading priority to normal (since this is only a PHP notice message, not a warning or error).
Comment #56
muriqui commentedAnd for those of us encountering this issue on D7, here's the backported patch.
Comment #57
drupov commentedPatch from #56 worked for my d7 installation. Thanks!
Comment #58
drupov commentedThe problem persists with drupal's update to 7.25. Patch from #56 solves that too.
Comment #59
schifazl commentedI confirm that patch from #56 works in Drupal 7.25
Comment #60
NaX commentedThanks to muriqui we now have a Drupal 7 and 8 patch that is confirmed to work.
Comment #61
swentel commentedField language system has been reworked completely in D8 - this patch probably became redundant for D8.
Comment #62
NaX commentedComment #63
shahidbscs commented#56 work for me on D7, thank you
Comment #64
mariacha1 commentedLooks good in D7.26. I say it's ready to be committed.
Comment #65
phizes commentedRe-upload of #56 to go through testing for D7.
Comment #66
miromarchi commented#65 worked for me in my D7 panopoly-based distro.
I had the notice using rules with a content type which title is managed by auto_nodetitle.
Thanks
Comment #67
cyberschorsch#65 takes care of this issue and works
Comment #68
heddnre: #1824820-47: String offset cast notice in field_invoke_method_multiple() & #1824820-61: String offset cast notice in field_invoke_method_multiple()
I did a quick grep through D8 code base. There's no mention of anything closely resembling the code from #1824820-65: String offset cast notice in field_invoke_method_multiple(). RTBCing again.
Comment #69
yched commentedYes, that API has been completely revamped in D8, iterating on fields and handling of field translation has nothing in common with D7 anymore.
Comment #70
kolafson commented#65 worked for me as well
Comment #71
drupov commented#65 works for me also.
Comment #72
nikolay shapovalov commented#65 works for me also.
Comment #73
pimok3000 commented# 65 works well on D 7.26
Comment #75
maximpodorov commentedComment #76
maximpodorov commented65: string-offset-cast-1824820-65-D7.patch queued for re-testing.
Comment #77
maximpodorov commentedThe patch was tested without errors! It fixes the problem - no notices.
Comment #79
maximpodorov commented65: string-offset-cast-1824820-65-D7.patch queued for re-testing.
Comment #80
maximpodorov commentedAll tests are passed again.
Comment #81
cameron prince commented#65 works well with current release version of D7.
Comment #82
adevms commented#65 is working for D7.
Comment #84
maximpodorov commented65: string-offset-cast-1824820-65-D7.patch queued for re-testing.
Comment #85
maximpodorov commentedAll tests are passed again.
Comment #86
David_Rothstein commentedThe original RTBC patch was #2 (which looks good to me), but this was changed in recent patches without much explanation. If the bug is that the code treats $options['language'] as an array even when it's not, why would we only fix this when $id is NULL (rather than actually checking that it's an array like #2 did)?
I guess we can live without tests if this is an obscure bug (and was presumably fixed as a side effect in Drupal 8 without any tests)...
Comment #87
heddnDue to the way that empty() functions in PHP 5.4, simply checking for is_array isn't sufficient. Checking for empty on $id is important. It could be an array but the warning still displays. #1824820-35: String offset cast notice in field_invoke_method_multiple() does a good job explaining.
Comment #88
pkiff commentedFor those wondering if the patch in #65 works with the field.attach.inc file included with Drupal 7.28, it does. There were no changes in field.attach.inc between 7.27 and 7.28, so you can use the same patch on 7.28.
Comment #89
drupov commentedYes, I can confirm that the patch works also on 7.28
Comment #91
David_Rothstein commentedOK, I read #35 again but I'm still not getting it. Can someone explain what I'm missing?
So why isn't #2 the correct approach?
Comment #92
crevillo commentedHi there.
David, what you say in #91 is exactly what i thought in doing #2
Further more, please remember that documentation for that function seems to allow other modules to provide that langcode without being an array.
http://cgit.drupalcode.org/drupal/tree/modules/field/field.attach.inc?h=...
it says
I cannot see what is the problem with #2 either...
Comment #93
siliconmind commentedSo, can we finally make this RTBC?
Comment #94
siliconmind commented65: string-offset-cast-1824820-65-D7.patch queued for re-testing.
Comment #96
David_Rothstein commented2: string-offset-cast-1824820-2.patch queued for re-testing.
Comment #97
David_Rothstein commentedWhich patch are you saying should be RTBC?
Comment #98
yched commentedPatch #2 should be RTBC.
Comment #99
a.milkovskyRe-rolled patch #65 without extra brackets
Comment #100
pkiff commentedIt will be confusing for new people arriving at this thread to figure out which patch is recommended by the community now. The patch in #2? Or #65, or now #99? I understood that the consensus was building for recommending the patch in #2 because it covers the more potential instances of the problem, as explained by David_Rothstein in post #91 above. Is that right?
Comment #102
skwashd commented@pkiff, the consensus of those in the know is that #2 be applied. It still applies cleanly and the tests pass.
Comment #103
gabrielmachadosantos commentedPatch #2 should be RTBC. Applied the patch and tested.
Comment #104
manuelBS commentedFor me patch 2 as well as 99 work.
Comment #105
danreb commentedPatch #2 works for me too - PHP 5.4 issue
Thanks
Comment #106
daniel kulbeNice - patch#2 worked for me too. Thanks a lot!
Comment #107
pjcdawkins commentedI'm also using the patch in #2
Comment #108
heddn+1 for #1824820-2: String offset cast notice in field_invoke_method_multiple()
Comment #109
siliconmind commentedCan we finally commit this? Please, the patch is ready for over a year now.
Comment #110
mr.alinaki commentedPatch from #2 is worked for me. Got this error when work with field collections and now it's gone!
Comment #111
coatezy commentedI would also like to see this patch committed.
Comment #112
miromarchi commentedI was used to use patch in #65, but I switched to patch in #2 too, and it's working perfectly.
Comment #113
dcam commentedRemoving unnecessary tag.
Comment #115
schifazl commented#87 says that what was done in patch #2 isn't sufficient, so it should be used patch #65, others say that patch #2 should be used... So which patch should I apply, waiting for the official commit?
Thanks.
Comment #116
pkiff commentedPatch #2 is currently the recommended patch. The comments in #87 are based on a misunderstanding of how #2 addresses the root problem of the bug, and this is explained in post #91 by David_Rothstein. The person who posted #87 (heddn), subsequently posted an endorsement of the patch in #2 (see #108).
And in #100 above, I asked the same question as you, and the answer then also recommended the patch in #2.
Comment #117
schifazl commentedThanks! There was so much confusion that even reading the thread two times I was still puzzled.
I'm hopefully waiting for a quick commit!
Comment #119
dcam commentedComment #120
dcam commentedPlease note I have no opinion on which patch is correct. I was simply resetting in bulk the multitude of RTBC issues which failed last week due to a problem with Testbot.
Comment #121
webadpro commentedWhen will this be commited?
Patch #2 seems to work.
Comment #122
giupenni commentedPatch #2 work fine.
Comment #123
Feral commentedWorks nicely with D7, thanks!
Comment #128
schifazl commentedPatch #2 passed (again) the test, it's the recommended patch and works fine for everyone, I think that it should be RTBC (again).
Comment #129
jwilson3Agreed. RTBC++. Patch in #2 solved my problem (getting this error on node/add/typename).
Comment #130
Håvard commentedPatch in #2 works like a charm and fixed an issue I had width validation of a date field and date popup-calendar.
Comment #131
mglamanThis is required when using Behat testing and the Drupal Extension to provide custom field values.
Comment #132
David_Rothstein commentedCommitted #2 to 7.x - thanks!
And thanks for all the testing, etc., to verify that the fix is working correctly.
Comment #135
David_Rothstein commentedHm...