Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By dawehner on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.x
Issue links:
Description:
In Drupal 7 forms have been basically used with drupal_get_form()
.
In Drupal 8 all the functions like drupal_prepare_form()
etc. are replaced with a form builder service.
Drupal 7
$parameter = 'new';
$form = drupal_get_form('comment_admin_overview', $parameter);
return $form;
Drupal 8
$parameter = 'new';
$form = \Drupal::formBuilder()->getForm('Drupal\comment\Form\CommentAdminOverview', $parameter);
Before | After |
---|---|
drupal_get_form() |
\Drupal::formBuilder()->getForm() |
drupal_build_form() |
\Drupal::formBuilder()->buildForm() |
form_state_defaults() |
\Drupal::formBuilder()->getFormStateDefaults() |
drupal_rebuild_form() |
\Drupal::formBuilder()->rebuildForm() |
form_get_cache() |
\Drupal::formBuilder()->getCache() |
form_set_cache() |
\Drupal::formBuilder()->setCache() |
drupal_validate_form() |
\Drupal::service('form_validator')->validateForm() |
drupal_form_submit() |
\Drupal::formBuilder()->submitForm() |
drupal_retrieve_form() |
\Drupal::formBuilder()->retrieveForm() |
drupal_process_form() |
\Drupal::formBuilder()->processForm() |
drupal_prepare_form() |
\Drupal::formBuilder()->prepareForm() |
drupal_redirect_form() |
\Drupal::formBuilder()->redirectForm() |
form_execute_handlers($type) , $type either 'submit' or 'validate' |
Either \Drupal::formBuilder()->executeSubmitHandlers() or \Drupal::service('form_validator')->executeValidateHandlers() |
form_set_error() |
$form_state->setErrorByName() |
form_clear_error() |
$form_state->clearErrors() |
form_get_errors() |
$form_state->getErrors() |
form_get_error() |
$form_state->getError() |
form_error() |
$form_state->setError() |
form_set_value() |
$form_state->setValueForElement() |
form_load_include($form_state, $type, $module, $name = NULL) |
$form_state->loadInclude($module, $type, $name = NULL) |
form_state_values_clean(&$form_state) |
$form_state->cleanValues() |
Impacts:
Module developers
Comments
Uses class instead of form id.
The getForm()/buildForm() methods now only accept the name of a class implementing
\Drupal\Core\FormInterface
as a string, or an actual instance of such a class, instead of the form id. (The id is still used for form alter hooks etc.)