public function RobotFormBase::buildForm in Examples for Developers 8
Same name and namespace in other branches
- 3.x modules/config_entity_example/src/Form/RobotFormBase.php \Drupal\config_entity_example\Form\RobotFormBase::buildForm()
Overrides Drupal\Core\Entity\EntityFormController::form().
Builds the entity add/edit form.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: An associative array containing the current state of the form.
Return value
array An associative array containing the robot add/edit form.
Overrides EntityForm::buildForm
File
- config_entity_example/
src/ Form/ RobotFormBase.php, line 78
Class
- RobotFormBase
- Class RobotFormBase.
Namespace
Drupal\config_entity_example\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
// Get anything we need from the base class.
$form = parent::buildForm($form, $form_state);
// Drupal provides the entity to us as a class variable. If this is an
// existing entity, it will be populated with existing values as class
// variables. If this is a new entity, it will be a new object with the
// class of our entity. Drupal knows which class to call from the
// annotation on our Robot class.
$robot = $this->entity;
// Build the form.
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $robot
->label(),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#title' => $this
->t('Machine name'),
'#default_value' => $robot
->id(),
'#machine_name' => [
'exists' => [
$this,
'exists',
],
'replace_pattern' => '([^a-z0-9_]+)|(^custom$)',
'error' => 'The machine-readable name must be unique, and can only contain lowercase letters, numbers, and underscores. Additionally, it can not be the reserved word "custom".',
],
'#disabled' => !$robot
->isNew(),
];
$form['floopy'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Floopy'),
'#default_value' => $robot->floopy,
];
// Return the form.
return $form;
}