官术网_书友最值得收藏!

Rendering forms

Staying on forms for just a bit longer, let's quickly learn how to render forms programmatically. Also, we have already seen how to map a form to a route definition so that the page being built contains the form when accessing the route path. However, there are times where we need to render a form programmatically, either inside a Controller or a block or wherever you want. We can do this using the FormBuilder service.

The form builder can be injected using the form_builder service key or used statically via the shorthand:

$builder = \Drupal::formBuilder();

Once we have it, we can build a form, like so:

$form = $builder->getForm('Drupal\hello_world\Form\SalutationConfigurationForm');

In the preceding code, $form will be a render array of the form that we can return, for example, inside a Controller. We'll talk more about render arrays a bit later on, and you'll understand how they get turned into actual form markup. However, for now, this is all you need to know about rendering forms programmatically--you get the form builder and request from it the form using the fully qualified name of the form class.

With this, we can close the parenthesis on forms.

主站蜘蛛池模板: 恭城| 富裕县| 鸡泽县| 渭南市| 老河口市| 丽水市| 桑日县| 威海市| 太仆寺旗| 广平县| 苗栗市| 仪征市| 林甸县| 宜君县| 陵水| 错那县| 沽源县| 肇源县| 广河县| 望城县| 太原市| 博爱县| 依兰县| 邢台市| 杨浦区| 金堂县| 浮山县| 普定县| 保康县| 涿鹿县| 琼海市| 青神县| 阿勒泰市| 石景山区| 鹤庆县| 东丽区| 尼勒克县| 河津市| 新龙县| 南靖县| 大庆市|