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.

  • 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 May 8 '14 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 May 8 '14 at 13:38
1
  $block = module_invoke('module_name', 'block_view', 'block_id');

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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