To preprocess the node title field is obviously not the easiest. It has its place in the node.tpl.php and in the page.tpl.php by default and it has its own status in Drupal core, which makes it not fieldable as simply as other fields and entities in a given content type bundle.
That's why there are many discussions going on at drupal.org and some little helper modules are also around already, to make the title field more flexible in different scenarios like field translation, label design, field order and much more. So if this is your problem you will easely find a little module helping you.
But what if you don't want to touch the behaviour of the title field as designed in core but only want to preprocess the title to change the markup dynamically? Suddently google keeps silent. Unlike others I agree with some core contributors who explain wisely why the title field has its reasonable difficulties and why the decision to make it simply a "field like others" is not THAT easy to make. So for some reasion I need a solution in template.php
This is how the page.tpl.php looks like in my case:
// page.tpl.php
<?php print render($title_prefix); ?>
<?php if ($title): ?>
<?php if ($page): ?>
<!-- This is where we want to get in with template.php -->
<h1 <?php print $title_attributes; ?>>
<?php print $title; ?>
</h1>
<?php endif; ?>
<?php endif; ?>
<?php print render($title_suffix); ?>
And this the place where we obvisously should try to make it happen:
// template.php
function YOURTHEME_process_page(&$variables) {
if ($variables['view_mode'] == 'full' &&
node_is_page($variables['node'])) {
// do something with $variables['title']['attributes'][];
}
}
But to be honest, I am a little bit lost here, since I only want to change the rendering of <h1>
to <h1 class="my_css_class">
. Does anybody has an idea of how to achieve this?
The attributes class seems not to exist this way, because I get an Unknown index warning and the class gets not attached. Or maybe I have to point to $title another way? Maybe something like $node->title ? I know how to wrap the title with a new container, or how to change the suffix/prefix. But here would really appreciate your thoughts. Thank you very much ...
$title_attributes
is not added by Drupal core (see the source of page.tpl.php), so it must be a theme/module specific to your installation that's adding it. WIthout knowing what the theme/module is I don't see how this can be answeredhook_preprocess_page()
orhook_process_page()
, depending on which one's actually been used in the first instance. I'd search through your custom theme/modules for the term$vars['title_attributes']
or$variables['title_attributes']
to see if you can find where it's being set. From there it should be easy