Skip to content

Instantly share code, notes, and snippets.

@Dropa
Created November 13, 2017 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dropa/160de5154e3e3358378c9e72d080c18a to your computer and use it in GitHub Desktop.
Save Dropa/160de5154e3e3358378c9e72d080c18a to your computer and use it in GitHub Desktop.
proper way to do different submits
<?php
namespace Drupal\general\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* List tests arranged in groups that can be selected and run.
*/
class TestForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'testform';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = [
'sub' => [
'#type' => 'container',
'submit' => [
'#type' => 'submit',
'#value' => 'Sub me too!',
'#name' => '2',
'#submit' => ['::dumptwo'],
'#weight' => 2,
'#parents' => [2],
],
],
'submit' => [
'#type' => 'submit',
'#value' => 'Sub me!',
'#name' => '1',
'#submit' => ['::dumpone'],
'#weight' => 1,
'#parents' => [1],
],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function dumpone(array &$form, FormStateInterface $form_state) {
dump(1);
}
/**
* {@inheritdoc}
*/
public function dumptwo(array &$form, FormStateInterface $form_state) {
dump(2);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// meh.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment