Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/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,我想在Drupal8中使用块模块构建表单。我知道在Drupal7中构建表单,但在Drupal8中似乎有所不同 请曾经使用drupal8自定义表单作为块的任何人帮助我。您的问题非常模糊,因为我不知道您对drupal8中的模块、表单和块了解多少。所以这里有一个小指南,关于如何做的细节的进一步信息对于这个答案来说是多余的 1。创建新模块并启用它 看这里: 基本上,您可以创建模块文件夹和模块信息yml文件,让Drupal了解该模块。然后使用drush或Drupal中的管理区域启用它 2。创建表单 看这里

我想在
Drupal8
中使用块模块构建表单。我知道在
Drupal
7中构建表单,但在Drupal8中似乎有所不同


请曾经使用drupal8自定义表单作为块的任何人帮助我。

您的问题非常模糊,因为我不知道您对drupal8中的模块、表单和块了解多少。所以这里有一个小指南,关于如何做的细节的进一步信息对于这个答案来说是多余的

1。创建新模块并启用它

看这里:

基本上,您可以创建模块文件夹和模块信息yml文件,让Drupal了解该模块。然后使用drush或Drupal中的管理区域启用它

2。创建表单

看这里:

模块/src/Form下创建表单。更多详情请参见上面的链接

3。创建块并渲染表单

看这里:

your_module/src/Plugin/Block/
下,创建将呈现表单的块

这个想法基本上是(代码根据Henrik的建议进行了更新):

注意:您不需要使用
$renderArray
包装
$builtForm
,只需返回
$builtForm
即可。我个人喜欢这样做,因为我经常需要向最终渲染数组添加其他内容,如一些标记、缓存设置或库等

4。放置块


将块放置在所需区域中。完成。

以下是如何完成此操作的详细摘要:-

按照上述指南,您将把完成的表单添加到块构建函数中,例如

class DemoBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {    
    $form = \Drupal::formBuilder()->getForm('Drupal\demo\Form\DemoForm');
    return $form;
  }

}
如果您是Drupal 8新手或需要掌握更多知识,请参阅一些更有用的文档:


要使用块模块构建表单,您可以轻松地使用添加表单并显示为块的位置


如果要在自定义块中以编程方式创建表单,可以通过创建如下所示的两个文件来实现:

表单文件(
src/Form/DemoForm.php
):

来源:



要向块配置中添加表单,请参阅:。

HI Frank,感谢您的重播。HI Frank,感谢您的回复。我实际上是想在一个块中实现一个表单。我只是发布了表示相同的URL。。。请仔细检查并建议我如何在主页上创建航班预订表单。我已经做了,请看我的回答:)我不确定您对我的期望。我当然不会口述每一行代码,因为这不是StackOverflow的目的。如果您对表单等完全不了解,只需在谷歌上搜索教程或其他内容,就可以使用以下工具直接构建表单:
builtForm=\Drupal::formBuilder()->getForm('Drupal\yourmodule\form\YourForm')–不管怎样,回答得不错。@FrankDrebin有没有办法订购退回的物品?我返回一个表单和一些标记,但是表单总是在标记之后结束,我希望表单位于标记之上。
class DemoBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {    
    $form = \Drupal::formBuilder()->getForm('Drupal\demo\Form\DemoForm');
    return $form;
  }

}
<?php

/**
 * @file
 * Contains \Drupal\demo\Form\DemoForm.
 */

namespace Drupal\demo\Form;

use Drupal\Core\Form\FormBase;

class DemoForm extends FormBase {

  /**
   * {@inheritdoc}.
   */
  public function getFormId() {
    return 'demo_form';
  }

  /**
   * {@inheritdoc}.
   */
  public function buildForm(array $form, array &$form_state) {

    $form['email'] = array(
      '#type' => 'email',
      '#title' => $this->t('Your .com email address.')
    );
    $form['show'] = array(
      '#type' => 'submit',
      '#value' => $this->t('Submit'),
    );

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, array &$form_state) {
    $values = $form_state->getValues();
    if (strpos($values['email'], '.com') === FALSE ) {
      $form_state->setErrorByName('email', t('This is not a .com email address.'));
    } 
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, array &$form_state) {
    drupal_set_message($this->t('Your email address is @email', array('@email' => $form_state['values']['email'])));
  }

}
<?php

namespace Drupal\mymodule\Plugin\Block;

use Drupal\Core\Block\BlockBase;

/**
 * Provides a 'Hello' Block.
 *
 * @Block(
 *   id = "form_block",
 *   admin_label = @Translation("My form"),
 *   category = @Translation("My Category"),
 * )
 */
class HelloBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {
    $form = \Drupal::formBuilder()->getForm('\Drupal\mymodule\Form\HelloBlock');
    //$form['#attached']['js'][] = drupal_get_path('module', 'example') .  '/js/example.js';
    //$form['#markup'] = $this->t('Custom text');
    return $form;
  }

}