dpa31905 2018-03-22 12:48
浏览 70
已采纳

drupal用户名超链接缺少站点url - 来自user_load的ahref($ node-> uid)

I'm using the drupal FAQ module which sends an email to the admin including the authors username as hyperlink, defined via:

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

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

unfortunately it only links to /users/joeblock and thus missing the site url https://example.com which means it won't work in emails.

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

I already tried the module pathologic hoping it adds the site url but didn't help (perhaps because the rendered ahref includes a / infront of it).

Is it possible to modify the hyperlink just for this instance to insert the siteurl?

Update: adding $variables['link_options']['absolute'] = true;into includes/theme.inc worked.

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']);
      }
  • 写回答

1条回答 默认 最新

  • dongyu2764 2018-03-23 12:07
    关注

    Yes, it's possible! the faq module uses the theme username. This theme is defined in includes/theme.inc function theme_username

    In your custom theme you can implement the template_process_username hook and alter the $variables array. The theme username uses the url function to create the url. This function accepts the absolute attribute to build an absolute url.

    to create this function you can create a custom theme https://www.drupal.org/docs/7/theming/howto/create-a-new-custom-theme-with-css-alone and put the yourthemename_process_username function inside the template.php file of your custom theme. Otherwise you can add the function in a custom module.

    Let's do an example with a custom module (with the markus name) because is much more common to create a custom module than a custom theme. Create the site/all/modules/custom/markus directory. Inside this directory create the markus.module file with this content:

    <?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;
        }
    }
    

    create the markus.info file inside the markus directory with this content:

    name = markus
    description = "my custom module"
    core = 7.x
    package = "markus"
    

    Now from the admin menu enable your theme.

    It's better to implement the markus_process_username function in a custom module and not to edit the includes/theme.inc file because in this way you can update drupal much more easly. The drupal core should never be edited :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘