1

I know that I can use template_node_view_alter() or template_preprocess_node() in template.php, but the problem is, that some of the information that I can alter there, seems to be processed already. For example:

$variables['content']['links']['node']['#links']['node-readmore']['title']

have some styling informations and a bit of text that is already translated. Is it possible to add a link even before, that structure was constructed? I don't want to overwrite something that was generated by Drupal for specific purpose.

2 Answers 2

1

You don't have to worry about overwriting what is there, just don't overwrite anything :)

The correct place for altering links is generally hook_node_view_alter(), as you mentioned.

For adding your own links it is hook_node_view()

As an example see http://drupal.org/node/224333#node_links

0

Before the node, comes the page. To process before the page, try

function MYTHEME_preprocess_page(&$variables) {

}

And before the page, comes HTML. To process before HTML, try

function MYTHEME_preprocess_html(&$variables) {

}

This would also go into your template.php

UPDATE:

You'll first need to know what's currently in the $variables array. To do so, you'll need to install & enable the Devel module. Then insert kpr($variables); within the preprocess function. When you'll refresh the page, you'll see what's actually inside the $variables array.

If you want to modify $variables['content']['links']['node']['#links']['node-readmore']['title'], you'd simply do this:

function MYTHEME_preprocess_page(&$variables) {
  $variables['content']['links']['node']['#links']['node-readmore']['title'] = 'title';    
}

Say, you want to keep the title, but want to add a <div> tag around it. This is where you need to follow Drupal Forms API.

['#prefix'] and ['#suffix'] will include string before and after form.

Here's the code:

function MYTHEME_preprocess_page(&$variables) {
  $variables['content']['links']['node']['#links']['node-readmore']['#prefix'] = '<div>';
  $variables['content']['links']['node']['#links']['node-readmore']['#suffix'] = '</div>';
}

Don't forget to change MYTHEME into your theme name, check if $variables array of the item you're changing, actually exists, and flush cache.

8
  • But inside these hooks, is no information about links whatsoever. I doubt you can just add something there that will be processed to links, but if so, then how? Oct 10, 2012 at 8:19
  • $variables array carries most of the data for your website. $variables array in preprocess_html, is used to build the html part of the page, then that information is carried to preprocess_page, where you can modify it again, then it carries the info to build the page, then it's carried to preprocess_node, where you can modify it again, then it carries the info to build the node, and so on. Oct 10, 2012 at 21:42
  • I'll update my answer with more info Oct 10, 2012 at 21:43
  • I've update the answer. See if that helps! Oct 10, 2012 at 21:56
  • It doesn't work for me. These variables added to content at hook_preprocess_page doesn't seem to appear in $variables at hook_preprocess_node() and doesn't appear on page. They had to be overridden. Oct 11, 2012 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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