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');
$form_state->setRedirect('my-route');
}
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');
$form_state->setRedirect('my-route');
}
Which isn't really elegant but does the job.
Is there a better way to get it ?