Drupal 8, how build a hierarchical form?

matthieu lopez

In Drupal 8, I don't understand how build a "hierarchical" form.

I have this sample form

...
public function buildForm(array $form, FormStateInterface $form_state) {
    $form['description'] = array(
        '#type' => 'fieldset',
        '#title' => t('Main description'),
    );
    $form['description']['subfirst'] = array(
        '#type' => 'textfield',
        '#title' => t('subfirst'),
    );
    $form['description']['subsecond'] = array(
        '#type' => 'textfield',
        '#title' => t('subsecond'),
    );

    $form['content'] = array(
        '#type' => 'fieldset',
        '#title' => t('Main description'),
    );
    $form['content']['subfirst'] = array(
        '#type' => 'textfield',
        '#title' => t('subfirst'),
    );
    $form['content']['subsecond'] = array(
        '#type' => 'textfield',
        '#title' => t('subsecond'),
    );

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Submit',
    );
    return $form;
}

public function submitForm(array &$form, FormStateInterface $form_state) {
    dpm($form_state->getValues(),"getValues");
}
...

enter image description here

When I submit my form, my form_state->getValues() return :

enter image description here

The form_state->getValues() contains only the ['content']['subfirst'] and ['content']['subsecond'] values... That means I must use uniques labels with the form api ? I find it weird...

Then, I change my form :

$form['content']['subfirst'] become $form['content']['totosubfirst']

$form['content']['subsecond'] become $form['content']['todosubsecond']

The new code :

public function buildForm(array $form, FormStateInterface $form_state) {
    $form['description'] = array(
        '#type' => 'fieldset',
        '#title' => t('Main description'),
    );
    $form['description']['subfirst'] = array(
        '#type' => 'textfield',
        '#title' => t('subfirst'),
    );
    $form['description']['subsecond'] = array(
        '#type' => 'textfield',
        '#title' => t('subsecond'),
    );

    $form['content'] = array(
        '#type' => 'fieldset',
        '#title' => t('Main description'),
    );
    $form['content']['totosubfirst'] = array(
        '#type' => 'textfield',
        '#title' => t('subfirst'),
    );
    $form['content']['totosubsecond'] = array(
        '#type' => 'textfield',
        '#title' => t('subsecond'),
    );

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Submit',
    );
    return $form;
}

And when I submit my form, my form_state->getValues() return :

enter image description here

I get the four values. But, they are in the same hierarchical level. How I use the form api for have a form_state like this :

'description' => 'subfirst' => string(3) "AAA"

'description' => 'subsecond' => string(3) "BBB"

'content' => 'totosubfirst' => string(3) "CCC"

'content' => 'totosubsecond' => string(3) "DDD"

?

I want get a hierarchical form_state because after I want create a custom function like :

foreach element in description
  do that
foreach element in content
  do that
...
matthieu lopez

The solution

Set #tree => TRUE on the parent elements, then the values will be nested accordingly

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

"Hierarchical Select" alternate module for Drupal 8

From Dev

How to add entity reference as a form field drupal 8? I it possible?

From Dev

how to add checkbox in field settings form in Drupal 8

From Dev

How to change option name in Drupal 8 form element?

From Dev

How can I get the Register form on the front page in Drupal 8?

From Dev

How to pass a value from javascript to drupal 8 form?

From Dev

How to alter the drupal8 user login form inputs and validation?

From Dev

Drupal 8 custom registration form

From Dev

Manipulate form element in Drupal 8

From Dev

Drupal 8 Custom form with popup

From Dev

How to log-in to Drupal if there is no form

From Dev

how to intetrupt a form submission in drupal

From Dev

How to build hierarchy paths with hierarchical subqueries

From Dev

Drupal 8: How do I customize a form widget to show an entity field value instead of the entity title?

From Dev

How do I check if the current password that the user gives in the password reset form matches with the hashed password in Drupal 8

From Dev

Drupal 8 YAML Form Module: How do I change the value of a Hidden element?

From Dev

How to create an advanced search form using the Drupal 8 Solr Search module?

From Dev

How to build a webform with custom fields in drupal 7?

From Dev

How to add image field in form drupal 7

From Dev

How to create a form and send it to a template file in Drupal

From Dev

How to push form values to database in Drupal

From Dev

How to render image inside a form in drupal

From Dev

how to add custom HTML form in drupal

From Dev

Drupal 8 Customize template content type form page

From Dev

Drupal 8 Customize template content type form page

From Dev

Drupal 8 Abstract method error when building a simple form

From Dev

How to build hierarchical objects with siblings tags using jquery selectors

From Dev

how to build Hierarchical array using specific key of single dimensional array

From Dev

How to programmatically create a node in Drupal 8?

Related Related

  1. 1

    "Hierarchical Select" alternate module for Drupal 8

  2. 2

    How to add entity reference as a form field drupal 8? I it possible?

  3. 3

    how to add checkbox in field settings form in Drupal 8

  4. 4

    How to change option name in Drupal 8 form element?

  5. 5

    How can I get the Register form on the front page in Drupal 8?

  6. 6

    How to pass a value from javascript to drupal 8 form?

  7. 7

    How to alter the drupal8 user login form inputs and validation?

  8. 8

    Drupal 8 custom registration form

  9. 9

    Manipulate form element in Drupal 8

  10. 10

    Drupal 8 Custom form with popup

  11. 11

    How to log-in to Drupal if there is no form

  12. 12

    how to intetrupt a form submission in drupal

  13. 13

    How to build hierarchy paths with hierarchical subqueries

  14. 14

    Drupal 8: How do I customize a form widget to show an entity field value instead of the entity title?

  15. 15

    How do I check if the current password that the user gives in the password reset form matches with the hashed password in Drupal 8

  16. 16

    Drupal 8 YAML Form Module: How do I change the value of a Hidden element?

  17. 17

    How to create an advanced search form using the Drupal 8 Solr Search module?

  18. 18

    How to build a webform with custom fields in drupal 7?

  19. 19

    How to add image field in form drupal 7

  20. 20

    How to create a form and send it to a template file in Drupal

  21. 21

    How to push form values to database in Drupal

  22. 22

    How to render image inside a form in drupal

  23. 23

    how to add custom HTML form in drupal

  24. 24

    Drupal 8 Customize template content type form page

  25. 25

    Drupal 8 Customize template content type form page

  26. 26

    Drupal 8 Abstract method error when building a simple form

  27. 27

    How to build hierarchical objects with siblings tags using jquery selectors

  28. 28

    how to build Hierarchical array using specific key of single dimensional array

  29. 29

    How to programmatically create a node in Drupal 8?

HotTag

Archive