Simplest.php in Examples for Developers 3.x
File
modules/ajax_example/src/Form/Simplest.php
View source
<?php
namespace Drupal\ajax_example\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class Simplest extends FormBase {
public function getFormId() {
return 'ajax_example_simplest';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['changethis'] = [
'#title' => $this
->t("Choose something and explain why"),
'#type' => 'select',
'#options' => [
'one' => 'one',
'two' => 'two',
'three' => 'three',
],
'#ajax' => [
'callback' => '::promptCallback',
'wrapper' => 'replace-textfield-container',
],
];
$form['replace_textfield_container'] = [
'#type' => 'container',
'#attributes' => [
'id' => 'replace-textfield-container',
],
];
$form['replace_textfield_container']['replace_textfield'] = [
'#type' => 'textfield',
'#title' => $this
->t("Why"),
];
$value = $form_state
->getValue('changethis');
if ($value !== NULL) {
$form['replace_textfield_container']['replace_textfield']['#description'] = $this
->t("Say why you chose '@value'", [
'@value' => $value,
]);
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
public function promptCallback($form, FormStateInterface $form_state) {
return $form['replace_textfield_container'];
}
}
Classes
Name |
Description |
Simplest |
A relatively simple AJAX demonstration form. |