I'm using hook_form_alter() to access some cck fields. I access most of the cck fields like
$t = $form['field_foo']['#type'];
$v = $form['field_foo']['#default_value'];
However, for some fields, I have to index an additional [0] and use different formats
$t = $form['field_bar'][0]['#type'];
$v = $form['field_bar'][0]['#default_value']['value'];
I noticed that if a nodereference field uses autocomplete, then I have to reference the 2nd way. If I use 'selectlist' then I need to use the 1st format. There are even some other variations. Are there rules on how to access these fields? (I skimmed the documents and didn't quite find an answer).
I am basically using debug to figure out which format to use. I am concerned that I'm doing something ad hoc that may break later.
I hope some expert can shine some lights on this.
Thx!
Comments
Comment #1
markus_petrux commentedThis basically depends on whether the CCK field manages multiple values itself or not. You may wish to look at includes/content.node_form.inc, function content_field_form().
You may also wish to look at the multigroup module, where these things are a bit more complex: #119102: Combo field - group different fields into one
PS: Switching issue component to something a bit more generic.
Comment #2
yched commentedMarkus is right.
Put a little differently :
- Most widgets are 'single-valued' : one widget holds one value, and several 'copies' of the widget are displayed to receive multiple values. That's typically textfield-based widgets.
For those, the form will be structured as in your second example :
$form['field_bar'][0]; $form['field_bar'][1]; ...(even if your field is single : CCK treats single fields as a degenerate case of multiple fields, with only one value. This keeps the form and node structures from changing when you change a field from single to multiple)- Some widgets are inherently 'multiple-valued' : *one* widget is used to enter multiple values? That's typically select/checkboxes-based widgets.
For those, well, it's actually up to the widget how it structures itself under
$form['field_bar'], but for CCK's 'optionwidgets' you'll find the widget directly under$form['field_bar'], as in your first example.Comment #3
newbuntu commentedThanks for both of your answers. I suspected that it had to do with multi-fields. In fact, I tried to avoid any explicit multi-fields.
In any case, it sounds like that's how I need to use them in each case.
Just for general discussion here are some of the variations I use in the same cck type:
As you can see, the formats are kinda of mind boggling. I'm relying on debugger to figure out which format to use.