- Drupal 8 Module Development
- Daniel Sipos
- 197字
- 2021-07-02 15:45:16
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.
- Android Jetpack開發:原理解析與應用實戰
- R語言經典實例(原書第2版)
- 深入理解Java7:核心技術與最佳實踐
- Easy Web Development with WaveMaker
- C語言程序設計
- Building Minecraft Server Modifications
- iOS自動化測試實戰:基于Appium、Python與Pytest
- 零基礎學C語言(第4版)
- HTML5+CSS+JavaScript深入學習實錄
- Hands-On Artificial Intelligence with Unreal Engine
- 匯編語言程序設計
- WCF 4.5 Multi-Layer Services Development with Entity Framework(Third Edition)
- ROS Robotics Projects
- 破繭成蝶:用戶體驗設計師的成長之路
- Elixir Cookbook