Inserting a form into a block in Drupal?
Asked Answered
H

3

19

Is there any command or method that I can use to insert the contents of a form (e.g. the user registration form) into a block?

Hepner answered 20/9, 2009 at 12:43 Comment(0)
W
15

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.

Williamson answered 20/9, 2009 at 14:10 Comment(3)
Basically, PHP filter is a bad idea. From Drupal's handbook page on the subject: "... it is a significant and dangerous security risk in the hands of a malicious user. Even a trusted user may accidentally compromise the site by entering malformed or incorrect PHP code". You're almost always better off writing a module (just a couple of lines of overhead, really).Williamson
One anecdote, I had a minor syntax error in a node with the PHP filter. It broke search indexing, which broke cron. It's not worth the debugging complexity.Dialectical
I agree with Grayside. With my very first Drupal site I had php redirects in some nodes, which completely broke the search indexing module. So in that sense it is bad.Hepner
H
26

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;

Hardy answered 11/3, 2011 at 10:2 Comment(1)
remember you need to implement hook_block_info()Bechance
W
15

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.

Williamson answered 20/9, 2009 at 14:10 Comment(3)
Basically, PHP filter is a bad idea. From Drupal's handbook page on the subject: "... it is a significant and dangerous security risk in the hands of a malicious user. Even a trusted user may accidentally compromise the site by entering malformed or incorrect PHP code". You're almost always better off writing a module (just a couple of lines of overhead, really).Williamson
One anecdote, I had a minor syntax error in a node with the PHP filter. It broke search indexing, which broke cron. It's not worth the debugging complexity.Dialectical
I agree with Grayside. With my very first Drupal site I had php redirects in some nodes, which completely broke the search indexing module. So in that sense it is bad.Hepner
B
0

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,
    // ];
  }

}
Bathsheeb answered 28/6, 2022 at 16:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.