开发者

Set link target=_blank for external links module

Is there any module in Drupal which will set targ开发者_JS百科et="_blank" for all external sites in


There's a module here which does it in javascript I think: http://drupal.org/project/extlink.

I wrote a similar module which does the same thing as a Drupal input filter. It's not released anywhere but I'm happy to share it if there's any reason why javascript doesn't work for you.


Not sure if there is a Drupal module, but this can be done easily using jquery:

$("a[href^='http:']").attr('target', '_blank');


Add some additional attributes for the menu link
Module name is: https://drupal.org/project/menu_attributes


Use a template to overwrite the formatting. e.g. field--field-external-link--article.tpl.php for article type and field field-external-link.

See $link_rendered.

<div class="<?php print $classes; ?>"<?php print $attributes; ?>>
  <?php if (!$label_hidden): ?>
    <div class="field-label"<?php print $title_attributes; ?>><?php print $label ?>:&nbsp;</div>
  <?php endif; ?>
  <div class="field-items"<?php print $content_attributes; ?>>
  <?php foreach ($items as $delta => $item): ?>
    <?php
     // Add target="_blank"
     $link_rendered =  preg_replace('/^<a/i', '<a target="_blank"', render($item));
    ?>
   <div class="field-item <?php print $delta % 2 ? 'odd' : 'even'; ?>"<?php print $item_attributes[$delta]; ?>><?php print $link_rendered; ?></div>
   <?php endforeach; ?>
 </div>

Breaks both rules a) edit xhtml with regex and b) editing in template rather than a hook. Could be changed to a module and hook call but wouldn't add to reusability.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜