Please enable JS

Programmatically render Page Manager page

April 08/Brian Young/Drupal 7

I needed to programmatically render a Ctools Page Manager page that consists of panels. The page required the user context in order to load the related content.

Below is a short example of how I'm manually rendering the page variant from a custom URL in Drupal 7.

  1. /**
  2.  * Implements hook_menu().
  3.  */
  4. function sample_module_menu() {
  5. return array(
  6. 'render/user/%/page' => array(
  7. 'title' => 'Member Profile Preview',
  8. 'access callback' => true,
  9. 'page callback' => 'sample_module_render_page',
  10. 'page arguments' => array(2),
  11. ),
  12. );
  13. }
  14.  
  15. /**
  16.  * The menu entry callback for render/user/%/page.
  17.  *
  18.  * @param $target_uid
  19.  * @return array
  20.  */
  21. function sample_module_render_page($target_uid) {
  22.  
  23. module_load_include('inc', 'ctools', 'page_manager/plugins/tasks/page');
  24. module_load_include('inc', 'ctools', 'includes/context');
  25.  
  26. $subtask_id = 'page_variant_machine_name';
  27. $page = page_manager_page_load($subtask_id);
  28. $task = page_manager_get_task($page->task);
  29. $subtask = page_manager_get_task_subtask($task, $subtask_id);
  30.  
  31.  
  32. $user_context = ctools_context_create('entity:user', user_load($target_uid));
  33. $user_context->id = 'argument_entity_id:user_1';
  34. $user_context->original_argument = $user_context->argument;
  35. $user_context->identifier = "User";
  36. $user_context->keyword = "user";
  37.  
  38. // Turn the contexts into a properly keyed array.
  39. $contexts = array($user_context->id => $user_context);
  40. $args = array($target_uid);
  41.  
  42. ctools_include('context-task-handler');
  43. $output = ctools_context_handler_render($task, $subtask, $contexts, $args);
  44. if ($output === FALSE) {
  45. return MENU_NOT_FOUND;
  46. }
  47.  
  48. return $output;
  49. }


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