Add a Form to the Block

参考网页https://stackoverflow.com/questions/36643474/how-to-create-a-form-using-block-module-in-drupal-8

个人实现的例子

<?php
/**

  • @file
  • Contains \Drupal\hello_world\Plugin\Block\HelloWorldBlock
    /
    namespace Drupal\hello_world\Plugin\Block;
    use Drupal\Core\Block\BlockBase;
    /
    *
  • Provides a 'Hello' Block.
  • @Block(
  • id = "hello_world_block",
  • admin_label = @Translation("Hello block"),
  • category = @Translation("Hello World"),
  • )
    */

class HelloWorldBlock extends BlockBase {
/**

  • Implements \Drupal\block\BlockBase::blockBuild().
    */
    public function build() {
    $builtForm = \Drupal::formBuilder()->getForm('Drupal\hello_world\Form\FirstForm');
    $renderArray['form'] = $builtForm;
    return $renderArray;
    }
    }

推荐阅读更多精彩内容