I have a form with a submit button :

$form['my-custom-submit'.$i] = array(
    '#name' => $nid,
    '#type' => 'submit',
    '#value' => 'Delete Node',
    '#submit' => array('::deleteNode'),

This submit button calls this function :

public function deleteNode(array &$form, FormStateInterface $form_state, $nid)
    // My Delete function($nid)
    drupal_set_message('Delete : '.$nid, 'status');

How can I pass the $nid parameter to my deleteNode function ?
Or if there is a way to get the '#name' in my function, it could be perfect too.
I cannot pass it through a $form_state->getValue('abc') because it is a list of incremented ids : $form['my-custom-submit'.$i].

I have searched google for 4 hours without finding, but I'm pretty sure that there is a simple way to accomplish this (newbie).

Thanks in advance.

Edit : I found that the '#name' and value of the submit are passed to my function, I can get it like this :

public function deleteNode(array &$form, FormStateInterface $form_state)
    $last_nid = '';

    foreach ($form_state->getValues() as $key => $value) {
      $last_nid = $key;
    // My Delete function($last_nid)
    drupal_set_message('Delete : '.$last_nid, 'status');

Which isn't really elegant but does the job.
Is there a better way to get it ?

2 Answers 2


You can try this. Set the node id in the form element

    '#nid' => $nid,

and use FormState::getTriggeringElement to get the triggering element and the property #nid:

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

    $trigger = $form_state->getTriggeringElement();
    $nid = $trigger['#nid'];

In some cases you have to get the triggering button from the parent property:

    $clicked_button = end($form_state->getTriggeringElement()['#parents']);
  • A little late but thank you very much. It is exactly what I wanted.
    – heyow
    Jun 28, 2017 at 7:59

The form state contains which node was submitted using the $form_state->getFormObject()

$nid = $form_state->getFormObject()->getEntity()->id();

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.