0

0

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

php中文网

php中文网

发布时间:2016-07-14 10:10:13

|

932人浏览过

|

来源于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免费学习笔记(深入)”;

Cogniflow
Cogniflow

Cogniflow是一个无代码AISaas解决方案,允许用户创建和部署AI模型,

下载

[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;
}
 
'>

 

立即学习PHP免费学习笔记(深入)”;

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/477543.htmlTechArticle今天遇到了drupal输出一个用户名的问题,使用的是drupal的主题函数theme输出的,于是查资料搜索,摸索出来以下一些内容: 1、theme(username,...

相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

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

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

智谱清言 - 免费全能的AI助手
智谱清言 - 免费全能的AI助手

智谱清言 - 免费全能的AI助手

相关专题

更多
batoto漫画官网入口与网页版访问指南
batoto漫画官网入口与网页版访问指南

本专题系统整理batoto漫画官方网站最新可用入口,涵盖最新官网地址、网页版登录页面及防走失访问方式说明,帮助用户快速找到batoto漫画官方平台,稳定在线阅读各类漫画内容。

58

2026.02.25

Steam官网正版入口与注册登录指南_新手快速进入游戏平台方法
Steam官网正版入口与注册登录指南_新手快速进入游戏平台方法

本专题系统整理Steam官网最新可用入口,涵盖网页版登录地址、新用户注册流程、账号登录方法及官方游戏商店访问说明,帮助新手玩家快速进入Steam平台,完成注册登录并管理个人游戏库。

6

2026.02.25

TypeScript全栈项目架构与接口规范设计
TypeScript全栈项目架构与接口规范设计

本专题面向全栈开发者,系统讲解基于 TypeScript 构建前后端统一技术栈的工程化实践。内容涵盖项目分层设计、接口协议规范、类型共享机制、错误码体系设计、接口自动化生成与文档维护方案。通过完整项目示例,帮助开发者构建结构清晰、类型安全、易维护的现代全栈应用架构。

7

2026.02.25

Python数据处理流水线与ETL工程实战
Python数据处理流水线与ETL工程实战

本专题聚焦 Python 在数据工程场景下的实际应用,系统讲解 ETL 流程设计、数据抽取与清洗、批处理与增量处理方案,以及数据质量校验与异常处理机制。通过构建完整的数据处理流水线案例,帮助开发者掌握数据工程中的性能优化思路与工程化规范,为后续数据分析与机器学习提供稳定可靠的数据基础。

0

2026.02.25

Java领域驱动设计(DDD)与复杂业务建模实战
Java领域驱动设计(DDD)与复杂业务建模实战

本专题围绕 Java 在复杂业务系统中的建模与架构设计展开,深入讲解领域驱动设计(DDD)的核心思想与落地实践。内容涵盖领域划分、聚合根设计、限界上下文、领域事件、贫血模型与充血模型对比,并结合实际业务案例,讲解如何在 Spring 体系中实现可演进的领域模型架构,帮助开发者应对复杂业务带来的系统演化挑战。

0

2026.02.25

Golang 生态工具与框架:扩展开发能力
Golang 生态工具与框架:扩展开发能力

《Golang 生态工具与框架》系统梳理 Go 语言在实际工程中的主流工具链与框架选型思路,涵盖 Web 框架、RPC 通信、依赖管理、测试工具、代码生成与项目结构设计等内容。通过真实项目场景解析不同工具的适用边界与组合方式,帮助开发者构建高效、可维护的 Go 工程体系,并提升团队协作与交付效率。

18

2026.02.24

Golang 性能优化专题:提升应用效率
Golang 性能优化专题:提升应用效率

《Golang 性能优化专题》聚焦 Go 应用在高并发与大规模服务中的性能问题,从 profiling、内存分配、Goroutine 调度、GC 机制到 I/O 与锁竞争逐层分析。结合真实案例讲解定位瓶颈的方法与优化策略,帮助开发者建立系统化性能调优思维,在保证代码可维护性的同时显著提升服务吞吐与稳定性。

9

2026.02.24

Golang 面试题精选:高频问题与解答
Golang 面试题精选:高频问题与解答

Golang 面试题精选》系统整理企业常见 Go 技术面试问题,覆盖语言基础、并发模型、内存与调度机制、网络编程、工程实践与性能优化等核心知识点。每道题不仅给出答案,还拆解背后的设计原理与考察思路,帮助读者建立完整知识结构,在面试与实际开发中都能更从容应对复杂问题。

5

2026.02.24

Golang 运行与部署实战:从本地到云端
Golang 运行与部署实战:从本地到云端

《Golang 运行与部署实战》围绕 Go 应用从开发完成到稳定上线的完整流程展开,系统讲解编译构建、环境配置、日志与配置管理、容器化部署以及常见运维问题处理。结合真实项目场景,拆解自动化构建与持续部署思路,帮助开发者建立可靠的发布流程,提升服务稳定性与可维护性。

5

2026.02.24

热门下载

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

精品课程

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

共142课时 | 7.5万人学习

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

共34课时 | 5.4万人学习

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

共20课时 | 1.5万人学习

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

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