phpdrupaldrupal-7drupal-forms

Render order of Field Sets in a Drupal Form


I have a form that creates multiple entries in a Fieldset depending on a counting variable $form_state['num_entries']. It contains a Textfield for a region, a Slider element and a hidden field for the slider value.

I need the render order to be: The Textfield, The Slider, The Hidden field.

But The form Just renders all the regions, then all the sliders and so on. How can I fix the render order?

Thanks for your time in advance!

  for ($i = 0; $i < $form_state['num_entries']; $i++) {


    $form['region_fieldset']['places'][$i] = [
      '#type' => 'textfield',
      '#title' => "Region" . " " . ($i + 1),
      '#size' => 22,
      '#attributes' => ['multiple' => 'multiple'],
      '#default_value' => isset($default_values["regions"][$i]) ? $default_values["regions"][$i] : '',

    ];

    $form['region_fieldset']["radius_slider"][$i] = [
      '#type' => 'slider_form_element_field',
      '#title' => 'Radius',
      '#attributes' => ['multiple' => 'multiple', 'id' => "slider_" . $i],
      '#default_value' =>  40,
    ];

    $form['region_fieldset']['slider_value'][$i] = [
      '#type' => 'hidden',
      '#default_value' => isset($form['entry_fieldset']["test-slider"]["#default_value"]) ? $form['entry_fieldset']["test-slider"]["#default_value"] : 40,
      '#attributes' => [
        'class' => ['slider-hidden-value'],
        'multiple' => 'multiple',
        'id' => "slider_value_" . $i,
      ],
    ];
  }

Solution

  • Okay I found it. It was a mistake with the order of the keys in the arrays. Here is the correct way:

     for ($i = 0; $i < $form_state['num_entries']; $i++) {
    
    
        $form['region_fieldset']['regions'][$i]["places"] = [
          '#type' => 'textfield',
          '#title' => "Region" . " " . ($i + 1),
          '#size' => 22,
          '#attributes' => ['multiple' => 'multiple'],
          '#default_value' => isset($default_values["regions"][$i]) ? $default_values["regions"][$i] : '',
    
        ];
    
        $form['region_fieldset']["regions"[$i]["radius_slider"]] = [
          '#type' => 'slider_form_element_field',
          '#title' => 'Radius',
          '#attributes' => ['multiple' => 'multiple', 'id' => "slider_" . $i],
          '#default_value' =>  40,
        ];
    
        $form['region_fieldset']["regions][$i]["slider_value"] = [
          '#type' => 'hidden',
          '#default_value' => isset($form['entry_fieldset']["test-slider"]["#default_value"]) ? $form['entry_fieldset']["test-slider"]["#default_value"] : 40,
          '#attributes' => [
            'class' => ['slider-hidden-value'],
            'multiple' => 'multiple',
            'id' => "slider_value_" . $i,
          ],
        ];
      }