Drupal 用户名超链接缺少站点网址 - 来自 user_load($node->uid) 的 Ahref



我正在使用drupal FAQ模块,该模块向管理员发送电子邮件,包括作者用户名作为超链接,通过以下方式定义:

'creator' => theme('username', array('account' => user_load($node->uid), 'plain' => TRUE)),

http://cgit.drupalcode.org/faq_ask/tree/faq_ask.module?id=9f4fbb7859c8fc24977f5d67cd589685236e442d#n480

不幸的是,它只链接到/users/joeblock,因此缺少站点网址 https://example.com这意味着它无法在电子邮件中使用。

<a href="/users/joeblock" title="View user profile." class="username">Joe Block</a>

我已经尝试了模块病理,希望它添加站点网址但没有帮助(可能是因为渲染的 ahref 在其前面包含一个/)。

是否可以仅为此实例修改超链接以插入站点网址?

更新:$variables['link_options']['absolute'] = true;添加到包含/主题.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']);
}

是的,这是可能的! 常见问题解答模块使用主题用户名。此主题在 include/theme.inc 函数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函数放入自定义主题的模板.php文件中。 否则,您可以在自定义模块中添加该函数。

让我们用自定义模块(带有 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 函数,而不是编辑 include/theme.inc 文件,因为通过这种方式您可以更轻松地更新 drupal。永远不应该编辑drupal核心:)

最新更新