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.