【问题标题】:How to create a form using block module in drupal 8?如何在 drupal 8 中使用块模块创建表单?
【发布时间】:2016-08-07 05:02:05
【问题描述】:

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

请求任何参与过 drupal8 自定义表单的人帮助我。

【问题讨论】:

    标签: drupal drupal-8


    【解决方案1】:

    要使用块模块构建表单,您可以轻松使用Webform module,您可以在其中添加表单并显示为块。


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

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

    <?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'])));
      }
    
    }
    

    来源:Building a Drupal 8 Module: Blocks and Forms.

    块文件(src/Plugin/Block/HelloBlock.php):

    <?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;
      }
    
    }
    

    来源:Create a custom block.


    要将表单添加到块配置,请参阅:Add a Form to the Block Configuration

    【讨论】:

      【解决方案2】:

      您的问题非常模糊,因为我不知道您已经对 Drupal 8 中的模块、表单和块了解多少。所以这里有一个小指南如何做,更多关于如何做的详细信息会这个答案有点矫枉过正。

      1.创建一个新模块并启用它

      看这里:Naming and placing your Drupal 8 module

      基本上你创建模块文件夹和模块信息 yml 文件让 Drupal 知道模块。然后使用 drush 或 Drupal 中的管理区域启用它。

      2。创建表单

      看这里:Introduction to Form API

      your_module/src/Form 下创建表单。上面的链接中的更多详细信息。

      3。创建块并渲染表单

      看这里:Create a custom block

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

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

      $builtForm = \Drupal::formBuilder()->getForm('Drupal\your_module\Form\Your‌​Form');
      $renderArray['form'] = $builtForm;
      
      return $renderArray;
      

      注意:您不需要用$renderArray 包装$builtForm,您可以只返回$builtForm 就可以了。我个人只是喜欢这样做,因为我经常需要在最终的渲染数组中添加一些其他东西,比如一些标记、缓存设置或库等。

      4.放置方块

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

      【讨论】:

      • 嗨弗兰克,感谢您的重播。
      • 嗨弗兰克,感谢您的回复。我实际上是在尝试在一个块中实现一个表单。我只是发布代表相同的 URL。vliegtarieven.nl ...请通过并建议我在主页中构建航班预订表单的方法。
      • 我已经做到了,看看我的回答 :) 我不确定您对我的期望。我当然不会规定每一行代码,因为这不是 StackOverflow 的意义所在。如果您完全不了解表单等,只需谷歌搜索教程或其他东西,有很多。
      • 您可以直接使用:$builtForm = \Drupal::formBuilder()-&gt;getForm('Drupal\yourmodule\Form\YourForm'); 构建表单 - 无论如何,很好的答案。
      • @FrankDrebin 有没有办法订购退回的物品?我要返回一个表单和一些标记,但表单总是在标记之后结束,我想将表单放在标记之上。
      【解决方案3】:

      以下是有关如何进行此操作的详细摘要:-

      https://www.sitepoint.com/building-drupal-8-module-blocks-forms/

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

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

      如果您是 Drupal 8 的新手或需要重新学习知识,可以参考一些更有用的文档:

      https://www.drupal.org/docs/8/creating-custom-modules

      https://www.drupal.org/docs/8/api/block-api

      https://www.drupal.org/docs/8/api/form-api

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-02
        • 1970-01-01
        • 2017-05-22
        • 2019-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多