Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting a form into a block in Drupal?

Tags:

forms

drupal

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?

like image 956
coderama Avatar asked Sep 20 '09 12:09

coderama


People also ask

How do I add a view to a block in Drupal?

Navigate to admin/structure/views. Click Add New View. This brings you to a Views creation wizard to ensure you have the correct basic settings for your view. In the View Name field enter a name for the view.

How do I create a custom block in Drupal 8?

Go to admin -> structure -> block layout -> custom block library. Click 'block types' tab. Once here, click on the 'Add custom block type' button. Enter block label and description.


2 Answers

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;

like image 105
chiborg Avatar answered Sep 19 '22 10:09

chiborg


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.

like image 39
Eli Krupitsky Avatar answered Sep 19 '22 10:09

Eli Krupitsky