Drupal output a user name using drupal theme function

Source: Internet
Author: User
Tags drupal
Today, I encountered the problem of using drupal to output a user name. I used drupal's theme function theme to output it. so I searched for information and found out the following content: 1. theme (& #39; username & #39;, array (& #39; account & #39 ;=> $ log) call the function, parameter 1... "/> <scripttype =" t

Today, I encountered the problem of using drupal to output a user name, which was output by the theme function of drupal. so I searched for the information and found out the following content:


1. theme ('username', array ('account' => $ log) calls a function. parameter 1 is hook, and parameter 2 is parameter

The code is mostly unavailable. view the address and click the open link.


2. during execution, go back to the template preprocessing function and the processing function are template_preprocess_username (& $ variables) and template_process_username (& $ variables) in theme. inc.
 

[Php]
/**
* Preprocesses variables for theme_username ().
*
* Modules that make any changes to variables like 'name' or 'Extra 'must insure
* That the final string is 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 that is safe for printing and
// That won't break 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'] is 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 _ bubutes '] = array ();
// Populate link path and attributes if appropriate.
If ($ variables ['uid'] & $ variables ['Profile _ access']) {
// We are linking to a local user.
$ Variables ['link _ bubutes '] = 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
// Space-separated set of values, so initialize it as an array to make it
// Easier for other preprocess functions to append to it.
$ Variables ['link _ bubutes '] = array ('Rel' => array ('nofollow '));
$ Variables ['link _ path'] = $ account-> homepage;
$ Variables ['homepage'] = $ account-> homepage;
}
// We do not want the l () function to check_plain () a second time.
$ Variables ['link _ options'] ['html'] = TRUE;
// Set a default class.
$ Variables ['bubutes _ 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 done in the process phase so that attributes may be added
// Modules or the theme during the preprocess phase.
If (isset ($ variables ['link _ path']) {
// $ Variables ['bubutes _ array'] contains attributes that shoshould be applied
// Regardless of whether a link is being rendered or not.
// $ Variables ['link _ bubutes '] contains attributes that shoshould only be
// Applied if a link is being rendered. Preprocess functions are encouraged
// To use 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 are
// Themselves arrays, so the merging needs to be recursive.
$ Variables ['link _ options'] ['bubuckets'] = array_merge_recursive ($ variables ['link _ bubutes '], $ variables ['attributes _ array']);
}
}

/**
* Preprocesses variables for theme_username ().
*
* Modules that make any changes to variables like 'name' or 'Extra 'must insure
* That the final string is 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 that is safe for printing and
// That won't break 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'] is 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 _ bubutes '] = array ();
// Populate link path and attributes if appropriate.
If ($ variables ['uid'] & $ variables ['Profile _ access']) {
// We are linking to a local user.
$ Variables ['link _ bubutes '] = 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
// Space-separated set of values, so initialize it as an array to make it
// Easier for other preprocess functions to append to it.
$ Variables ['link _ bubutes '] = array ('Rel' => array ('nofollow '));
$ Variables ['link _ path'] = $ account-> homepage;
$ Variables ['homepage'] = $ account-> homepage;
}
// We do not want the l () function to check_plain () a second time.
$ Variables ['link _ options'] ['html'] = TRUE;
// Set a default class.
$ Variables ['bubutes _ 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 done in the process phase so that attributes may be added
// Modules or the theme during the preprocess phase.
If (isset ($ variables ['link _ path']) {
// $ Variables ['bubutes _ array'] contains attributes that shoshould be applied
// Regardless of whether a link is being rendered or not.
// $ Variables ['link _ bubutes '] contains attributes that shoshould only be
// Applied if a link is being rendered. Preprocess functions are encouraged
// To use 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 are
// Themselves arrays, so the merging needs to be recursive.
$ Variables ['link _ options'] ['bubuckets'] = array_merge_recursive ($ variables ['link _ bubutes '], $ variables ['attributes _ array']);
}
}
 

3. after processing these variables, the theme_username ($ variables) is implemented );

 

[Php]
Function theme_username ($ variables ){
If (isset ($ variables ['link _ path']) {
// We have a link path, so we shoshould generate a link using l ().
// Additional classes may be added as array elements like
// $ Variables ['link _ options'] ['bubuckets'] ['class'] [] = 'myclass ';
$ Output = l ($ variables ['name']. $ variables ['Extra '], $ variables ['link _ path'], $ variables ['link _ options']);
}
Else {
// Modules may have added important attributes so they must be supported ded
// In the output. Additional classes may be added as array elements like
// $ Variables ['tes Butes _ array'] ['class'] [] = 'myclass ';
$ Output = ''. $ variables ['name']. $ variables ['Extra '].'';
}
Return $ output;
}

Function theme_username ($ variables ){
If (isset ($ variables ['link _ path']) {
// We have a link path, so we shoshould generate a link using l ().
// Additional classes may be added as array elements like
// $ Variables ['link _ options'] ['bubuckets'] ['class'] [] = 'myclass ';
$ Output = l ($ variables ['name']. $ variables ['Extra '], $ variables ['link _ path'], $ variables ['link _ options']);
}
Else {
// Modules may have added important attributes so they must be supported ded
// In the output. Additional classes may be added as array elements like
// $ Variables ['tes Butes _ array'] ['class'] [] = 'myclass ';
$ Output = ''. $ variables ['name']. $ variables ['Extra '].'';
}
Return $ output;
}
 

 

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.