<?php
use DrupalnodeEntityNode;
/**
* Implements hook_preprocess_page().
*/
function alternative_title_preprocess_page(&$variables) {
// Check if the current route contains a node.
if ($node = Drupal::routeMatch()->getParameter('node')) {
// Check if the node type is 'info'.
if ($node->getType() == 'info') {
Drupal::messenger()->addMessage('Node type is Info');
$node_title = $node->getTitle();
// Get the alternative title field value.
$alternative_title = $node->get('field_titel')->value;
// If an alternative title exists, use it.
if (!empty($alternative_title)) {
$variables['#title'] = $alternative_title;
Drupal::messenger()->addMessage($variables['#title']);
}
} else {
Drupal::messenger()->addMessage('Node type is not Info, node type is "' . $node->getType() . '"');
}
}
}
My goal is to write a module which shows an alternative title instead of the real title if the alternative title is not empty. Somehow the $variables[‘#title’] gets the change of the title in the Messenger but on the actual page it’s still showing the old title.
Maybe I have to change the title in a different way, but I am not sure how.
Thanks for the help!