Articles


How to render a custom form block and a webform block in a single block?

How to render a custom form block and a webform block in a single block?



Posted bysivaprasad,20th May 2016

This Example here shows how we can render one custom form block and a webform block in another single block.


function MYMODULE_block_info() {
  $blocks['custom_block'] = array(
     'info' => t('Block rendering webform block and custom form block'),
     'cache' => DRUPAL_NO_CACHE,
  );
  return $blocks;
}

function MYMODULE_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'custom_block':
      $block['subject'] = '';
      $block['content'] = MYMODULE_custom_block();
      break;
  }
  return $block;

Custom form Block


function MYMODULE_block_form($form, &$form_state) {
  
 $form['text'] = array(
    '#type' => 'textfield',
    '#attributes' =>array('placeholder' => t('Start Typing')),
  ); 

$form['submit_button'] = array(
    '#type' => 'submit',
    '#value' => t('Click Here!'),
  );
  return $form;
}

Assume webform block as 'Client-block-1234'

Block rendering the other two Blocks


function MYMODULE_custom_block() {
  $webform = module_invoke('webform', 'block_view', 'client-block-1234');
  $form = drupal_get_form('MYMODULE_block_form');
  $output = render($form) . $webform['content'];
  return $output;
}

Related Articles