/**
  * Render the panels display for a given panelizer entity.
  *
  * @param stdClass $entity
  *   A fully-loaded entity object controlled by panelizer.
  * @param array $args
  *   Optional array of arguments to pass to the panels display.
  * @param string $address
  *   An optional address to send to the renderer to use for addressable
  *   content.
  * @param array $extra_contexts
  *   An optional array of extra context objects that will be added to the
  *   display.
  *
  * @return array
  *   If the entity isn't panelized, this returns NULL. Otherwise, it returns an
  *   associative array as meant for use with CTools with the following keys:
  *   - 'content': String containing the rendered panels display output.
  *   - 'no_blocks': Boolean defining if the panels display wants to hide core
  *      blocks or not when being rendered.
  */
 function render_entity($entity, $view_mode, $langcode = NULL, $args = array(), $address = NULL, $extra_contexts = array())
 {
     if (empty($entity->panelizer[$view_mode]) || empty($entity->panelizer[$view_mode]->display)) {
         return FALSE;
     }
     list($entity_id, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);
     $panelizer = $entity->panelizer[$view_mode];
     $display = $panelizer->display;
     $display->context = $this->get_contexts($panelizer, $entity) + $extra_contexts;
     $display->args = $args;
     $display->css_id = $panelizer->css_id;
     // This means the IPE will use our cache which means it will get appropriate
     // allowed content should it be selected.
     $display->cache_key = implode(':', array_filter(array('panelizer', $this->entity_type, $entity_id, $view_mode, $revision_id)));
     // Check to see if there is any CSS.
     if (!empty($panelizer->css)) {
         ctools_include('css');
         $filename = ctools_css_retrieve($display->cache_key);
         if (!$filename) {
             $filename = ctools_css_store($display->cache_key, $panelizer->css);
         }
         drupal_add_css($filename, array('group' => CSS_THEME));
     }
     if ($view_mode == 'page_manager') {
         // We think this is handled as a page, so set the current page display.
         panels_get_current_page_display($display);
     }
     // Allow applications to alter the panelizer and the display before rendering them.
     drupal_alter('panelizer_pre_render', $panelizer, $display, $entity);
     ctools_include('plugins', 'panels');
     $renderer = panels_get_renderer($panelizer->pipeline, $display);
     // If the IPE is enabled, but the user does not have access to edit
     // the entity, load the standard renderer instead.
     // Use class_parents so we don't try to autoload the class we are testing.
     $parents = class_parents($renderer);
     if (!empty($parents['panels_renderer_editor']) && (!$this->panelizer_access('content', $entity, $view_mode) && !$this->entity_access('update', $entity))) {
         $renderer = panels_get_renderer_handler('standard', $display);
     }
     $renderer->address = $address;
     $info = array('title' => $panelizer->display->get_title(), 'content' => panels_render_display($display, $renderer), 'no_blocks' => !empty($panelizer->no_blocks));
     $info['classes_array'] = array();
     if (!empty($panelizer->css_class)) {
         foreach (explode(' ', $panelizer->css_class) as $class) {
             $class = ctools_context_keyword_substitute($class, array(), $display->context);
             if ($class) {
                 $info['classes_array'][] = drupal_html_class($class);
             }
         }
     }
     if (!empty($parents['panels_renderer_editor'])) {
         $path = drupal_get_path('module', 'panelizer');
         ctools_add_js('panelizer-ipe', 'panelizer');
         drupal_add_js($path . "/js/panelizer-ipe.js", array('group' => JS_LIBRARY));
         drupal_add_css($path . "/css/panelizer-ipe.css");
     }
     return $info;
 }
  function edit_form_content(&$form, &$form_state) {
    ctools_include('ajax');
    ctools_include('plugins', 'panels');
    ctools_include('display-edit', 'panels');
    ctools_include('context');

    // If we are cloning an item, we MUST have this cached for this to work,
    // so make sure:
    if ($form_state['form type'] == 'clone' && empty($form_state['item']->export_ui_item_is_cached)) {
      $this->edit_cache_set($form_state['item'], 'clone');
    }

    $cache = panels_edit_cache_get('panels_mini:' . $this->edit_cache_get_key($form_state['item'], $form_state['form type']));

    $form_state['renderer'] = panels_get_renderer_handler('editor', $cache->display);
    $form_state['renderer']->cache = &$cache;

    $form_state['display'] = &$cache->display;
    $form_state['content_types'] = $cache->content_types;
    // Tell the Panels form not to display buttons.
    $form_state['no buttons'] = TRUE;
    $form_state['display_title'] = !empty($cache->display_title);

    $form = panels_edit_display_form($form, $form_state);
    // Make sure the theme will work since our form id is different.
    $form['#theme'] = 'panels_edit_display_form';
  }
 function edit_form(&$form, &$form_state)
 {
     ctools_include('plugins', 'panels');
     // If the plugin is not set, then it should be provided as an argument:
     if (!isset($form_state['item']->plugin)) {
         $form_state['item']->plugin = $form_state['function args'][2];
     }
     parent::edit_form($form, $form_state);
     $form['category'] = array('#type' => 'textfield', '#title' => t('Category'), '#description' => t('What category this layout should appear in. If left blank the category will be "Miscellaneous".'), '#default_value' => $form_state['item']->category);
     ctools_include('context');
     ctools_include('display-edit', 'panels');
     ctools_include('content');
     // Provide actual layout admin UI here.
     // Create a display for editing:
     $cache_key = 'builder-' . $form_state['item']->name;
     // Load the display being edited from cache, if possible.
     if (!empty($_POST) && is_object($cache = panels_edit_cache_get($cache_key))) {
         $display =& $cache->display;
     } else {
         $content_types = ctools_content_get_available_types();
         panels_cache_clear('display', $cache_key);
         $cache = new stdClass();
         $display = panels_new_display();
         $display->did = $form_state['item']->name;
         $display->layout = $form_state['item']->plugin;
         $display->layout_settings = $form_state['item']->settings;
         $display->cache_key = $cache_key;
         $display->editing_layout = TRUE;
         $cache->display = $display;
         $cache->content_types = $content_types;
         $cache->display_title = FALSE;
         panels_edit_cache_set($cache);
     }
     // Set up lipsum content in all of the existing panel regions:
     $display->content = array();
     $display->panels = array();
     $custom = ctools_get_content_type('custom');
     $layout = panels_get_layout($display->layout);
     $regions = panels_get_regions($layout, $display);
     foreach ($regions as $id => $title) {
         $pane = panels_new_pane('custom', 'custom');
         $pane->pid = $id;
         $pane->panel = $id;
         $pane->configuration = ctools_content_get_defaults($custom, 'custom');
         $pane->configuration['title'] = 'Lorem Ipsum';
         $pane->configuration['body'] = $this->lipsum;
         $display->content[$id] = $pane;
         $display->panels[$id] = array($id);
     }
     $form_state['display'] =& $display;
     // Tell the Panels form not to display buttons.
     $form_state['no buttons'] = TRUE;
     $form_state['no display settings'] = TRUE;
     $form_state['cache_key'] = $cache_key;
     $form_state['content_types'] = $cache->content_types;
     $form_state['display_title'] = FALSE;
     $form_state['renderer'] = panels_get_renderer_handler('editor', $cache->display);
     $form_state['renderer']->cache =& $cache;
     $form = panels_edit_display_form($form, $form_state);
     // If we leave the standard submit handler, it'll try to reconcile
     // content from the input, but we've not exposed that to the user. This
     // makes previews work with the content we forced in.
     $form['preview']['button']['#submit'] = array('panels_edit_display_form_preview');
 }