7

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.

6

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'];
}
  • 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 '17 at 18:28
2

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.

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;
}

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.