Problem/Motivation
I want to use conditional fields in a form where the structure form array does not match the structure of the form_state values array. Drupal supports this by providing two sets of parents, the '#array_parents' for the $form and the '#parents' for $form_state['values']. In conditional_fields_attach_dependency the parents for a conditional field element are set equal to '#parents' and this is then used to get the widget element in the $form array. This is not correct.

Solution
Replace '#parents' with '#array_parents'.

This change will make Conditional Fields much more versatile when it comes to be a general API.

Questions
I'm not sure what to do about field parents.

CommentFileSizeAuthor
#4 2298953-4.patch811 bytesrlmumford
#1 2298953-1.patch1.56 KBrlmumford
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rlmumford’s picture

FileSize
1.56 KB

here's a patch.

rlmumford’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 1: 2298953-1.patch, failed testing.

rlmumford’s picture

FileSize
811 bytes
rlmumford’s picture

Status: Needs work » Needs review
rlmumford’s picture

There's another contrib module that now requires this patch #2124191: Introduce Conditional Fields support

andrewbelcher’s picture

Status: Needs review » Reviewed & tested by the community

I can confirm that the patch in #4 solves this problem.

rlmumford’s picture

Priority: Major » Critical

Can this be committed please? It's been 9 months!

  • peterpoe committed 12ab0cb on 7.x-3.x authored by rlmumford
    Issue #2298953 by rlmumford: conditional_fields_attach_dependency uses...
peterpoe’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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

mbaynton’s picture

This patch looks dangerous. Here's a snippet with the patch applied:

  if (isset($dependee['#parents'])) {
    $form['#conditional_fields'][$dependee['#field_name']]['parents'] = $dependee['#array_parents'];
    $form['#conditional_fields'][$dependee['#field_name']]['dependents'][$id] = array(
      'dependent' => $dependent['#field_name'],
      'options'   => $options,
    );
  }

Note that it is guarded by an isset($dependee['#parents']), which is the thing that used to be read within the if block. Now, we read $dependee['#array_parents'], which could be undefined if the function is called with no $id and a field name string in $dependee.

I suspect a good solution would be to just assume #array_parents should equal #parents for the string-as-$dependee case. I'd be happy to do a patch on this issue or a separate one if requested.