You are here

function panels_export_display in Panels 6.2

Same name and namespace in other branches
  1. 8.3 panels.module \panels_export_display()
  2. 5.2 panels.module \panels_export_display()
  3. 6.3 panels.module \panels_export_display()
  4. 7.3 panels.module \panels_export_display()

Exports the provided display into portable code.

This function is primarily intended as a mechanism for cloning displays. It generates an exact replica (in code) of the provided $display, with the exception that it replaces all ids (dids and pids) with 'new-*' values. Only once panels_save_display() is called on the code version of $display will the exported display written to the database and permanently saved.

Parameters

object $display instanceof panels_display \n: This export function does no loading of additional data about the provided display. Consequently, the caller should make sure that all the desired data has been loaded into the $display before calling this function.

string $prefix: A string prefix that is prepended to each line of exported code. This is primarily used for prepending a double space when exporting so that the code indents and lines up nicely.

Return value

string $output The passed-in $display expressed as code, ready to be imported. Import by running eval($output) in the caller function; doing so will create a new $display variable with all the exported values. Note that if you have already defined a $display variable in the same scope as where you eval(), your existing $display variable WILL be overwritten.

See also

panels_page_export() or _panels_page_fetch_display() for sample implementations.

Related topics

3 calls to panels_export_display()
panels_mini_export in panels_mini/panels_mini.module
Export a mini panel into PHP code for use in import.
panels_page_export in panels_page/panels_page.write.inc
Export a panel page into PHP code for use in import.
panels_page_save_display in panels_page/panels_page.write.inc
Check to see if the panel page needs to be saved due to a display having been just saved.

File

./panels.module, line 797
panels.module

Code

function panels_export_display($display, $prefix = '') {
  $output = '';
  $output .= $prefix . '$display = new panels_display()' . ";\n";
  $output .= $prefix . '$display->did = \'new\'' . ";\n";

  // $fields = array('name', 'layout', 'layout_settings', 'panel_settings');
  // TODO 'name' field was removed several months ago, so temporarily removing
  // it from here whil investigations into exactly why it was removed continue
  $fields = array(
    'layout',
    'layout_settings',
    'panel_settings',
  );
  foreach ($fields as $field) {
    $output .= $prefix . '$display->' . $field . ' = ' . panels_var_export($display->{$field}, $prefix) . ";\n";
  }
  $output .= $prefix . '$display->content = array()' . ";\n";
  $output .= $prefix . '$display->panels = array()' . ";\n";
  $panels = array();
  if (!empty($display->content)) {
    $pid_counter = 0;
    $region_counters = array();
    foreach ($display->content as $pane) {
      $pane->pid = 'new-' . ++$pid_counter;
      $output .= panels_export_pane($pane, $prefix . '  ');
      $output .= "{$prefix}  " . '$display->content[\'' . $pane->pid . '\'] = $pane' . ";\n";
      if (!isset($region_counters[$pane->panel])) {
        $region_counters[$pane->panel] = 0;
      }
      $output .= "{$prefix}  " . '$display->panels[\'' . $pane->panel . '\'][' . $region_counters[$pane->panel]++ . '] = \'' . $pane->pid . "';\n";
    }
  }
  return $output;
}