In this article we will look at 7 Drupal API functions that are very helpful in the development of sites to Drupal 7.
check_plain($text) – re-encodes special characters to HTML entities.
Parameters:
- $text – the string for conversion
The return value: the processed string to display as HTML.
This function can be used to treat all kinds of data coming to the site from a variety of sources: user input, import data from another site, Twitter, etc.
t($string, array $args = array(), array $options = array()) – converts the string to the user-selected language.
Parameters:
- $string – the string to be translated
- $args – an associative array of wildcard patterns (placeholders)
- $options – an associative array of additional options, contains two possible options: langcode – a clear indication of the language code that you want to translate a string, context – allows you to set the context of translation.
The return value: the translated string.
Example of the function t():
t('Good afternoon, @first_name @last_name. ', array('@first_name' => 'Jhon', '@last_name' => 'Smith')); // Returns 'Good afternoon, Jhon Smith.'
There are three types of wildcard patterns:
- !name – value is substituted without processing
- @name – value processed by the function check_plain, all HTML tags are cut
- %name – value processed by the function theme_placeholder (also that check_plain, but the result is wrapped in a tag <em>)
format_plural($count, $singular, $plural, array $args = array(), array $options = array()) – creates a string containing quantitative value.
Parameters:
- $count – quantitative value
- $singular – a string that will be used if $count == 1
- $plural – a string that will be used if $count > 1
- $args – an associative array of of wildcard patterns (placeholders)
- $options – the same as in the t() function
The return value: a string translated by using the function t(), depending on the parameter $count selected string that will be used for translation.
Example:
$comment_count=1;
format_plural($comment_count, '1 comment', '@count comments'); // return '1 comment'
$comment_count=5;
format_plural($comment_count, '1 comment', '@count comments'); // return '5 comments'
drupal_get_title() – returns the current page title.
This function can be used in combination with drupal_set_title() to process the header and install a new one.
drupal_set_title($title = NULL, $output = CHECK_PLAIN) – sets the title of the page.
Parameters:
- $title – a string that will be used as the title of the page
- $output – a flag that determines whether the $title processed by function check_plain().
The return value: updated page title.
url($path = NULL, array $options = array()) – forms an internal or external URL.
Parameters:
- $ path – internal relative or external absolute path
- $ options – an associative array of options:
- query- array passed parameters such as key/value
- fragment – the anchor element on the page
- absolute – flag (default FALSE) if set to TRUE then the url will be defined as an absolute.
- alias – the flag (default FALSE) if set to TRUE then the path will be regarded as an alias (this will not be accomplished in the search for the alias database that will speed up)
- external – the flag, if set to TURE url will be regarded as external.
- language – a language object defines the language to find an alias selected language
- https – the flag is set to TRUE if the path will be https protocol, if FALSE, then the http.
- base_url – the value to replace the standard base path
- prefix – the prefix path language
The returned value: formed URL.
drupal_goto($path = ”, array $options = array(), $http_response_code = 302) – produces redirect the user to another page.
Parameters:
- $path – relative or absolute path to be produced redirect
- $options – the list of options as a function of the url()
- $http_response_code – code status code
This text is a translation of the article “7 полезных функций Drupal API который должен знать каждый!” published on drupal-learning.com.
Forums and blogs without spam
CleanTalk is a SaaS spam protection service for Web-sites. CleanTalk uses protection methods which are invisible for site visitors. Connecting to the service eliminates needs for CAPTCHA, questions and answers and other methods of protection, complicating the exchange of information on the site.