By smiletrl on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.x
Issue links:
Description:
Motivation:
Forms often need boilerplate code, such as the translation manager or an empty validateForm() method.
For example
abstract class ConfirmFormBase implements ConfirmFormInterface {
// So other code.
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
}
}
Add FormBase to avoid this boilerplate code, now abstract class ConfirmFormBase becomes:
abstract class ConfirmFormBase extends FormBase implements ConfirmFormInterface {
}
No need to call validateForm again.
Oher API changes associated with form controllers:
API additions
\Drupal\Core\Form\FormBaseis added, containingprotected function t()as a helper method\Drupal\Core\Entity\EntityFormControllerInterfacehas apublic function setTranslationManager(), which is called automatically to prevent constructor changesprotected function getCurrentUser()to get the current user.protected function public function l($text, $route_name, array $parameters = array(), array $options = array())(), which allows to provide a link using route names and parameters.public function redirect($route_name, array $route_parameters = array(), $status = 302);, which allows to redirect to a specified route.public function url($route_name, array $route_parameters = array(), array $options = array());, which allows to create an url using specified route name/parameters.
API changes
- While in route based forms, the request can still be retrieved in buildForm(), it is unreliable.
$this->getRequest()should be used - Entity forms (
EntityFormControllerInterface) can no longer useEntityControllerInterface, they should be like all other forms and useContainerInjectionInterface \Drupal\image\Form\ImageStyleFormBasehad its$this->translatorproperty changed to$this->translationManagerto match the rest of core
Impacts:
Site builders, administrators, editors
Module developers