9

I want to get triggering_element element on form_alter after form rebuild, Also I know that this could be used on submit handler on Ajax controller to findout which element cause form submit or Ajax triggered.

So how can I get triggering_element after Ajax call or form submissions?

2
  • 4
    There's a method to get it in the form state object (probably getTriggeringElement).
    – Eyal
    Commented Dec 18, 2016 at 23:22
  • yes,after a look on FormState.php I findout how to get it, some question maybe could help Drupalists at future, if you google "How to get Trigggerdelement on drupal 8 " you didn't find simple solution,
    – Yuseferi
    Commented Dec 18, 2016 at 23:36

4 Answers 4

12

Eyal is correct. FormState has a method getTriggeringElement. See the original change record for details.

$form_state->getTriggeringElement();

and if you want to get id try

$triggerdElement = $form_state->getTriggeringElement();
$htmlIdofTriggeredElement = $triggerdElement['#id'];
1
  • 7
    Submit buttons often return 'op' and you can't tell which button it is. Then you have to get the id from the parent: $clicked_button = end($form_state->getTriggeringElement()['#parents']);
    – 4uk4
    Commented Dec 19, 2016 at 7:39
5

My preferred method is:

$triggering_element = $form_state->getTriggeringElement();
$button_name = $triggering_element['#name'];
if ($button_name === 'button1') {
  // ...
}

Works both for validateForm() and submitForm().

Ensure you have #name for all your buttons. E.g.:

$form['button1'] = [
  '#type' => 'submit',
  '#name' => 'button1',
  '#value' => $this->t('Button 1'),
];

Compared to $triggering_element['#id'] it's more stable because #id auto-generated and can change implicitly.

Compared to $triggering_element['#value'] it's better because:
- #value can be not unique;
- #value is a part UI (button label) and frequently changes.

1
  • 1
    This is definitely a better method. You should always set a unique '#name' for your button. If you don't, and you use the same '#value' for more than one button, only the last button with that '#value' will have it's array returned in $form_state['triggering_element'] Commented Sep 14, 2018 at 20:24
1

3

$triggering_element = $form_state->getTriggeringElement();
$button_name = $triggering_element['#name'];
if ($button_name === 'button1') {
  // ...
}

Works both for validateForm() and submitForm().

Ensure you have #name for all your buttons. E.g.:

$form['button1'] = [
  '#type' => 'submit',
  '#name' => 'button1',
  '#value' => $this->t('Button 1'),
];

This is the correct answer.

0

Using jQuery you can get the element using ajaxComplete function

$( document ).on( "ajaxComplete", function( event, request, settings ) {
    if(settings.extraData != undefined) {
    console.log(settings.extraData._triggering_element_name);
    }
});

Your Answer

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

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