Please enable JS

Programmatically load a panels pane

February 01/Brian Young/Drupal 7Software

I needed to be able to render an individual pane from a Ctools page via AJAX after a user updated a modal form. See my previous article on how to programmatically trigger AJAX and return HTML from a URL.

At the bottom of this snippet, I’m using the preprocess hook to add the pane’s ID as a HTML data attribute for my JavaScript. This part is optional, but I figured I would include it to help.

Please note: I am loading a single pane from an existing Ctools Page. This Ctools Page must be a saved and functioning page for this to work. We’re just reaching in and loading one panels pane from what would normally be an entire page.

Example Code

render_pane.module

  1. /**
  2.  * Renders a single pane from the "test_panels_page" Ctools Page.
  3.  *
  4.  * @param integer $pane_id The numeric pane ID for each pane in a panels variant ctools page. (This id is in the panels_pane table.)
  5.  * @param integer $uid The User ID we require for the Context.
  6.  * @return string The rendered markup of the pane.
  7.  */
  8. function render_pane_render($pane_id, $uid) {
  9. // Do some bootstrapping so we can use some Panels files.
  10. module_load_include('inc', 'panels', 'plugins/task_handlers/panel_context');
  11. module_load_include('inc', 'panels', 'includes/plugins');
  12.  
  13. // Load up an existing Ctools Page called "test_panels_page"
  14. $ctools_page = page_manager_get_page_cache('page-test_panels_page');
  15.  
  16. // Turn the result array into just the object at the first index of the array.
  17. $handler = reset($ctools_page->handlers);
  18.  
  19. // Load the display object.
  20. $display = panels_panel_context_get_display($handler);
  21.  
  22. // Load the user object that our Page Context requires.
  23. $account = user_load($uid);
  24.  
  25. // Create a basic context for the user account argument.
  26. $context = new ctools_context(array(
  27. 'entity:user',
  28. 'entity',
  29. 'user',
  30. ), $account);
  31. // Stub out some
  32. $context->argument = $uid;
  33. $context->title = $account->name;
  34. $context->empty = FALSE;
  35. $context->id = 'argument_entity_id:user_1';
  36. $context->original_argument = $uid;
  37. $context->plugin = "user";
  38.  
  39. $display->args[] = $account->uid;
  40. $display->context[$context->id] = $context;
  41.  
  42. // Get the display's renderer object.
  43. $renderer = panels_get_renderer($handler->conf['pipeline'], $display);
  44.  
  45. $renderer->prepare();
  46. // Render an individual pane of the potential multiple panes on the ctools page.
  47. $content = $renderer->render_pane($renderer->prepared['panes'][$pane_id]);
  48.  
  49. // Return the rendered markup to the calling process.
  50. return $content;
  51. }
  52.  
  53. // The below is optional but nice if we want easy access to a pane's ID for AJAX.
  54. /**
  55.  * Implments hook_preprocess_HOOK_ID().
  56.  */
  57. function render_pane_preprocess_panels_pane(&$variables) {
  58. // Add the a pane's DB ID to each wrapper so we can grab it for potential AJAX pane rendering.
  59. $variables['classes_array'][] = 'pane-id';
  60. $variables['classes_array'][] = 'pane-id-' . $variables['pane']->pid;
  61. $variables['attributes_array']['data-pane-id'] = $variables['pane']->pid;
  62. }


RELATED POSTS


Comments/ 0


LEAVE A COMMENT

(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.
But why?

My random thoughts and documentation on the various hardware and software projects that occupy my day-to-day. My only hope is that other people can benefit from my notes.

Recent posts
Categories