문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top