Drupal - How to add extra arguments/parameters to ajax callback function?
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'];
}
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.