├── README.md ├── demo_confirm_form.info.yml ├── demo_confirm_form.links.menu.yml ├── demo_confirm_form.routing.yml └── src └── Form ├── ExampleMinimalForm.php └── ExampleForm.php /README.md: -------------------------------------------------------------------------------- 1 | # Demo Confirm Form 2 | 3 | Demonstration for Drupal 9 ```confirmFormBase``` usage in one file. 4 | -------------------------------------------------------------------------------- /demo_confirm_form.info.yml: -------------------------------------------------------------------------------- 1 | name: Demo confirm form 2 | type: module 3 | description: Example for a confirm form and a form submit on the same class. 4 | package: Demo 5 | core: 8.x 6 | core_version_requirement: ^8 || ^9 7 | -------------------------------------------------------------------------------- /demo_confirm_form.links.menu.yml: -------------------------------------------------------------------------------- 1 | demo_confirm_form.settings_form_minimal: 2 | title: Demo confirm form minimal 3 | description: Configure demo confirm form. 4 | parent: system.admin_config_system 5 | route_name: demo_confirm_form.settings_form_minimal 6 | weight: 1 7 | 8 | demo_confirm_form.settings_form: 9 | title: Demo confirm form 10 | description: Configure demo confirm form. 11 | parent: system.admin_config_system 12 | route_name: demo_confirm_form.settings_form 13 | weight: 2 14 | -------------------------------------------------------------------------------- /demo_confirm_form.routing.yml: -------------------------------------------------------------------------------- 1 | demo_confirm_form.settings_form_minimal: 2 | path: '/admin/config/system/demo-form-minimal' 3 | defaults: 4 | _title: 'demo confirm form minimal' 5 | _form: 'Drupal\demo_confirm_form\Form\ExampleMinimalForm' 6 | requirements: 7 | _permission: 'administer demo_confirm_form_minimal configuration' 8 | 9 | demo_confirm_form.settings_form: 10 | path: '/admin/config/system/demo-form' 11 | defaults: 12 | _title: 'demo confirm form' 13 | _form: 'Drupal\demo_confirm_form\Form\ExampleForm' 14 | requirements: 15 | _permission: 'administer demo_confirm_form configuration' 16 | -------------------------------------------------------------------------------- /src/Form/ExampleMinimalForm.php: -------------------------------------------------------------------------------- 1 | setMessenger($messenger); 34 | } 35 | 36 | /** 37 | * {@inheritdoc} 38 | */ 39 | public static function create(ContainerInterface $container): ExampleMinimalForm { 40 | return new static( 41 | $container->get('messenger') 42 | ); 43 | } 44 | 45 | /** 46 | * {@inheritdoc} 47 | */ 48 | public function getFormId(): string { 49 | return 'example_confirm_form'; 50 | } 51 | 52 | /** 53 | * {@inheritdoc} 54 | */ 55 | public function getQuestion(): TranslatableMarkup { 56 | return $this->t('Are you sure you want to %action?', ['%action' => $this->action]); 57 | } 58 | 59 | /** 60 | * {@inheritdoc} 61 | */ 62 | public function getCancelUrl(): Url { 63 | return new Url('demo_confirm_form.settings_form'); 64 | } 65 | 66 | /** 67 | * {@inheritdoc} 68 | */ 69 | public function buildForm(array $form, FormStateInterface $form_state): array { 70 | // When this is the confirmation step fall through to the confirmation form. 71 | if ($this->action) { 72 | return parent::buildForm($form, $form_state); 73 | } 74 | 75 | $form['action'] = [ 76 | '#type' => 'radios', 77 | '#title' => $this->t('Choose action'), 78 | '#options' => [ 79 | 'delete' => $this->t('Delete'), 80 | 'import' => $this->t('Import'), 81 | ], 82 | '#required' => TRUE, 83 | ]; 84 | $form['actions'] = ['#type' => 'actions']; 85 | $form['actions']['submit'] = [ 86 | '#type' => 'submit', 87 | '#value' => $this->t('Run selected action'), 88 | '#button_type' => 'primary', 89 | ]; 90 | return $form; 91 | } 92 | 93 | /** 94 | * {@inheritdoc} 95 | */ 96 | public function validateForm(array &$form, FormStateInterface $form_state): void { 97 | // The confirmation step needs no additional validation. 98 | if ($this->action) { 99 | return; 100 | } 101 | 102 | parent::validateForm($form, $form_state); 103 | } 104 | 105 | /** 106 | * {@inheritdoc} 107 | */ 108 | public function submitForm(array &$form, FormStateInterface $form_state): void { 109 | // If this form has not yet been confirmed, store the values and rebuild. 110 | if (!$this->action) { 111 | $form_state->setRebuild(); 112 | $this->action = $form_state->getValue('action'); 113 | return; 114 | } 115 | 116 | $method = $this->action . 'Data'; 117 | $this->{$method}(); 118 | } 119 | 120 | /** 121 | * Example action. 122 | */ 123 | protected function deleteData(): void { 124 | $this->messenger->addMessage($this->t('Action %action done!', ['%action' => $this->action])); 125 | } 126 | 127 | /** 128 | * Example action. 129 | */ 130 | protected function importData(): void { 131 | $this->messenger->addMessage($this->t('Action %action done!', ['%action' => $this->action])); 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /src/Form/ExampleForm.php: -------------------------------------------------------------------------------- 1 | setMessenger($messenger); 43 | } 44 | 45 | /** 46 | * {@inheritdoc} 47 | */ 48 | public static function create(ContainerInterface $container): ExampleForm { 49 | return new static( 50 | $container->get('messenger') 51 | ); 52 | } 53 | 54 | /** 55 | * {@inheritdoc} 56 | */ 57 | public function getFormId(): string { 58 | return 'example_confirm_form'; 59 | } 60 | 61 | /** 62 | * {@inheritdoc} 63 | */ 64 | public function getFormName(): string { 65 | return 'example_confirm'; 66 | } 67 | 68 | /** 69 | * {@inheritdoc} 70 | */ 71 | public function getDescription(): TranslatableMarkup { 72 | $args = [ 73 | '%data' => $this->data, 74 | '%action' => $this->action, 75 | ]; 76 | return $this->t('This action of %action on %data is definitive.', $args); 77 | } 78 | 79 | /** 80 | * {@inheritdoc} 81 | */ 82 | public function getConfirmText(): TranslatableMarkup { 83 | $args = [ 84 | '%data' => $this->data, 85 | '%action' => $this->action, 86 | ]; 87 | return $this->t('Run %action on %data', $args); 88 | } 89 | 90 | /** 91 | * {@inheritdoc} 92 | */ 93 | public function getQuestion(): TranslatableMarkup { 94 | $args = [ 95 | '%data' => $this->data, 96 | '%action' => $this->action, 97 | ]; 98 | return $this->t('Are you sure you want to %action on %data?', $args); 99 | } 100 | 101 | /** 102 | * {@inheritdoc} 103 | */ 104 | public function getCancelUrl(): Url { 105 | return new Url('demo_confirm_form.settings_form'); 106 | } 107 | 108 | /** 109 | * {@inheritdoc} 110 | */ 111 | public function buildForm(array $form, FormStateInterface $form_state): array { 112 | // When this is the confirmation step fall through to the confirmation form. 113 | if ($this->action) { 114 | return parent::buildForm($form, $form_state); 115 | } 116 | 117 | $form['data'] = [ 118 | '#type' => 'textfield', 119 | '#title' => $this->t('Data to process'), 120 | '#required' => TRUE, 121 | ]; 122 | $form['action'] = [ 123 | '#type' => 'radios', 124 | '#title' => $this->t('Choose action'), 125 | '#options' => [ 126 | 'delete' => $this->t('Delete'), 127 | 'import' => $this->t('Import'), 128 | 'invalid' => $this->t('Invalid action'), 129 | ], 130 | '#required' => TRUE, 131 | ]; 132 | $form['actions'] = ['#type' => 'actions']; 133 | $form['actions']['submit'] = [ 134 | '#type' => 'submit', 135 | '#value' => $this->t('Run selected action'), 136 | '#button_type' => 'primary', 137 | ]; 138 | return $form; 139 | } 140 | 141 | /** 142 | * {@inheritdoc} 143 | */ 144 | public function validateForm(array &$form, FormStateInterface $form_state): void { 145 | // The confirmation step needs no additional validation. 146 | if ($this->action) { 147 | return; 148 | } 149 | 150 | if ($form_state->getValue('data') === 'invalid') { 151 | $form_state->setErrorByName('data', $this->t('The value is not correct.')); 152 | } 153 | 154 | $action = $form_state->getValue('action'); 155 | $method = $action . 'Data'; 156 | if (!method_exists($this, $method)) { 157 | $form_state->setErrorByName('action', $this->t('Action %action not yet implemented!', ['%action' => $action]), 'error'); 158 | return; 159 | } 160 | parent::validateForm($form, $form_state); 161 | } 162 | 163 | /** 164 | * {@inheritdoc} 165 | */ 166 | public function submitForm(array &$form, FormStateInterface $form_state): void { 167 | // If this form has not yet been confirmed, store the values and rebuild. 168 | if (!$this->action) { 169 | $form_state->setRebuild(); 170 | $this->action = $form_state->getValue('action'); 171 | $this->data = $form_state->getValue('data'); 172 | return; 173 | } 174 | 175 | $method = $this->action . 'Data'; 176 | if (!method_exists($this, $method)) { 177 | $this->messenger->addMessage($this->t('Action %action not yet implemented!', ['%action' => $this->action]), 'error'); 178 | return; 179 | } 180 | $this->{$method}(); 181 | } 182 | 183 | /** 184 | * Example action. 185 | */ 186 | protected function deleteData(): void { 187 | $this->messenger->addMessage($this->t('Deleted %data!', ['%data' => $this->data])); 188 | } 189 | 190 | /** 191 | * Example action. 192 | */ 193 | protected function importData(): void { 194 | $this->messenger->addMessage($this->t('Imported %data!', ['%data' => $this->data])); 195 | } 196 | } 197 | --------------------------------------------------------------------------------