Drupal output A user name problem with Drupal's theme function theme output _php tutorial

Source: Internet
Author: User
Tags drupal
Today, I met the problem of Drupal output a user name, using the Drupal theme function theme output, and then search the data to find out the following content:


1, Theme (' username ', array (' account ' = $log)) call function, parameter 1 is hook, parameter 2 is parameter

The code is more not exhibited, view the address click Open link


2, the implementation period back to find the template preprocessing functions and processing functions are Template_preprocess_username (& $variables) and Template_process_username (&$ variables) These two functions are in Theme.inc

[PHP]
/**
* Preprocesses Variables for theme_username ().
*
* Modules that do any changes to variables like ' name ' or ' extra ' must insure
* that the final string was safe to include directly in the output by using
* Check_plain () or FILTER_XSS ().
*
* @see Template_process_username ()
*/
Function Template_preprocess_username (& $variables) {
$account = $variables [' account '];

$variables [' extra '] = ';
if (Empty ($account->uid)) {
$variables [' uid '] = 0;
if (theme_get_setting (' toggle_comment_user_verification ')) {
$variables [' extra '] = ' ('. T (' not verified '). ')';
}
}
else {
$variables [' uid '] = (int) $account->uid;
}

Set the name to a formatted name, is safe for printing and
That won ' t breaks tables by being too long. Keep an unshortened,
unsanitized version, in case other preprocess functions want to implement
Their own shortening logic or add markup. If They do so, they must ensure
That $variables [' name '] was safe for printing.
$name = $variables [' name_raw '] = Format_username ($account);
if (Drupal_strlen ($name) > 20) {
$name = drupal_substr ($name, 0, 15). '...';
}
$variables [' name '] = Check_plain ($name);

$variables [' profile_access '] = user_access (' Access user profiles ');
$variables [' link_attributes '] = array ();
Populate link path and attributes if appropriate.
if ($variables [' uid '] && $variables [' profile_access ']) {
We is linking to a local user.
$variables [' link_attributes '] = Array (' title ' = + t (' View user profile. '));
$variables [' link_path '] = ' user/'. $variables [' uid '];
}
ElseIf (!empty ($account->homepage)) {
Like the ' class ' attribute, the ' rel ' attribute can hold a
space-separated set of values, so initialize it as a array to make it
Easier-preprocess functions to append to it.
$variables [' link_attributes '] = Array (' rel ' = = Array (' nofollow '));
$variables [' link_path '] = $account->homepage;
$variables [' homepage '] = $account->homepage;
}
We don't want the L () function to Check_plain () a second time.
$variables [' link_options '] [' html '] = TRUE;
Set a default class.
$variables [' attributes_array '] = Array (' class ' = = Array (' username '));
}

/**
* Processes Variables for theme_username ().
*
* @see Template_preprocess_username ()
*/
Function Template_process_username (& $variables) {
Finalize the link_options array for passing to the L () function.
This is the do in the process phase so, attributes may are added by
Modules or the theme during the preprocess phase.
if (Isset ($variables [' Link_path '])) {
$variables [' Attributes_array '] contains attributes that should be applied
Regardless of whether a link is being rendered or not.
$variables [' Link_attributes '] contains attributes that should only be
Applied if a link is being rendered. Preprocess functions is encouraged
The former unless they want to add attributes on the link only.
If a link is being rendered, these need to be merged. Some attributes is
themselves arrays, so the merging needs to be recursive.
$variables [' link_options '] [' attributes '] = array_merge_recursive ($variables [' link_attributes '], $variables [' Attributes_array ']);
}
}

/**
* Preprocesses Variables for theme_username ().
*
* Modules that do any changes to variables like ' name ' or ' extra ' must insure
* that the final string was safe to include directly in the output by using
* Check_plain () or FILTER_XSS ().
*
* @see Template_process_username ()
*/
Function Template_preprocess_username (& $variables) {
$account = $variables [' account '];

$variables [' extra '] = ';
if (Empty ($account->uid)) {
$variables [' uid '] = 0;
if (theme_get_setting (' toggle_comment_user_verification ')) {
$variables [' extra '] = ' ('. T (' not verified '). ')';
}
}
else {
$variables [' uid '] = (int) $account->uid;
}

Set the name to a formatted name, is safe for printing and
That won ' t breaks tables by being too long. Keep an unshortened,
unsanitized version, in case other preprocess functions want to implement
Their own shortening logic or add markup. If They do so, they must ensure
That $variables [' name '] was safe for printing.
$name = $variables [' name_raw '] = Format_username ($account);
if (Drupal_strlen ($name) > 20) {
$name = drupal_substr ($name, 0, 15). '...';
}
$variables [' name '] = Check_plain ($name);

$variables [' profile_access '] = user_access (' Access user profiles ');
$variables [' link_attributes '] = array ();
Populate link path and attributes if appropriate.
if ($variables [' uid '] && $variables [' profile_access ']) {
We is linking to a local user.
$variables [' link_attributes '] = Array (' title ' = + t (' View user profile. '));
$variables [' link_path '] = ' user/'. $variables [' uid '];
}
ElseIf (!empty ($account->homepage)) {
Like the ' class ' attribute, the ' rel ' attribute can hold a
space-separated set of values, so initialize it as a array to make it
Easier-preprocess functions to append to it.
$variables [' link_attributes '] = Array (' rel ' = = Array (' nofollow '));
$variables [' link_path '] = $account->homepage;
$variables [' homepage '] = $account->homepage;
}
We don't want the L () function to Check_plain () a second time.
$variables [' link_options '] [' html '] = TRUE;
Set a default class.
$variables [' attributes_array '] = Array (' class ' = = Array (' username '));
}

/**
* Processes Variables for theme_username ().
*
* @see Template_preprocess_username ()
*/
Function Template_process_username (& $variables) {
Finalize the link_options array for passing to the L () function.
This is the do in the process phase so, attributes may are added by
Modules or the theme during the preprocess phase.
if (Isset ($variables [' Link_path '])) {
$variables [' Attributes_array '] contains attributes that should be applied
Regardless of whether a link is being rendered or not.
$variables [' Link_attributes '] contains attributes that should only be
Applied if a link is being rendered. Preprocess functions is encouraged
The former unless they want to add attributes on the link only.
If a link is being rendered, these need to be merged. Some attributes is
themselves arrays, so the merging needs to be recursive.
$variables [' link_options '] [' attributes '] = array_merge_recursive ($variables [' link_attributes '], $variables [' Attributes_array ']);
}
}

3, after the processing of these variables next to the theme function, where the final HTML to achieve the assembly and output theme_username ($variables);

[PHP]
function Theme_username ($variables) {
if (Isset ($variables [' Link_path '])) {
We have a link path and so we should generate a link using L ().
Additional classes is added as array elements like
$variables [' link_options '] [' attributes '] [' class '] [] = ' MyClass ';
$output = L ($variables [' name ']. $variables [' Extra '], $variables [' Link_path '], $variables [' link_options ']);
}
else {
Modules may has added important attributes so they must is included
In the output. Additional classes is added as array elements like
$variables [' Attributes_array '] [' class '] [] = ' MyClass ';
$output = ' . $variables [' name ']. $variables [' Extra ']. '';
}
return $output;
}

function Theme_username ($variables) {
if (Isset ($variables [' Link_path '])) {
We have a link path and so we should generate a link using L ().
Additional classes is added as array elements like
$variables [' link_options '] [' attributes '] [' class '] [] = ' MyClass ';
$output = L ($variables [' name ']. $variables [' Extra '], $variables [' Link_path '], $variables [' link_options ']);
}
else {
Modules may has added important attributes so they must is included
In the output. Additional classes is added as array elements like
$variables [' Attributes_array '] [' class '] [] = ' MyClass ';
$output = ' . $variables [' name ']. $variables [' Extra ']. '';
}
return $output;
}

http://www.bkjia.com/PHPjc/477543.html www.bkjia.com true http://www.bkjia.com/PHPjc/477543.html techarticle today, I met the problem of Drupal output a user name, using the Drupal theme function theme output, and then search the data to find out the following some of the contents: 1, Theme (username,...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.