18.10.2016 Views

Drupal 7 Module Development

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3<br />

First let's look at the following theme_username function's documentation:<br />

/**<br />

* Format a username.<br />

*<br />

* @param $variables<br />

* An associative array containing:<br />

* - account: The user object to format.<br />

* - name: The user's name, sanitized.<br />

* - extra: Additional text to append to the user's name, sanitized.<br />

* - link_path: The path or URL of the user's profile page, home<br />

* page, or other desired page to link to for more information<br />

* about the user.<br />

* - link_options: An array of options to pass to the l() function's<br />

* $options parameter if linking the user's name to the user's<br />

* page.<br />

* - attributes_array: An array of attributes to pass to the<br />

* drupal_attributes() function if not linking to the user's page.<br />

*/<br />

Quite conveniently, theme_username() has a handy $link_path variable that we<br />

want to alter to achieve our old-school giggles. Assuming that we've used some<br />

other business logic with the user module's hooks to load our GeoCities URL into<br />

the user's account (the "hard" part), replacing the link to the user's profile page can<br />

be accomplished with the following simple preprocess function:<br />

/**<br />

* Implements awesomeness with hook_preprocess_username().<br />

*/<br />

function retro_preprocess_username(&$variables) {<br />

$variables['link_path'] = $variables['account']->geocities_url;<br />

}<br />

That's it! We don't have to override the user module's theme implementation; we just<br />

modify its parameters.<br />

Theme overrides<br />

While module developers usually don't have to worry about whether a theme<br />

overrides a particular theme function or not, it's still important to understand how<br />

this mechanism works.<br />

[ 69 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!