0

So I'm struggling with the concept of trees in Drupal. I have a form that presents a user with a drop down of number 1 - 27 depending on what they pick the form renders out an equal number of text fields. The goal is to collect all the values put into the text fields into an array even if it's a single element. Right now all that seems to happen is the last field is written as a string to the variable.

What I have:

if (!isset($form_state['values']['number_subscribers'])) {
$form_state['values']['number_subscribers'] = 0;
}

for($i = 0; $i < $form_state['values']['number_subscribers']; $i++){
$form['subscribers_wrapper_container'][]["#tree"] = TRUE;
$form['subscribers_wrapper_container'][]['UCM_SUB'] = array(
'#type' => 'textfield',
'#size' => 30,
'#title' => t('Subscriber IP Address'),
'#required' => TRUE,
);
}

End goal would be to read $form_state['values']['UCM_SUB'] and have it return an array.

1
1

This:

$form['subscribers_wrapper_container'][]["#tree"] = TRUE;
$form['subscribers_wrapper_container'][]['UCM_SUB'] = array(...

Can't be what you meant to do. You're creating two new elements there, one with only #tree in it.

Surely you want the tree on the outer element, e.g.

$form['subscribers_wrapper_container']['UCM_SUB']['#tree'] = TRUE;

for($i = 0; $i < $form_state['values']['number_subscribers']; $i++){
  $form['subscribers_wrapper_container']['UCM_SUB'][] = array(
    '#type' => 'textfield',
    '#size' => 30,
    '#title' => t('Subscriber IP Address'),
    '#required' => TRUE,
  );
}
2
  • That is exactly what I wanted. Thank you, perhaps one day working with Drupal won't feel like groping around in the dark. – rsaturns Oct 31 '14 at 17:04
  • Whenever it feels dark again, we have Clive to the rescue :) Seriously I got many of my questions answered from hims DrupalAnswers answers. – AKS Oct 31 '14 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.