Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在drupal8中的细枝模板中渲染表单_Drupal_Drupal 8 - Fatal编程技术网

在drupal8中的细枝模板中渲染表单

在drupal8中的细枝模板中渲染表单,drupal,drupal-8,Drupal,Drupal 8,我现在正在使用drupal 8,我已经使用form api创建了表单 下面是模块目录下的代码 // module/src/Form/ContributeForm class ContributeForm extends FormBase { public function getFormId() { return 'amazing_forms_contribute_form'; } public function buildForm(array $form,

我现在正在使用drupal 8,我已经使用form api创建了表单

下面是模块目录下的代码

  // module/src/Form/ContributeForm

  class ContributeForm extends FormBase {

  public function getFormId() {
     return 'amazing_forms_contribute_form';
  }

  public function buildForm(array $form, FormStateInterface $form_state) 
  {
      $form['title'] = array(
           '#type' => 'textfield',
           '#title' => t('Title'),
           '#required' => TRUE,
      );
      $form['video'] = array(
           '#type' => 'textfield',
           '#title' => t('Youtube video'),
      );
      $form['video'] = array(
          '#type' => 'textfield',
          '#title' => t('Youtube video'),
      );
     $form['develop'] = array(
          '#type' => 'checkbox',
          '#title' => t('I would like to be involved in developing this 
           material'),
    );
    $form['submit'] = array(
          '#type' => 'submit',
          '#value' => t('Submit'),
);
return $form;
}

public function validateForm(array &$form, FormStateInterface $form_state) {
}

public function submitForm(array &$form, FormStateInterface $form_state) {
}
}
现在我需要在细枝模板中呈现上面的虚幻,如下所示

 //themes/page.html.twig

 <body>
    {{form.title}}
    {{form.video}}
    {{form.video}}
 </body>
//主题/page.html.twig
{{form.title}
{{form.video}
{{form.video}

细枝将位于主题文件夹下。是否可以在page.html.twig文件中获取变量???

通过细枝呈现和自定义表单的最佳方法是,在表单中使用
$form['#theme']
值。示例如下:

模块名称/scr/Form/your\u Form.php

public function buildForm(array $form, FormStateInterface $form_state){
       .....
       .....

       $form['#theme'] = 'your_form_theme';

       return $form;
}
模块名称/形式。模块

function form_theme() {

    $themes['your_form_theme'] = ['render element' => 'form'];

    return $themes;
}
剩下的就是创建自定义细枝并插入表单字段

module\u name/templates/your form theme.html.twig

<body>
    {{form.field_1}}
    {{form.field_2}}
    {{form.field_3}}
 </body>

{{form.field_1}}
{{form.field_2}}
{{form.field_3}}

希望它能帮助你

您可以使用控制器创建页面。从中,您可以得到表单元素


在src/Controller文件夹内创建一个module_nameController.php文件。在该文件中创建一个应扩展到Controllerbase的类。在该文件中,通过使用form builder函数,您可以获得表单元素。

CSRF令牌呢?{{form.form\u build\u id}}{{form.form\u token}}{{form.form\u id}{{form.actions}