douren7179 2012-07-10 01:33
浏览 28
已采纳

Drupal6:将查询字符串附加到主链接

I'm trying to add a query string "?device=mobile" onto all primary links programmatically.

In my theme's page.tpl.php file I've tried the following,

<?php if (is_array($primary_links)) : ?>
    <?php foreach ($primary_links as $link): ?>
             $link['href'] = $link['href'].'?device=mobile';
    <?php endforeach; ?>
<?php endif; ?>

However this simply prints out the code onto the page. Currently I'm trying to use hook_menu_link_alter, but so far I haven't been successful. To test my code on just one primary link item I've tried the code below:

myModule_menu_link_alter(&$item, $map){
    $items['photo_gallery']['href'] = 'photo_gallery?device=mobile';
}

Unfortunately there was no change in the link. I'm also going to investigate hook_menu_item_link() from my template.php file, but at this point I'd like it if someone could point me in the right direction, and let me know what I've done wrong.

Thanks.

  • 写回答

1条回答 默认 最新

  • doudi1750 2012-07-10 02:11
    关注

    The code you're putting in your page.tpl.php has the right idea, but you're missing a couple of things:

    1. The body of the foreach loop should be surrounded by the PHP tags, so that PHP will interpret and execute the code. This is why you see that Drupal just "prints out the code": because you're putting it out of the PHP "world", so it simply becomes part of your template's HTML.
    2. Even if you correctly execute the code, you will not see any changes, because by default, the $link variable in your foreach loops is a copy of the original item in the array, so doing $link['href'] = 'stuff' won't modify the original. To modify the original, you can use the reference syntax, like: foreach ($primary_links as &$link). (Ampersand prefixed to the variable name, see PHP docs on references).
    3. And finally, even if you fix the previous two issues, it might still not work because the link's HREF attribute is probably going to be processed by theme('links') later, and your "?" and "=" are going to get encoded and it will break the link.

    So, fixing those three issues, I'd say you should modify your page.tpl.php code to look like:

    <?php if (is_array($primary_links)) : ?>
      <?php foreach ($primary_links as &$link): ?>                           
      <?php $link['query'] = array('device' => 'mobile'); ?>
      <?php endforeach; ?>                                                   
    <?php endif; ?>                                                          
    

    Or, if it annoys you to have to open/close the PHP on every line, just use a normal block like:

    <?php 
      if (is_array($primary_links)) {
        foreach ($primary_links as &$link) {
          $link['query'] = array('device' => 'mobile');
        }
      }
    ?>
    

    Note 1. The &$link syntax (use reference instead of copy), and 2. The query array key of the $link array, which is one of those "special" array keys that Drupal will search for, and, if found, utilize to build a proper URL query to attach to the final link (see the docs for Drupal's url() function).

    Also, remember to clear the caches whenever you see that "nothing changes", especially when working on a theme.

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

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集