0

0

drupal输出一个用户名的问题 用drupal的主题函数theme输出

php中文网

php中文网

发布时间:2016-06-13 10:56:14

|

948人浏览过

|

来源于php中文网

原创

 今天遇到了drupal输出一个用户名的问题,使用的是drupal的主题函数theme输出的,于是查资料搜索,摸索出来以下一些内容:


  1、theme('username', array('account' => $log))调用函数, 参数1为hook,  参数2为参数

        代码比较多不展出了,查看地址点击打开链接


  2、执行期间回去找模版的预处理函数和处理函数分别是 template_preprocess_username(&$variables)和template_process_username(&$variables)  这两个函数在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_attributes'] = array(); 
  // Populate link path and attributes if appropriate.  
  if ($variables['uid'] && $variables['profile_access']) { 
    // We are 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 an array to make it  
    // easier for other preprocess functions to append to it.  
    $variables['link_attributes'] = 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['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 done in the process phase so that attributes may be 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 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']['attributes'] = array_merge_recursive($variables['link_attributes'], $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_attributes'] = array();
  // Populate link path and attributes if appropriate.
  if ($variables['uid'] && $variables['profile_access']) {
    // We are 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 an array to make it
    // easier for other preprocess functions to append to it.
    $variables['link_attributes'] = 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['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 done in the process phase so that attributes may be 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 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']['attributes'] = array_merge_recursive($variables['link_attributes'], $variables['attributes_array']);
  }
}
 

  3、处理完这些变量后接下来就到了主题函数了,这里实现了最终html的拼装与输出 theme_username($variables);

 

php商城系统
php商城系统

PHP商城系统是国内功能优秀的网上商城系统,同时也是一个商业的PHP开发框架,有多套免费模版,强大的后台管理功能,专业的网上商城系统解决方案,快速建设网上购物商城、数码商城、手机商城、办公用品商城等网站。 php商城系统v3.0 rc6升级 1、主要修复用户使用中出现的js未加载完报错问题,后台整改、以及后台栏目的全新部署、更利于用户体验。 2、扩展出,更多系统内部的功能,以便用户能够迅速找到需

下载

[php]
function theme_username($variables) { 
  if (isset($variables['link_path'])) { 
    // We have a link path, so we should generate a link using l().  
    // Additional classes may be 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 have added important attributes so they must be included  
    // in the output. Additional classes may be 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, so we should generate a link using l().
    // Additional classes may be 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 have added important attributes so they must be included
    // in the output. Additional classes may be added as array elements like
    // $variables['attributes_array']['class'][] = 'myclass';
    $output = '' . $variables['name'] . $variables['extra'] . '';
  }
  return $output;
}
 
'>

 

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Word 字间距调整方法汇总
Word 字间距调整方法汇总

本专题整合了Word字间距调整方法,阅读下面的文章了解更详细操作。

2

2025.12.24

任务管理器教程
任务管理器教程

本专题整合了任务管理器相关教程,阅读下面的文章了解更多详细操作。

2

2025.12.24

AppleID格式
AppleID格式

本专题整合了AppleID相关内容,阅读专题下面的文章了解更多详细教程。

0

2025.12.24

csgo视频观看入口合集
csgo视频观看入口合集

本专题整合了csgo观看入口合集,阅读下面的文章了知道更多入口地址。

29

2025.12.24

yandex外贸入口合集
yandex外贸入口合集

本专题汇总了yandex外贸入口地址,阅读下面的文章了解更多内容。

58

2025.12.24

添加脚注通用方法
添加脚注通用方法

本专题整合了添加脚注方法合集,阅读专题下面的文章了解更多内容。

1

2025.12.24

重启电脑教程汇总
重启电脑教程汇总

本专题整合了重启电脑操作教程,阅读下面的文章了解更多详细教程。

3

2025.12.24

纸张尺寸汇总
纸张尺寸汇总

本专题整合了纸张尺寸相关内容,阅读专题下面的文章了解更多内容。

5

2025.12.24

Java Spring Boot 微服务实战
Java Spring Boot 微服务实战

本专题深入讲解 Java Spring Boot 在微服务架构中的应用,内容涵盖服务注册与发现、REST API开发、配置中心、负载均衡、熔断与限流、日志与监控。通过实际项目案例(如电商订单系统),帮助开发者掌握 从单体应用迁移到高可用微服务系统的完整流程与实战能力。

1

2025.12.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
XML教程
XML教程

共142课时 | 5.1万人学习

麻省理工大佬Python课程
麻省理工大佬Python课程

共34课时 | 4.8万人学习

Webpack4.x---十天技能课堂
Webpack4.x---十天技能课堂

共20课时 | 1.3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号