Add or Change Text on any Drupal page

I just worked on an old 4.6.x site and had to add text to the user page. This was a challenging task on 4.6.x, so I thought I’d write about the various ways one can add text to a Drupal page, and about this specific solution.

  1. hook_form_alter. In any custom module, you can write a yourmodule_form_alter function that gives you the opportunity to modify any Drupal form. I generally first create a new function with only

    print $form_id;

    in the body, then visit the page I want to modify to get the name of the form I want to alter. Then go back to your code and put your hook code inside:

    if ($form_id == '')

    Forms API (FAPI) is very powerful. You can modify form elements (change a textarea to a select box), add form elements, add HTML markup (‘#type’ => ‘markup’) elements, add #prefix and #suffix directives to existing elements, re-order elements (using #weight, or even array_splice and array_merge), and add #theme to theme elements. Take note of this last suggestion, adding #theme to form elements is very handy; it separates your display from your logic and as such is considered good form. FAPI has been around since Drupal 4.7.x days and was the major hassle in upgrading modules from 4.6.x to 4.7.x. But what it buys us was well worth the upgrade!

  2. theme_ functions. Any funciton in any code that starts with theme_ can be overriden in your theme by creating a yourtheme_ function of the same name. Since 4.7.x days and FAPI, you can also override the formatting of any form (if the form name is system_modules, then the theme function is theme_system_modules).
  3. content area blocks. You can create a block (admin/build/block) and add it to the content area of your page. Depending on your theme, you might be able to add it to the top or the bottom of the content area. Using this technique, you can add help text above or below any generated page.
  4. locale strings. In 4.6.x days and before, developers frequently enabled the locale module, created an English translation, and used this to modify the strings. This was a big hack and is not recommended.
  5. theme _phptemplate_variables. As you can tell, all of the solutions above were either for 4.7.x or a “big hack”. So, my final solution for modifying the text on any page was to use the phptemplate theme function _phptemplate_variables. This function is called for every block and page with a set of variables that will be sent to the theming functions which allows you to modify the variable values before they are sent to the .tpl.php files. Here’s my solution:
    function _phptemplate_variables($hook, $vars = array()) {
      switch ($hook) {
        case 'page':
          if (arg(0) == 'user') {
            global $user;
            if ($user->uid == 0) {
              $match = t('Enter your Shift In Action username.');
              $replace = $match .' '. t('Most IONS user names are First Last (with space in-between). You can request an reminder email with your password by clicking the "Request New Password" tab.');
    ['content'] = preg_replace('/'. $match .'/', $replace, $vars['content']); } } break; } return $vars; }

As you can see, Drupal gives you many ways to modify a page, without modifying the core files, some more elegant than others. When possible use FAPI and/or a theme function. Also consider using a #theme rather than #prefix and #suffix. But as a last resort, you can use _phptemplate_variables.

2017-03-31T06:20:58+00:00 Categories: Drupal|

Share this

About the Author: