I am attempting to use conditional fields in hook_form_alter() with non-CCK form fields. I can't figure out where to do it. I'd rather use the existing setup then roll my own jquery function to do this.

I added a row to the conditional_field database like this:

control_field_name = test_controller
field_name = test_child
type = mynodetype
trigger_values = a:1:{i:1;s:1:"1";}

And then attempt to use it like this:


function modulename_form_alter($form_id, &$form) {
  if ($form_id != 'mynodetype_node_form') {
    return;
  }
  
  if (module_exists('conditional_fields')){
    $form['test_controller']=array(
      '#type' => 'checkbox',
      '#title' => 'Show another field?',
      '#default_value' => 0,
    );
    $form['test_child']=array(
       '#type' => 'textfield',
      '#title' => 'This field should hide',
    );

    conditional_fields_node_editing_form($form);
  }

}

The hook is being called, and modifying the form and loading the conditional js, but it doesn't seem to do anything with the conditional stuff, except add this to Drupal.settings:

{ "ConditionalFields": {
  "controlling_fields": {
    "#conditional-test-controller": {
      "#conditional-test-child": {
        "field_id": [ "#conditional-test-child", "#conditional-test-child" ],
        "trigger_values": { "1": "1" }
      }
    }
  }
} 

How do I set the parent field DIVs to have the correct IDs?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

peterpoe’s picture

Version: 5.x-2.x-dev » 5.x-1.x-dev

This is a use of conditional fields I hadn't thought about, so I am not sure what is missing.

Check that conditional fields are inclosed in a DIV with:
id = "conditional-". form_clean_id(FIELD NAME),
class = "conditional-field controlling-field" or "conditional-field controlled-field"

Also check that you are using the 5.x-1.x-dev version, since 5.x-2.x-dev is outdated.

peterpoe’s picture

I forgot to mention:

the conditional field module has a weight of 10 (so it can handle fieldgroups). This might also cause issues with your approach?

konsumer’s picture

Thanks. I thought that I was missing the function to wrap the fields in a div with the proper ID.

I made sure I was using 5.x-1.x-dev (I may have been before, and just marked the wrong version.)

I realized I don't need the conditional_fields_node_editing_form($form) if the form name is setup in the db.

What I'm actually trying to do is hook to things that don't have standard fields, like from ecommerce and event.

I get this error:

Fatal error: Cannot use string offset as an array in sites/all/modules/conditional_fields/conditional_fields.module on line 477

I looked at 477, and the issue seems to be

$form['#node']->{$field['control_field_name']}[0]['value']

It's hardset to use [0]['value'], but product-type field stuff doesn't use that (neither does event)

I think I will just use my own solution.

In looking at this, it occurred to me that it might make sense to have a marker on the form element, of some sort, and be able to process the form, instead of relying on the db for every part of it.

here is an example of pseudo-form-code:

function modulename_form_alter($form_id, &$form) {
  if ($form_id != 'mynodetype_node_form') {
    return;
  }
  if (module_exists('conditional_fields')){
    $form['test_controller']=array(
      '#type' => 'checkbox',
      '#title' => 'Show another field?',
      '#default_value' => 0,
      '#conditional'=>'controller',     
    );
    $form['test_child']=array(
       '#type' => 'textfield',
      '#title' => 'This field should hide',
      '#conditional'=>'child',
      '#conditional_eval' =>'return ($form["test_controller"]["#value"] == 1);',
    );
  }

}

This way, you could inject stuff to be processed, and not need to put things in the database (for non-CCK stuff), or rely on any prescribed value format. This would allow any module to take advantage of conditional fields, and use them in all kinds of unexpected ways (the conditional_eval field would allow developers to check for things other then a field value, too...)

This could also make it easier to make conditional sub-modules that handle special things, like product forms, event forms, user forms, whatever.

Using field theming functions, you could also add the wrapper DIVs, based on those #conditional vals, so it would be transparent, take less code, and allow developers to over-write it, if they need to change how it outputs it (like in my case.)

peterpoe’s picture

Lots of interesting proposals...

Different field structures were the hardest things I had to deal with, and since I wasn't able to find a solution, I resolved to support for now only the default cck widgets.

But it would be great to give more abstraction to conditional fields... I'll see if I can work something out.

In the meantime, I started working on the theme part, and moved the wrapping of fields out of #prefix and #suffix, to a theme function. I'm not sure if I did it the right way, and fieldgroups wrapping is still hardcoded, but it's a start.

The patch is attached here, and it has already been committed.

Thank you very much!

konsumer’s picture

More about these ideas here: #231384: Controlling field ID in fieldset not consistent with ID in Drupal.settings.ConditionalFields.controlling_fields

I implemented a slightly different system for the jquery part, that doesn't require any information about he parent fields, or the id's to be in any particular format.

I tried out newest 5.x-1.x-dev version (that has your patch) and I got:

warning: Invalid argument supplied for foreach() in
{PATH}sites/all/modules/conditional_fields/conditional_fields.module on line 193.

In the display phase.

I got rid of the error with

if (!empty($node->content[$group]) && is_array($node->content[$group])){

but he conditionals no longer work on the edit form. In my form the groups are turned off on display (unstyled) and they are just for partitioning fields for the admin interface. Even on a "normal" sort of setup, it still doesn't do the conditional stuff, if they are in a fieldgroup (they are in the same one.)

peterpoe’s picture

Title: hook_form_alter() » Custom conditional fields
Version: 5.x-1.x-dev » 6.x-1.x-dev
Component: Documentation » Code
Category: support » feature

I started some concept work on what would be required to add custom conditionals dynamically with other wodules:

- Node form altering and node viewing:
Conditional fields module should provide a hook function for custom data. Data is structured like this:

  array(
    [0] =>
      array(
        'field_name' => 'CONTROLLED FIELD NAME',
        'control_field_name' => 'CONTROLLING FIELD NAME',
        'trigger_values' => array('KEY1' => 'KEY1', [...], 'KEYn' => 'KEYn')
      ),
    [n] => ...
  )

The data should also include the options that are normally stored in variables (c_fields_...)

All should work with just this hook, with the only limitation of groups: for these I need to figure out a way to move a the wrapping inside our divs in a theme function. But with drupal 6 this should be easy.

ntt’s picture

subscribing

patchak’s picture

This is really interesting, is there any new work or patches to test for this? I would love to have some taxonomy fields provided by core to respond to other fields... I can't use content taxonomy so I would need to control the core fields (with hierarchical select)

Thanks!
Patchak

scottrigby’s picture

@peterpoe: This would be great - need any help?

peterpoe’s picture

@scottrigby: Yes, I'm stuck. I can't find a way to move the markup of controlled fieldgroups to a theme function. The themeing of fieldgroups happens inside the fieldset, so I can't wrap the needed div outside of it!

dums2000’s picture

Title: Custom conditional fields » conditional fields/group with "TAXONOMY controlling field"

Hello ,
I'm really looking for conditional fields/group like this module, but with TAXONOMY controlling field.
Actually, I'm using hierarchical select to look up into taxonomy vocabulary, and I need to call a particular group of field for a particular taxonomy.

I have discovered drupal there is a month, read some books and test ......and I would very please to help...but my experience with module is just some module tutorial....If somebody would tell me what function in .module file I can touch, and how to search for taxonomy...

Thanks to you,
Christophe, from France.

jorge’s picture

Subscribe.
This would be a powerful feature.

ntg’s picture

suscribing

Bilmar’s picture

subscribing

peterpoe’s picture

Title: conditional fields/group with "TAXONOMY controlling field" » Custom conditional fields
Version: 6.x-1.x-dev » 6.x-2.x-dev

I realize now that this issue had been "hijacked" on #11.
If you need compatibility with Taxonomy, please refer to: #250748: Integrate with Content Taxonomy.

This discussion about custom conditional fields could be useful for #648934: Plugin system for compatibility with non-core CCK modules.

Stomper’s picture

I am looking for fields to be displayed based on a taxonomy vocabulary term. This is an UberCart application where some fields only apply to certain product types.

I do not want to have to create different product classes/content types, I'd rather just control whether the fields are shown.

kenorb’s picture

+1

peterpoe’s picture

Status: Active » Closed (outdated)