drupal 用户名超链接缺少站点 url - 来自 user_load($node->uid) 的 ahref
drupal username hyperlink missing site url - ahref from user_load($node->uid)
我正在使用 drupal FAQ 模块,它会向管理员 including the authors username as hyperlink 发送电子邮件,定义如下:
'creator' => theme('username', array('account' => user_load($node->uid), 'plain' => TRUE)),
不幸的是它只链接到 /users/joeblock 因此 缺少站点 url https://example.com这意味着它不适用于电子邮件。
<a href="/users/joeblock" title="View user profile." class="username">Joe Block</a>
我已经尝试过模块 pathologic hoping it adds the site url but didn't help (perhaps because the rendered ahref includes a / infront of it).
是否可以仅针对此实例修改超链接以插入站点url?
更新:
将 $variables['link_options']['absolute'] = true;
添加到 includes/theme.inc 有效。
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';
$variables['link_options']['absolute'] = true;
$output = l($variables['name'] . $variables['extra'], $variables['link_path'], $variables['link_options']);
}
是的,有可能!常见问题解答模块使用主题用户名。这个主题定义在 includes/theme.inc function theme_username
在您的自定义主题中,您可以实现 template_process_username 挂钩并更改 $variables 数组。
主题用户名使用url函数创建url。此函数接受绝对属性以构建绝对 url。
要创建此函数,您可以创建自定义主题 https://www.drupal.org/docs/7/theming/howto/create-a-new-custom-theme-with-css-alone 并将 yourthemename_process_username 函数放入自定义主题的 template.php 文件中。
否则,您可以在自定义模块中添加该功能。
让我们用自定义模块(使用 markus 名称)做一个例子,因为创建自定义模块比自定义主题更常见。
创建 site/all/modules/custom/markus 目录。
在此目录中创建包含以下内容的 markus.module 文件:
<?php
function markus_node_presave($node){
if( $node->type == 'faq' ){
drupal_static('markus_faq_node_save', true);
}
}
function markus_process_username( &$variables ){
if( drupal_static('markus_faq_node_save', false) ){
// alter the link_options only when you came from the ask module otherwise, without
// this if, all the username links in drupal will be absolute url.
// Actually this is not a problem but it may be overkilling
$variables['link_options']['absolute'] = true;
}
}
在 markus 目录中创建 markus.info 文件,内容如下:
name = markus
description = "my custom module"
core = 7.x
package = "markus"
现在从管理菜单启用您的主题。
最好在自定义模块中实现 markus_process_username 功能,而不是编辑 includes/theme.inc 文件,因为这样可以更轻松地更新 drupal。永远不要编辑 drupal 核心 :)
我正在使用 drupal FAQ 模块,它会向管理员 including the authors username as hyperlink 发送电子邮件,定义如下:
'creator' => theme('username', array('account' => user_load($node->uid), 'plain' => TRUE)),
不幸的是它只链接到 /users/joeblock 因此 缺少站点 url https://example.com这意味着它不适用于电子邮件。
<a href="/users/joeblock" title="View user profile." class="username">Joe Block</a>
我已经尝试过模块 pathologic hoping it adds the site url but didn't help (perhaps because the rendered ahref includes a / infront of it).
是否可以仅针对此实例修改超链接以插入站点url?
更新:
将 $variables['link_options']['absolute'] = true;
添加到 includes/theme.inc 有效。
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';
$variables['link_options']['absolute'] = true;
$output = l($variables['name'] . $variables['extra'], $variables['link_path'], $variables['link_options']);
}
是的,有可能!常见问题解答模块使用主题用户名。这个主题定义在 includes/theme.inc function theme_username
在您的自定义主题中,您可以实现 template_process_username 挂钩并更改 $variables 数组。 主题用户名使用url函数创建url。此函数接受绝对属性以构建绝对 url。
要创建此函数,您可以创建自定义主题 https://www.drupal.org/docs/7/theming/howto/create-a-new-custom-theme-with-css-alone 并将 yourthemename_process_username 函数放入自定义主题的 template.php 文件中。 否则,您可以在自定义模块中添加该功能。
让我们用自定义模块(使用 markus 名称)做一个例子,因为创建自定义模块比自定义主题更常见。 创建 site/all/modules/custom/markus 目录。 在此目录中创建包含以下内容的 markus.module 文件:
<?php
function markus_node_presave($node){
if( $node->type == 'faq' ){
drupal_static('markus_faq_node_save', true);
}
}
function markus_process_username( &$variables ){
if( drupal_static('markus_faq_node_save', false) ){
// alter the link_options only when you came from the ask module otherwise, without
// this if, all the username links in drupal will be absolute url.
// Actually this is not a problem but it may be overkilling
$variables['link_options']['absolute'] = true;
}
}
在 markus 目录中创建 markus.info 文件,内容如下:
name = markus
description = "my custom module"
core = 7.x
package = "markus"
现在从管理菜单启用您的主题。
最好在自定义模块中实现 markus_process_username 功能,而不是编辑 includes/theme.inc 文件,因为这样可以更轻松地更新 drupal。永远不要编辑 drupal 核心 :)