There are many good ways to accomplish the same goals in Drupal, due to its flexible APIs and ease of development. That said, there can also be many wrong ways of approaching a problem, methods which are obviously wrong to experienced developers but which may seem like a quick & easy solution for an inexperienced developer. Lets be clear about this - doing things the wrong way will almost always lead to headache and grief in the long term, so it's best to avoid these. This new regular series, dubbed "You're Doing It Wrong" or #YDIW, will highlight some of the ways not to solve problems.
A frequent small task that Drupal developers find themselves needing to do is to change some of the interface text from either Drupal's core files or a contributed module. There are two very simple ways of doing this - the right way and the wrong way.
The Right Way?
Supposing a client has a site for promoting their products and wants to let people provide reviews directly on corresponding product pages (presuming some level of moderation). The client might then decide that instead of pages saying "Comments" and "Add new comment", they would prefer to have the pages say "Reviews" and "Add a new review".
The wrong way of changing built-in text in Drupal is to open up the original file and directly make the changes. This might not seem like a big deal, especially given that the two lines are in a single file (comment-wrapper.tpl.php) in Drupal 7. But what if the client didn't like the word "node" and wanted to change it to "article" or "post", like Wordpress uses, how many files would have to be changed for that?
The right way of changing text is via string overrides. Drupal has lots of support for full internationalization of your content & site text, but for minor changes you don't need to go through all of that headache and can instead use this simple method.
Step One - Finding the text
There are two steps to changing the text. The first step is search for the exact string you would like to replace. There are three quick things to keep in mind when looking for this string:
- The text must be wrapped in the t() function in order to be changeable, so if someone hardcoded the text into a module file you can't use these techniques; if you happen to find text that is not wrapped in t() you should submit an issue to the module's issue queue to have the module fixed, preferably with a patch file too :)
- The text is case-sensitive, so what is displayed as "comment" in one location will be a different string to "Comment" displayed in another location.
- You can only change the entire string that is passed to t(), so e.g. changing the word "comment" to "review" won't also automatically change "add new comment" to say "add new review", you must handle these as two separate strings.
In our example we would like to change the phrase "Add a new comment", which is on line 48 of the file "comment-wrapper.tpl.php" in Drupal 7 (correct as of Drupal 7.2, the line number may change in subsequent releases).
Step Two - Changing the text
Once the exact text has been found, there are two options for changing it.
Option #1: code
If you edit the settings.php file for your site and scroll down to near the end you'll find the following if your site is Drupal 6:
1 2 3 4
# $conf['locale_custom_strings_en'] = array( # 'forum' => 'Discussion board', # '@count min' => '@count minutes', # );
The settings.php file for Drupal 7 contains the following instead (note values are nested within an additional array):
1 2 3 4
# $conf['locale_custom_strings_en'][''] = array( # 'forum' => 'Discussion board', # '@count min' => '@count minutes', # );
If you uncomment those lines (remove the # symbols at the start of the lines) you'll now see the phrase "Discussion board" used in place of the single word "forum". For our examples above the resulting lines should be as follows:
1 2 3 4
$conf['locale_custom_strings_en'] = array( 'Comments' => 'Reviews', 'Add new comment' => 'Add new review', );
1 2 3 4
$conf['locale_custom_strings_en'][''] = array( 'Comments' => 'Reviews', 'Add new comment' => 'Add new review', );
If you'd prefer not to fiddle with the settings.php file for your site you might want to try option #2 instead.
Option #2: Module
As it happens there there's a module for making it super easy to change the string overrides right from the Drupal admin - the aptly named String Overrides module! If the thought of changing some lines in a PHP file eeks you out, you might want to try this simple module instead. Like any other module, simply download it to your sites/all/modules/contrib directory (you do put all of your contrib modules there, don't you?), enable the module and then go to its settings page where you'll see a very simple interface. Easy peasie!
- Changing text from the Drupal interface is easy.
- Find the text you wish to change, it'll be wrapped in a t() function call.
- Either add a string override line to the settings.php page, or..
- Use the String Overrides module.
Where this post came from
At Bluespark Labs we often are given the job of expanding upon existing work built by others. Because not everybody starts a project knowing how to properly work with Drupal, often times we will find some decisions made by the previous developers that make us scratch our heads. This particular issue arose because the original developer for one site we inherited was not aware of the string overrides system and hacked up several core files, and contrib modules, to make textual changes that were being asking for. Thankfully, now you know how to do it the right way.
Don't forget that this is the first of a new, ongoing series and there will be many more to come, so check back often!