8

I am using drupal 7. I need to add extra arguments to my ajax callback function. I have five fieldsets in my form, and in all of them same there are same elements, just their IDs are different. For example; group-1, group-2... I need to include that 1,2,3.. part in the callback function as arguments. Otherwise, it will be too complicated to build a logic to predict which fieldset element need to change.

For example, is such a an implementation possible?

function mymodule_form($form, $form_state) {
    $group = 1;

    // Some form elements...

    $form['button'] = array(
        '#type' => 'button',
        '#ajax' => array(
            'callback' => 'my_callback_function',
            'wrapper' => 'my_target_area',
            'arguments' => array($group1, ...) // Is such a thing possible or another way? 
        ),
    );
}

function my_callback_function($args, $form, $form_state) {
    $arguments = $args;
    return $form['my_target_area'];
}

This question as already been asked here. But I those answers don't fit my needs mentioned above. Thank you.

4 Answers 4

9

Drupal doesn't have a mechanism to do that, no. As mentioned in the post you linked to, there's no need to pass that data to Javascript and then back to PHP though - just keep it in the form:

function mymodule_form($form, $form_state) {
  $form['button'] = array(
    '#type' => 'button',
    '#ajax' => array(
      'callback' => 'my_callback_function',
      'wrapper' => 'my_target_area',
    ),
  );

  $form['#foo'] = array($group1, ...);

}

function my_callback_function($form, $form_state) {
  $arguments = $form['#foo'];
  return $form['my_target_area'];
}
1
  • I'm trying to do the same thing, pass the $form array to my callback function. From this code it appears the arguments are available, but the $form argument appears empty when the callback is called. function my_ajax_cb($form, $form_state) { dpm($form);
    – icicleking
    Aug 15, 2017 at 18:28
6

For pass data to ajaxCallback, this is how i do :

$form['repas'][$i]['detail']['midi']['mois_midi_'.$i] = [
              '#type' => 'select',
              '#title' => $this->t('Mois'),
              '#data'=>$i,
              '#options'=>array(
                  ''=>'Tous',
                  '01'=>'Janvier',
                  '02'=>'Février',
                  '03'=>'Mars',
                  '04'=>'Avril',
                  '05'=>'Mai',
                  '06'=>'Juin',
                  '07'=>'Juillet',
                  '08'=>'Aout',
                  '09'=>'Septembre',
                  '10'=>'Octobre',
                  '11'=>'Novembre',
                  '12'=>'Décembre',

              ),
              '#ajax'   => [
                  'event' => 'change',
                  'effet'=>'fade',
                  'wrapper'=>'legumes'.$i,
                  'method'=>'replace',
                  'callback' => array($this,'changeLegumeCallback'),
              ],
              '#default_value'=>$mois,
          ];

i add a custom attributes to my array : #data

Then in the callback :

public function changeLegumeCallback(array &$form, FormStateInterface $form_state){
    echo $form_state->getTriggeringElement()['#data'];
    exit();

}

You ll get your dynamic value from the trigger Element.

1
  • For me it ended up being $i = $form_state->getTriggeringElement()['#field_parents'][1] for node form field.
    – No Sssweat
    Jun 8, 2022 at 12:12
0
$form["action"] = [
  "#type" => "button",
  "#value" => "Retirer",
  "#arg" => $id,
  "#attributes" => ["class" => ["button button--small"]],
  "#ajax" => [
    "callback" => [$this,"remove_chiffre_item"],
    "event" => "click",   
    "progress" => [],
  ],
];

public function remove_chiffre_item(array &$form, FormStateInterface $form_state) {

  $element= $form_state->getTriggeringElement()["#arg"]; // $form["action"]["#arg"]
  drupal_set_message($element);
  return $form;
}
0

an other solution (I had to loop on inputs) :

$form['propositions_fieldset'][$i]['add_date_de_pose'] = array(
'#type' => 'submit',
'#value' => t('Ajouter une date de pose'),
'#submit' => array('ajax_date_de_pose_add_more_add_one'),
'#name'=>$i,
  '#ajax' => array(
      'callback' => 'ajax_date_de_pose_add_more_callback',
      'wrapper' => 'date-de-pose-fieldset-wrapper-'.$i,
     ),
);

You just have to add your argument with #name => $i

And get it back into the callback function with :

$i = $form_state['clicked_button']['#name'];


function ajax_date_de_pose_add_more_callback($form, $form_state) {
    $i = $form_state['clicked_button']['#name'];
    return $form['propositions_fieldset'][$i]['dates_de_pose_fieldset'];
}

I hope it helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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