template_preprocess_node() uses the following code.
$variables['node'] = $variables['elements']['#node'];
$node = $variables['node'];
$variables['date'] = format_date($node->created);
$variables['name'] = theme('username', array('account' => $node));
$uri = entity_uri('node', $node);
$variables['node_url'] = url($uri['path'], $uri['options']);
$variables['title'] = check_plain($node->title);
It is perfectly fine, to set $variables['title']
in hook_preprocess_node()
instead of calling drupal_set_title()
/drupal_get_title()
. The error in the code shown by the OP is not setting the local $node
variable. The correct code is the following one.
// Drupal 7
function mytemplate_preprocess_node(&$variables) {
$variables['title'] = "trial" . check_plain($variables['node']->title);
}
In Drupal 8, the equivalent for the code I wrote would be the following one.
// Drupal 8
function mytemplate_preprocess_node(&$variables) {
$variables['label'] = "trial" . check_plain($variables['node']->label());
}
This would not change the title used for the <title>
though. If you need to change that as well, you should use the following code.
// Drupal 7
function mytemplate_preprocess_node(&$variables) {
$variables['title'] = "trial" . check_plain($variables['node']->title);
drupal_set_title("trial" . check_plain($variables['node']->title), PASS_THROUGH);
}