Drupal 7 - String Handling Basics
From comments to usernames, email addresses, URLs, blog entries, etc., modern websites are constantly dealing with strings of text. These strings could be generated in various languages (e.g. on international sites), and they could be carefully crafted by hackers in attempts to undermine the site's security. As a modern CMS, Drupal 7 has some fundamental APIs that provide developers with basic string handling capabilities.
The core of this code is found in "site_root/includes/bootstrap.inc". The first relevant function is used to perform language translation for module-generated text and is simply named t():
<?php
function t($string, array $args = array(), array $options = array()) {
global $language;
static $custom_strings;
// Merge in default.
if (empty($options['langcode'])) {
$options['langcode'] = isset($language->language) ? $language->language : 'en';
}
if (empty($options['context'])) {
$options['context'] = '';
}
// First, check for an array of customized strings. If present, use the array
// *instead of* database lookups. This is a high performance way to provide a
// handful of string replacements. See settings.php for examples.
// Cache the $custom_strings variable to improve performance.
if (!isset($custom_strings[$options['langcode']])) {
$custom_strings[$options['langcode']] = variable_get('locale_custom_strings_' . $options['langcode'], array());
}
// Custom strings work for English too, even if locale module is disabled.
if (isset($custom_strings[$options['langcode']][$options['context']][$string])) {
$string = $custom_strings[$options['langcode']][$options['context']][$string];
}
// Translate with locale module if enabled.
elseif ($options['langcode'] != 'en' && function_exists('locale')) {
$string = locale($string, $options['context'], $options['langcode']);
}
if (empty($args)) {
return $string;
}
else {
// Transform arguments before inserting them.
foreach ($args as $key => $value) {
switch ($key[0]) {
case '@':
// Escaped only.
$args[$key] = check_plain($value);
break;
case '%':
default:
// Escaped and placeholder.
$args[$key] = drupal_placeholder($value);
break;
case '!':
// Pass-through.
}
}
return strtr($string, $args);
}
}
?>The function is passed three arguments at most. The first argument is the actual string that is to be translated. The second (optional) argument is an $args array that contains replacement values for placeholders in the main string. The third (optional) argument is an $options array that can do things like override the language that the string is being translated into. Under normal circumstances this function will translate the input string to the language that the page is being rendered in. It will then replace any placeholders in the translated string with the appropriate values that are given in the $args array. This function is fundamental in providing Drupal with its multilingual capabilities.
Aside from translation, it is imperative to have the capability of generating properly formatted HTML text from strings. This is the domain of the short but sweet check_plain() function:
<?php
function check_plain($text) {
return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
}
?>As is obvious from the above code, check_plain() in Drupal 7 is just a wrapper for PHP's built-in htmlspecialchars() function, which turns items such as ampersands into their respective HTML strings (e.g. &).
These two functions provide the core of Drupal's string handling API. From what I can tell, the core locale module can provide much more extensive translation capabilities than is available with the t() function alone. However, I have yet to use this module.




Comments
Post new comment