1

This question already has an answer here:

I am trying to call a custom form in a block. The form is written in my custom module. I tried different ways to call the form, but not getting the form.

Here is the syntax, I last tried.

$form = drupal_get_form('my_custom_form');
print drupal_render($form); 
//my_custom_form is my form_id

(or)

drupal_render(drupal_get_form('my_custom_form'));

Here is the link I found a similar question, but didn't get the correct solution for my question. Form within a block

marked as duplicate by Mołot, monymirza, Free Radical, Patrick Kenny, Beebee Nov 6 '13 at 15:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I tried that way. The below answer is the same for that question. I want a new way of doing it. I want to know an newer methods of doing it. – Kiran Tej Nov 4 '13 at 8:47
4

You should return your form using drupal_get_form in the content definition of your block. Drupal will do the rest.

For example like this:

function YOURMODULE_block_info()
{
    $blocks = array();

    $blocks['custom_block'] = array(
        'info' => t('Custom block example'),
        'title' => t('Custom block example'),
        'region' => 'content',
        'status' => 1,
    );

    return $blocks;
}

function YOURMODULE_block_view($delta = '')
{
    $block = array();

    switch ($delta) {
        case 'custom_block' :
            $block['content'] = YOURMODULE_block_content();
            break;
    }

    return $block;
}

function YOURMODULE_block_content()
{
    return drupal_get_form('my_custom_form');
}

function my_custom_form($form, &$form_state)
{
    // Build your form

    return $form
}
  • Yes, I tried this. Is there any other method to do this? – Kiran Tej Nov 4 '13 at 7:06
  • You also can directly call the drupal_render on the form and return it as #markup content for the block ... but you already tried ... which isn't working for you. Do you have some content of the block rendered on the page? (i.e.: the block wrapper html, title, ...) – Mike Vranckx Nov 4 '13 at 12:22
  • Anyway as the requirement is urgent, I am using the old method. Thanks for your help. – Kiran Tej Nov 5 '13 at 4:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.