We are using condition_fields with the contact form. After upgrading to the latest dev release, we start to see the following errors
Warning: explode() expects parameter 2 to be string, array given in Drupal\conditional_fields\Plugin\conditional_fields\handler\OptionsButtons->radioHandler() (line 36 of modules/contrib/conditional_fields/src/Plugin/conditional_fields/handler/OptionsButtons.php).
The original error is caused in \Drupal\conditional_fields\Plugin\conditional_fields\handler\OptionsButtons::radioHandler
Code 1
protected function radioHandler($field, $field_info, $options) {
$select_states = [];
$values_array = empty($options['values']) ? $options['values'] : explode("\r\n", $options['values']);
At the time $options['values']
is already an array set in the
Code 2
function conditional_fields_form_after_build($form, FormStateInterface &$form_state) {
...
if (!empty($options['values']) && is_string($options['values'])) {
$options['values'] = explode("\r\n", $options['values']);
}
This is caused by duplicate fixes in different places which are all applied to the code.
Code 1: #2886483: Dependency on select list values not exploded
Code 2: #2884354: Conditional Fields No Longer works with select lists
Comment | File | Size | Author |
---|---|---|---|
#11 | conditional_fields-explode_expects_string-2898893-11.patch | 7.32 KB | Baysaa |
Comments
Comment #2
eric.chenchao CreditAttribution: eric.chenchao commentedHere is the patch.
Comment #3
eric.chenchao CreditAttribution: eric.chenchao commentedComment #5
colanMakes sense to me.
Comment #7
JonMcL CreditAttribution: JonMcL commentedI know this is closed, but I am unsure if the patch fixed everything. There are similar lines of code in
DefaultStateHandler::statesHandler
andOptionsButtons::checkBoxesHandler
. I know I am getting the same error in DefaultStateHandler.Comment #8
colanComment #9
Baysaa CreditAttribution: Baysaa as a volunteer commentedThe issue described in #7 arises from the fact that $options['values'] is already an explode()'d array. The explode() happened in conditional_fields_form_after_build() (/conditional_fields.api.inc:69)
Comment #10
M_Z CreditAttribution: M_Z commentedI think that the patch in #9 only fixes the first problem described in #7 (DefaultStateHandler::statesHandler).
I got an error that seems to belong to the second problem described in #7 (OptionsButtons::checkBoxesHandler).
To reproduce: Add a multiple (unlimited) taxonomy term (entity reference) field with the checkboxes form widget as "controlled by" field and go to the node add form. There will be the error message and the conditional field logic won't work.
Comment #11
Baysaa CreditAttribution: Baysaa as a volunteer commentedThanks #10 M_Z.
I have gone through and removed the unnecessary explode()'s and $values_array assignments and just used $options['values'] instead. Could use someone to test it, thanks!
Comment #12
afarino CreditAttribution: afarino commentedWas this ever successfully patched? I don't have the ability to test it myself but am also encountering this issue.
Comment #13
ldegoursac@gmail.com CreditAttribution: ldegoursac@gmail.com commented@afarino, the patch from #11 did stop the error.
I then used the "regular expression" method to specify an expression matching values for my entity reference dependee field that would force a 2nd entity reference field to be required. Note - it didn't work with a regular expression matching node ids or uuids, but rather matching node titles. So that is working for me.
Comment #14
rooby CreditAttribution: rooby commentedThe patch in #11 fixes a bunch of errors for me.
Glancing over the code in the patch, it looks good to me, although I don't know enough about conditional_fields to know if it's doing anything that could cause other issues.
One thing I would say is that ConditionalFieldsHandlersPluginInterface::statesHandler() needs more information in the doc block describing what the parameters are expected to be, most importantly the $options parameter of statesHandler().
Also, I don't think this is normal priority, as it breaks my site. Maybe even critical, although it doesn't seem that everyone has this problem or there might be more input into this issue.
Comment #15
TLWatsonPatch #11 resolved errors for me.