1

How do I put a block in a form?

My searches on this site and on google only have lead to questions of putting a form in a block. I want the opposite. I have a form definition array in a module, and I want to add in an existing block.

2
  • It would be difficult. In Drupal you only put blocks in regions, and regions are theme-dependent. Forms are module-dependent so it will be a lot of ugly hacks to do it that way. Do you have any actual problem you are facing, any use case that would require it?
    – Mołot
    Commented May 8, 2014 at 6:34
  • I have the form I want, except it's in a block. Instead of duplicating the code defining the form in another form, I would just like to put it right where I want it.
    – user1359
    Commented May 8, 2014 at 13:38

1 Answer 1

1
  $block = module_invoke('module_name', 'block_view', 'block_id');

  $form['block'] = array(
    '#markup' => render($block),
  );
2
  • Of course if form itself will end up in this block, it will cause beautiful case of WSOD.
    – Mołot
    Commented May 12, 2014 at 6:40
  • It's working out for me.
    – user1359
    Commented May 12, 2014 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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