2

I have created a custom form through Drupal form API. Also i have added various content pages of a Content Type called "Search Pages". Those pages contain only Metadata which they are hidden. What i actually want is to show my custom form into those pages (Search Pages). How can i implement that programmaticaly?

1

You can output form as block, then output this block in region and set block's visibility options to show only on specific content types:

/**
 * Implements hook_block_info().
 */
function mymodule_block_info() {
  $blocks = array();
  $blocks['my_block'] = array(
    'info' => t('Block with form'),
  );
  return $blocks;
}
/**
 * Implements hook_block_view().
 */
function mymodule_block_view($delta = '') {
  $block = array();
  switch($delta) {
    case 'my_block' :
      $block['content'] = drupal_get_form('mymodule_custom_form');
      break;
  }
  return $block;
}
/**
 * Form example.
 */
function mymodule_custom_form($form, &$form_state) {
  $form = array();
  $form['text'] = array(
    '#type' => 'textfield',
    '#title' => t('Place your form fields here'),
  );
  return $form;
}

Clear cache, go to blocks page (/admin/structure/block), assign your block to region. Go to your block configuration page and define Visibility settings on Content types tab.

  • Simple, straightforward, awesome! – Labrosb Apr 3 '16 at 19:35
  • Happy to help :) – skorzh Apr 3 '16 at 19:38

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.