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

Rendering forms

Staying on forms for just a bit longer, let's quickly learn how to render forms programmatically. 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 when 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.

主站蜘蛛池模板: 炎陵县| 公主岭市| 盐津县| 莱芜市| 怀化市| 高州市| 辰溪县| 绥芬河市| 阳城县| 荣昌县| 富源县| 丹阳市| 苗栗县| 广水市| 漳平市| 盘山县| 惠州市| 抚顺县| 乐都县| 吉安市| 新宁县| 白城市| 光山县| 周至县| 沈阳市| 宁化县| 永嘉县| 康平县| 芜湖县| 南乐县| 兴安县| 平武县| 兴义市| 曲沃县| 清流县| 常州市| 浦东新区| 南漳县| 项城市| 铁力市| 旌德县|