2

How can I add a custom form to a Panels page in Drupal 7?

return drupal_get_form('my_custom_form');

I would prefer not to use the PHP input filter if possible.

5

Create a custom module that defines a block with that form as the content. You can then place that block anywhere on your site using the standard Drupal interfaces (blocks configuration, Panels, Context, etc.).

There are many readily available resources on creating custom modules that define blocks. The Drupal.org Module Developer's guide is a great place to start, as well as the Examples module, which includes a sample module for defining blocks that can be easily copied and customized.

2
  • "Create a custom module that defines a block with that form as the content." In my case my content type display does not have any form associated with it. So how do I add that display to a block? – pal4life Oct 28 '14 at 0:42
  • You can also create a Custom Panels Pane for that. See Tutorial from Campbell Vertesi for How to Create a Custom Panels Pane – LarS Aug 19 '15 at 20:12
1

it can be done using hook_block

function MODULENAME_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks[1] = array('info' => t("BLOCK TITLE"));        
    return $blocks;   
    case 'view':
      $block =array(
        'subject'=>t("BLOCK TITLE"),
        'content'=>drupal_get_form('my_custom_form'));
  return $block;
  }
}

Now you can include this block in your panel :)

1
  • 3
    In Drupal 7 this hook has been replaced several functions. List, configure, save and view operations are carried out by hook_block_info(), hook_block_configure(), hook_block_save() and hook_block_view(), respectively. – Brent Mar 21 '13 at 21:58
1

It was trickier to pass context's to blocks that I thought, but that was the simplest solution here.

Though while searching for a solution I did run across an interesting module that others may find useful.

Check out Page Manager: Existing Pages

You can use any path registered with hook_menu() and it works beautifully.

0

You can create ctools plugin that will show the form. Passing context to plugins is close to trivial:

$plugin = array(
  'category' => ...,
  'render callback' => 'example_pane_render',
  'edit form' => 'example_pane_render',
  'required context' => new ctools_context_required(t('Account'), 'user'),
  'title' => t('User profile form'),
);

...

/**
 * Render callback.
 */
function example_pane_render($subtype, array $conf = array(), $args, $context) {
  module_load_include('pages.inc', 'user');
  if (count($args) > 1) {
    $profile_type = $args[1];
  }
  else {
    $profile_type = $conf['profile_type'];
  }
  return (object) array(
    'content' => drupal_get_form('user_profile_form', $context->data, $profile_type),
  );
}

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.