Drupal 7: a dynamic muldimensional array to form, how use dynamical handler (with dynamicals submit)

StackOverflow https://stackoverflow.com/questions/21924721

  •  14-10-2022
  •  | 
  •  

Domanda

(sorry for my bad english writing x) )

Hi ! I have a multidimensional array like this :

A
    A1
    A2
B
    B1
C
    C1
    C2
    C3
...

I want display this array into a form (I did it =) ). And for each element (A, A1, A2, B...) I want add an ajax remove button like this :

A x
    A1 x
    A2 x

So, for each element, I add a submit form with ajax arguments like this :

foreach (...){
    ...
    $form[$tree][$subtree][$id]['remove'] = array(
        '#type' => 'submit',
        '#submit' => array('_delete_element_submit'),
        '#ajax' => array(
            'callback' => '_delete_element_ajaxcallback',
            'wrapper' => 'my-html-id',
        ),
    );
    ...
}

I thought I could use an argument when I use the handler for each handler. But after google searchs, Its seems its impossible to pass argument to handler :/ It is impossible to create dynamicals handler ? :/ What is the correct/best way for implement a delete ajax-button for each element of a dynamical form ?

Thank you =) I hope Im clear !

È stato utile?

Soluzione

Hope following code would be helpful:

 $form['names_fieldset']['remove_useful_name'][$key] = array(
            '#type' => 'submit',
            '#value' => t('delete' ),
            '#submit' => array('remove_one_method'),
            '#ajax' => array(
                'callback' => 'add_more_method',
                'wrapper' => 'names-fieldset-wrapper',
                // add trigger 
                'trigger_as' => array(
                  'name' => 'submit_name'.$key,
                ), //
            ),
        );

function remove_one_method($form, &$form_state) {
    dd($_POST['_triggering_element_name']);
    $submit_name= $_POST['_triggering_element_name'];
    $key= str_replace ('submit_name','',$submit_name);
    // You can get the $key here.
    // Base this $key remove the component from the form.
    ......
    $form_state['rebuild'] = TRUE;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top