Same filename and directory in other branches
- 8.9.x core/modules/system/tests/modules/form_test/src/FormTestControllerObject.php
- 9 core/modules/system/tests/modules/form_test/src/FormTestControllerObject.php
Namespace
Drupal\form_test
File
core/modules/system/tests/modules/form_test/src/FormTestControllerObject.php
View source
<?php
namespace Drupal\form_test;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class FormTestControllerObject extends ConfigFormBase {
public function getFormId() {
return 'form_test_form_test_controller_object';
}
protected function getEditableConfigNames() {
return [
'form_test.object',
];
}
public static function create(ContainerInterface $container) {
\Drupal::messenger()
->addStatus(t('The FormTestControllerObject::create() method was used for this form.'));
return new static($container
->get('config.factory'), $container
->get('config.typed'));
}
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL, $custom_attributes = NULL) {
$form['element'] = [
'#markup' => 'The FormTestControllerObject::buildForm() method was used for this form.',
];
$form['custom_attribute']['#markup'] = $custom_attributes;
$form['request_attribute']['#markup'] = $request->attributes
->get('request_attribute');
$form['bananas'] = [
'#type' => 'textfield',
'#title' => $this
->t('Bananas'),
];
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$this
->messenger()
->addStatus($this
->t('The FormTestControllerObject::validateForm() method was used for this form.'));
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this
->messenger()
->addStatus($this
->t('The FormTestControllerObject::submitForm() method was used for this form.'));
$this
->config('form_test.object')
->set('bananas', $form_state
->getValue('bananas'))
->save();
}
}
Classes