Preview functionality is known feature of many publishing systems: it's intended to show exactly how your content will look like before publishing. With nodes displayed in Panel Pages there is issue that panel layout can be quite complex, and sometimes you can't have same display in Panel Page and in preview (without spending too much effort). Basically, to have same display one would have to code that complex layout in node template too, in addition to Panel lauout template, or have some way to render Panel display in preview.
How do you solve that problem ? It's question to everyone who uses Panels, not only Earl.
Is it possible to render Panel page in preview in place of node template ?

Comments

crea’s picture

I am calling page_manager_node_view($node) from my theme_node_preview(). It works ok for simple panels, but for panel where I have mini-panels embedded as panes I get WSOD.

markus_petrux’s picture

Before invoking page_manager_node_view(), you probably need to load the file where it is:

module_load_include('inc', 'page_manager', 'plugins/tasks/node_view');

You should see errors in your log though.

crea’s picture

Yes I thought too that it's something about missing function. However including 'plugins/tasks/node_view' didn't help. I think it's included in page_manager_get_task() anyway, that's why I get working panel if I disable mini-panels.
This probably has something to do with mini-panel specific code which relies on missing code.

crea’s picture

Huh, actually it was custom formatter code that went crazy.

crea’s picture

Status: Active » Needs review

Ok, my code. Maybe someone finds this useful. Something like this would be nice to have in Panels out of the box.

/**
 * Implementation of theme_node_preview(). 
 */
function mytheme_node_preview($node) {
  $output = '<div class="preview clear-block">';

  $preview_trimmed_version = FALSE;
  // Do we need to preview trimmed version of post as well as full version?
  if (isset($node->teaser) && isset($node->body)) {
    $teaser = trim($node->teaser);
    $body = trim(str_replace('<!--break-->', '', $node->body));

    // Preview trimmed version if teaser and body will appear different;
    // also (edge case) if both teaser and body have been specified by the user
    // and are actually the same.
    if ($teaser != $body || ($body && strpos($node->body, '<!--break-->') === 0)) {
      $preview_trimmed_version = TRUE;
    }
  }

  if ($preview_trimmed_version) {
    drupal_set_message(t('The trimmed version of your post shows what your post looks like when promoted to the main page or when exported for syndication.<span class="no-js"> You can insert the delimiter "&lt;!--break--&gt;" (without the quotes) to fine-tune where your post gets split.</span>'));
    $output .= '<h3>'. t('Preview trimmed version') .'</h3>';
    $output .= node_view(drupal_clone($node), 1, FALSE, 0);
    $output .= '<h3>'. t('Preview full version') .'</h3>';
    $output .= custom_node_preview($node);
  }
  else {
    $output .= custom_node_preview($node);
  }
  $output .= "</div>\n";

  return $output;
}

/**
 * Render Panel Page as part of node preview page.
 * 
 * Based on page_manager_node_view().
 */
function custom_node_preview(&$node) {
  // Load my task plugin
  $task = page_manager_get_task('node_view');

  // Load the node into a context.
  ctools_include('context');
  ctools_include('context-task-handler');
  $contexts = ctools_context_handler_get_task_contexts($task, '', array($node));

  $output = ctools_context_handler_render($task, '', $contexts, array($node->nid));
  if ($output !== FALSE) {
    return $output;
  }
  // Otherwise, fall back.
  return node_view($node);
}

merlinofchaos’s picture

Status: Needs review » Needs work

If you would like this in Panels out of the box, can you provide it as an actual patch to the panels node module?

crea’s picture

I needed it for Panel Pages. I thought it works for panel nodes already, however I don't use them. I gave it some thought and came to conclusion it would be better to add this to documentation, since it would require override theming function within a module and any user defined function withing a theme would break it anyway. So it's better to just have this theming example and let user make theming function they want exactly.

merlinofchaos’s picture

Oh I see. So it would actually be a patch for CTools, not Panels.

In any case, an actual patch would be more useful to me.

thomjjames’s picture

Hi,

Think the code for #5 needs to be slightly different or you get the whole page return in the preview:

/**
* Render Panel Page as part of node preview page.
*
* Based on page_manager_node_view().
*/
function custom_node_preview(&$node) {
  // Load my task plugin
  $task = page_manager_get_task('node_view');

  // Load the node into a context.
  ctools_include('context');
  ctools_include('context-task-handler');
  $contexts = ctools_context_handler_get_task_contexts($task, '', array($node));

  $output = ctools_context_handler_render($task, '', $contexts, array($node->nid), false);
  if ($output !== FALSE) {
    return $output['content'];
  }
  // Otherwise, fall back.
  return node_view($node);
}

Think this should be default behaviour for the page_manager module.

Cheers
Tom

thalemn’s picture

Did this get added to CTools?

I'm using Panels to display results from a CCK form - but previewing the node isn't possible. I'm relatively new to Drupal, so I don't have the skills yet to work directly with the php...can you point me in the right direction?

Thank you for any assistance!

Tom

merlinofchaos’s picture

No, this has not been added. It would require a patch to be added.

That said, I do believe that fixing it so that when you click 'preview' on a node, you get the actual panel that would be displayed, so something like this would be desirable, and IMO should be automatic.

thalemn’s picture

I did find a patch to direct the preview to view the panel. Works great!

thomasmuirhead’s picture

Hi thomlynn,

Could you share where you found the patch - that would be great!

thanks

THomas

egifford’s picture

This information is great. Thanks.

liquidcms’s picture

this would be cool.. but code above (either version) doesn't work for me.

ctools_context_handler_render($task, '', $contexts, array($node->nid), false);   

returns false.

panels 3.9
ctools 1.8

pmathur01’s picture

did someone find a patch or a solution for this? I am not able to preview the panel node with preview.

thanks
Prerna

thalemn’s picture

I can't recall where I found this solution, but it does work.

I inserted this code into the template.php file:

function marinelli_node_preview($node) {
$output = '

';

$preview_trimmed_version = FALSE;
// Do we need to preview trimmed version of post as well as full version?
if (isset($node->teaser) && isset($node->body)) {
$teaser = trim($node->teaser);
$body = trim(str_replace('

', '', $node->body));

// Preview trimmed version if teaser and body will appear different;
// also (edge case) if both teaser and body have been specified by the user
// and are actually the same.
if ($teaser != $body || ($body && strpos($node->body, '

') === 0)) {
$preview_trimmed_version = TRUE;
}
}

if ($preview_trimmed_version) {
drupal_set_message(t('The trimmed version of your post shows what your post looks like when promoted to the main page or when exported for syndication. You can insert the delimiter "<!--break-->" (without the quotes) to fine-tune where your post gets split.'));
$output .= '

'. t('Preview trimmed version') .'

';
$output .= node_view(drupal_clone($node), 1, FALSE, 0);
$output .= '

'. t('Preview full version') .'

';
$output .= custom_node_preview($node);
}
else {
$output .= custom_node_preview($node);
}
$output .= "

\n";

return $output;
}

/**
* Render Panel Page as part of node preview page.
*
* Based on page_manager_node_view().
*/
function custom_node_preview(&$node) {
// Load my task plugin
$task = page_manager_get_task('node_view');

// Load the node into a context.
ctools_include('context');
ctools_include('context-task-handler');
$contexts = ctools_context_handler_get_task_contexts($task, '', array($node));

$output = ctools_context_handler_render($task, '', $contexts, array($node->nid), false);
if ($output !== FALSE) {
return $output['content'];
}
// Otherwise, fall back.
return node_view($node);
}

pmathur01’s picture

Thanks a lot! It worked beautifully..

Prerna

Letharion’s picture

Title: Usability: panel pages and preview button » Usability: Render panel page node_view during node preview
Category: support » feature
Status: Needs work » Closed (won't fix)

Feature request has essentially been accepted by merlin, but this issue has been in "Needs work" since 2009. I'm closing this one won't fix until someone can actually submit a patch.

glenn_echo’s picture

I struggled with a similar problem where ctools_context_handler_get_task_contexts returned false. I finally realized it returned false because I was passing it an array instead of an object. Simply:

  $node = (object) $node;

prior to calling the function made this work.

vali hutchison’s picture

#17 worked well for me too. I'm using D7 and found I had to change

return $output['content'];

to

return $output;

in the custom_node_preview function to make it work.

vali hutchison’s picture

Actually, the above doesn't fully work for the site i'm testing in D7. It will only preview the existing saved node. So on node creation, you just see 'Array' and for existing nodes you see the display well via panels, but not the new changes made in the edit form prior to clicking preview.

The panel testing does use 'OG group from node' as a context so maybe this is the issue?

liquidcms’s picture

Version: 6.x-3.x-dev » 7.x-3.x-dev
Issue summary: View changes
Status: Closed (won't fix) » Active

Any update on this?

i'll reopen as i don't think:

#19 - "I'm closing this one won't fix until someone can actually submit a patch."

makes any sense.

ezman’s picture

The reason none of these fixes work fully is because they're all calling ctools_context_handler_render() and passing in $node->nid

When a new unsaved node is previewed, it has no nid, so the page renderer is silently failing.

I've recently encountered this issue, and at the moment can see no easy way around it: the ctools page at node/%node needs a node id to work properly. Not to mention if you have any Views in there which want the node id to query the database with...

It seems to be a big can of worms. The only workaround I can think of right now is to just save the node when its previewed, possibly deleting it afterwards if needed.

japerry’s picture

Status: Active » Closed (outdated)

Drupal 7 is no longer supported, closing.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.