- Drupal 8 Module Development
- Daniel Sipos
- 197字
- 2021-07-02 12:22:40
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.
- WildFly:New Features
- Android和PHP開發最佳實踐(第2版)
- DevOps for Networking
- UML+OOPC嵌入式C語言開發精講
- TypeScript圖形渲染實戰:基于WebGL的3D架構與實現
- Learning Apache Kafka(Second Edition)
- Hands-On Natural Language Processing with Python
- SQL Server 2016數據庫應用與開發
- 軟件測試技術指南
- C語言程序設計
- 智能搜索和推薦系統:原理、算法與應用
- Swift 4從零到精通iOS開發
- Swift語言實戰晉級
- 多媒體技術及應用
- Java自然語言處理(原書第2版)