Embed a mini panel in code

By shane
Mon, 2011-03-14 20:39

Share with Others

So I recently ran into the situation where I was theming an Ubercart product node page and had the need to display a mini panel in the .tpl.php file. Here is the code I used to get it to display. The first code snippets went in the template.php file of the theme, while the second code snippet went in the node-product.tpl.php file.

You will need to replace "MYTHEME" with the name of your theme and "my_mini_panel" with the name of your mini panel. This can be found on the mini panels admin page (admin/build/mini-panels) under the Name column.

function MYTHEME_preprocess_node(&$vars, $hook) {
  $function = 'MYTHEME_preprocess_node'.'_'. $vars['node']->type;
  if (function_exists($function)) {
function MYTHEME_preprocess_node_product(&$vars) {
  $quicktabs = quicktabs_load(1);
  $vars['my_quicktab'] = theme('quicktabs', $quicktabs);
  //add a node context to the mini panel
  $node = node_load($vars['nid']);
  $panel_mini = panels_mini_load('my_mini_panel');
  $context = new StdClass();
  $context->type = 'node';
  $context->data = $node;
  $contexts =  array($context);
  $context = ctools_context_match_required_contexts($panel_mini->requiredcontexts, $contexts);
  $panel_mini->context = $panel_mini->display->context = ctools_context_load_contexts($panel_mini, FALSE, $context);
  $vars['my_display'] = panels_render_display($panel_mini->display);

You will notice that the tricky part here is getting the context to apply to the mini-panel. I had to do quite a bit of searching to find the code (I believe from the issue queue on drupal.org) to get the mini panel to display. You will also notice that I have the quicktabs module installed and am also manually loading a quicktabs display into a variable.

Now that I have my preprocess function I can go ahead and use the $my_display and $my_quicktab variables in my node-product.tpl.php file. Below are the quick html/php snippets that are in my node-product.tpl.php file.

 print $my_display;

 print $my_quicktab;

For a little back story, I would have done the entire node view page for the products in panels but it is not easy to get the Ubercart product information to display. You can do it through a view, but in this case it was easier to just jump into the theme (and a lot more efficient). The reason for using a mini-panel was to create an area displaying author information. The mini-panel I used had a required node context and two relationships, one for the node author, and one for the related profile node.

If I was more of a designer or themer, I probably could have bypassed the mini-panel altogether but my lack of design skills lends itself better to the drag and drop capabilities of panels.


I just had this issue and couldn't make your code work in my template file. A bit of digging later, this is what I came up with (D7 version).

$block = module_invoke('panels_mini', 'block_view', 'mini_panel_machine_name');
print $block['content'];

Replace "mini_panel_machine_name" with the machinename of your mini-panel.

Thank you for your code, this was exactly what I was looking for and it works like a charm.

However, the substitution keywords are empty when displayed (checkbox "Use substitution keywords" is checked when adding custom panel content.

Drupal 7 - load and render mini-panel, with context:

  $delta = 'my_mini_panel';
  $panel_mini = panels_mini_load($delta);
  $context = ctools_context_create('entity:node', $node);
  $contexts = ctools_context_match_required_contexts($panel_mini->requiredcontexts, array($context));
  $panel_mini->context = $panel_mini->display->context =  ctools_context_load_contexts($panel_mini, FALSE, $contexts);
  $panel_mini->display->css_id = panels_mini_get_id($panel_mini->name);
  // Here is the renderable markup, to with with whatever you wish.
  $renderable_markup = panels_render_display($panel_mini->display);

* Note: first arg for ctools_context_create() is -- $type: The type of context to create; this loads a plugin.
For entities in general, this seems to be 'entity:ENTITY_TYPE'.

Post new comment