How to create a form using block module in drupal 8?

前端 未结 3 1425
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-03 14:40

I want build a form using a block module in Drupal 8. I am aware of building the forms in Drupal 7 but the same seems to be different in Drupal 8.

3条回答
  •  不要未来只要你来
    2021-02-03 15:23

    Here is a detailed summary of how to go about this:-

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

    Following the above guide, you would add the completed form to the block build function, e.g.

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

    Some more useful docs if you are new to Drupal 8 or need to dust off your knowledge:

    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

提交回复
热议问题