ContainerDemo.php in Examples for Developers 3.x
File
modules/form_api_example/src/Form/ContainerDemo.php
View source
<?php
namespace Drupal\form_api_example\Form;
use Drupal\Core\Form\FormStateInterface;
class ContainerDemo extends DemoBase {
public function buildForm(array $form, FormStateInterface $form_state) {
$form['description'] = [
'#type' => 'item',
'#markup' => $this
->t('This form example demonstrates container elements: details, fieldset and container.'),
];
$form['author'] = [
'#type' => 'details',
'#title' => 'Author Info (type = details)',
];
$form['author']['name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Name'),
];
$form['author']['pen_name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Pen Name'),
];
$form['book'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Book Info (type = fieldset)'),
];
$form['book']['title'] = [
'#type' => 'textfield',
'#title' => $this
->t('Title'),
];
$form['book']['publisher'] = [
'#type' => 'textfield',
'#title' => $this
->t('Publisher'),
];
$form['accommodation'] = [
'#type' => 'container',
];
$form['accommodation']['title'] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $this
->t('Special Accommodations (type = container)'),
];
$form['accommodation']['diet'] = [
'#type' => 'textfield',
'#title' => $this
->t('Dietary Restrictions'),
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
];
return $form;
}
public function getFormId() {
return 'form_api_example_container_demo';
}
}