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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

eric.chenchao created an issue. See original summary.

eric.chenchao’s picture

Assigned: eric.chenchao » Unassigned
Status: Active » Needs review
FileSize
726 bytes

Here is the patch.

eric.chenchao’s picture

Issue summary: View changes

  • colan committed b1caa93 on 8.x-1.x authored by eric.chenchao
    Issue #2898893 by eric.chenchao: Assume we already have an array.
    
colan’s picture

Status: Needs review » Fixed

Makes sense to me.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

JonMcL’s picture

I know this is closed, but I am unsure if the patch fixed everything. There are similar lines of code in DefaultStateHandler::statesHandler and OptionsButtons::checkBoxesHandler. I know I am getting the same error in DefaultStateHandler.

colan’s picture

Status: Closed (fixed) » Active
Baysaa’s picture

Status: Active » Needs review
FileSize
714 bytes

The 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)

M_Z’s picture

I 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.

Baysaa’s picture

Thanks #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!

afarino’s picture

Was this ever successfully patched? I don't have the ability to test it myself but am also encountering this issue.

ldegoursac@gmail.com’s picture

@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.

rooby’s picture

Priority: Normal » Major

The 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.

TLWatson’s picture

Patch #11 resolved errors for me.