简体   繁体   中英

Inserting a form into a block in Drupal?

我可以使用任何命令或方法将表单(例如用户注册表单)的内容插入块中吗?

In Drupal 7, it looks like this:

function yourmodule_block_view($delta='')
{
  switch($delta) {
    case 'your_block_name':
      $block['subject'] = null; // Most forms don't have a subject 
      $block['content'] = drupal_get_form('yourmodule_form_function');
      break;
   }
   return $block;
 }

The form array returned by drupal_get_form will be automatically rendered.

yourmodule_form_function is a function (in your module or an existing Drupal module) that returns the form array;

drupal_get_form($form_id) - put it in a module's hook_block ($op=='view') or even... shudder ... inside a block with PHP filter on.

You need to find the form id first - look for a hidden input with the name form_id within the form. Its value should be the the form id.

Also, you could simply use the Form Block module.

One other thing is that it puts some of the PHP code into the database, instead of in the filesystem where the rest is. It's easy to forget and waste a lot of time searching for the code, and grep is a lot more convenient that going through every block and seeing if the PHP is there.

Drupal 8+ solution

Create the form. Then, to create the block use something like this:

<?php

namespace Drupal\my_module\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\my_module\Form\MyForm;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides the My Block block.
 *
 * @Block(
 *   id = "my_block",
 *   admin_label = @Translation("My Block")
 * )
 */
class MyBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * The form builder.
   *
   * @var \Drupal\Core\Form\FormBuilder
   */
  protected $formBuilder;

  /**
   * Constructs a new MyBlock object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   Our service container.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ContainerInterface $container) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->formBuilder = $container->get('form_builder');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container
    );
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $form = $this->formBuilder->getForm(MyForm::class);
    return $form;

    // // Or return a render array.
    // // in mytheme.html.twig use {{ form }} and {{ data }}.
    // return [
    //   '#theme' => 'mytheme',
    //   "#form" => $form,
    //   "#data" => $data,
    // ];
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM