1

I would like to display a specific message to users when they are previewing a node they are submitting. So I thought I would use a block with PHP code, using some info indicating that the node is being previewed. Unfortunately, I cannot get it working. I have tried these conditions:

if ($variables['view_mode'] == 'preview') ...

I get this error:

Notice : Undefined variable: variables in eval() (line 4 in /.../modules/php/php.module(80) : eval()'d code).

Tried this too:
if ($node->in_preview == TRUE) ...

I get these errors:

Notice : Undefined variable: node in eval() (line 5 in /.../php.module(80) : eval()'d code). Notice : Trying to get property of non-object in eval() (line 5 in /.../php.module(80) : eval()'d code).

I guess I have to declare the variable before, but how? I thought that since the node form is rendered anyway, the needed variable was available?

2 Answers 2

1

The variable that inside node.tpl.php says the view mode currently used for the node is $view_mode, but that variable is not used when Drupal is displaying a preview of the node. To know that, you can:

  • Check the value of $node->in_preview; when that value is TRUE, Drupal is displaying the preview of a node.

  • Check that $classes contains the string 'node-preview', such as with the following code.

    if (strpos($classes, 'node-preview') !== FALSE) {
      // Drupal is showing a preview of the node.
    }
    

Drupal already shows a message when the user is watching the preview of a node, anyway.

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.

0

Blocks don't know anything about the main content that is being displayed. They have to deduce this information e.g. from the current Drupal path.

If it's OK to display the message in the main content area, implement hook_node_view() to display the message.

Another solution is to implement hook_node_view() in such a way that it lets the block know that a node is being previewed, e.g.

function example_node_is_in_preview($node = null) {
  static $result = null;
  if ($node !== null) {
    $result = $node;
  }
  return $result;
}

Call this function in your hook_node_view() implementation, passing the node as argument. Call this function in your block code without arguments to determine if and which node is being previewed.

This works because the main content area and its form submissions are dealt with before block content is rendered.

1
  • Oswald, Thanks a lot for your detailed response. The thing is I am not a developer, so it's not that clear to me. However, I have tried the 1st proposed solution. So I put this into node.tpl.php: function hook_node_view($node, $view_mode, $langcode) { if ($view_mode == 'node-preview') { $node->content['SPECIFIC_field'] = array( '#markup' => $SPECIFIC_field, '#weight' => 10, '#text' => 'SPECIFIC MESSAGE', ); } } But it has no effect. Would you have more tips to achieve it?
    – Guirec
    Commented Mar 9, 2012 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.