Feature exports default value for this field instance as integer, rather than string:
// Exported field_instance: 'commerce_customer_profile-billing-field_test'.
$field_instances['commerce_customer_profile-billing-field_test'] = array(
'bundle' => 'billing',
'default_value' => array(
0 => array(
'value' => 0,
),
),
'deleted' => 0,
'description' => '',
'display' => array(
'default' => array(
'label' => 'above',
'module' => 'list',
'settings' => array(),
'type' => 'list_default',
'weight' => -9,
),
),
'entity_type' => 'commerce_customer_profile',
'field_name' => 'field_test',
'label' => 'Test',
'required' => 1,
'settings' => array(
'user_register_form' => FALSE,
),
'widget' => array(
'active' => 1,
'module' => 'options',
'settings' => array(),
'type' => 'options_buttons',
'weight' => -9,
),
);
Comments
Comment #3
sardis commentedOops, previous patch was created with IDE. Resubmitting patch made with git diff.
Comment #4
br0kenMight be it's late hour but I didn't understand how type checking can resolve the problem between integers and strings. Also, I see the case why your changes break BC.
In general, I doubt it is the bug in the core rather than in
features.Can you provide a test?
Comment #5
sardis commentedWell, i agree, it seems like a bad export of field instance via
features.But apparently, there could be cases, where
$element['#value']is not a string.And here's what happens according to PHP manual when you compare Integer with String:
In our case:
And this is not what we want our code to do.
However:
That's why i decided to use
===operator. And it did the trick.So what should we do about it?
Comment #6
br0kenBy adding type comparison here you'll definitely break compatibility for some part of users which had this working for years. Not all code produced by Features. Someone may create fields programmatically and, to be honest, I saw too much PHP code for Drupal 7 without any thoughts about types (e.g.
'0'or0).What do you think about casting a
stringtype for this particular operand in condition?Comment #7
sardis commentedI like this idea as well. I'll make a patch bit later so that you could review.
Comment #8
David_Rothstein commentedI don't understand what the practical backwards compatibility concern would be with the above patch (#4 is interesting but seems theoretical). That said, the revised solution of casting to a string before checking sounds good too!
I agree that automated tests would be a good idea here. It should be possible to do something simple (not involving Drupal Commerce) that programmatically saves the field instance with a default value of integer zero in order to trigger the bug.
Thanks for a very detailed bug report!
Comment #9
br0kenActually core has these tests (in
options.test) but they do not show the problem because of web requests which convert integers to strings (see\OptionsWidgetsTestCase::testRadioButtons()). So, the real good example is to create a custom form with theradioselement having theoptions_field_widget_validatefunction for its#element_validate.@David_Rothstein, as I've shown it in my previous comment, field value can be an object, which can be interpreted as a string (it's an edge case, I agree).
Comment #10
br0kenHm, I've tried to create that one and realized that I was wrong. We definitely need a test which is able to reproduce this because submitted values are always converted to strings. Might it be that some of the modules (there are a lot in "commerce" stack) modify the type?
Comment #11
sardis commentedFrom the start i've tried to reproduce bug in easier way. Simply by creating field instance programmatically with default value set in int and then go to page, where this widget is used (it was node add form), but i had the same result.
That's why i had to include whole Drupal Commerce stack in order to reproduce bug, which happens on my dev server.
So i think, you made a good point, @BR0kEN: this might be commerce. But it has to be proved. I'll try and find out.
Comment #12
br0kenBoth of these patches must lead tests to fail. But the problem is not self-generated, as you can see (I purposely added wrong validation function).
Comment #13
br0kenComment #14
br0ken@Sardis, you need to put a breakpoint at the first line of the
options_field_widget_validate()and step by the stack trace until you find the point where data modifies.Comment #17
br0kenSo now when tests finished you may see the "problem". Of course we can rectify the core a bit but in general, it isn't a problem of the core.
Comment #18
David_Rothstein commentedIt's probably possible to have the tests force it to be an integer by submitting the form programmatically using drupal_form_submit(). However, I have no idea if that corresponds to anything the Commerce modules are doing.
What's also interesting is that if $element['#value'] ever does wind up set to integer 0 on a required field, the built-in form API validation should reject it also (see https://api.drupal.org/api/drupal/includes%21form.inc/function/_form_val... and in particular the $is_empty_value variable). So it's not obvious why fixing this in the Options module would actually improve things.
Definitely agree that some more debugging would be useful to understand what is going on.
Comment #19
anrikun commentedMarked #2287743: Options widget counts 0 as "none selected" as a duplicate.
Comment #20
anrikun commentedApplied patch #13
Get Notice : Array to string conversion in options_field_widget_validate() (line 172 in [...]\modules\field\modules\options\options.module). when cardinality is multiple.
Comment #21
anrikun commentedHere is a new patch (that needs tests).