How to output link text without http://

mjpg - July 25, 2009 - 22:19
Project:Link
Version:6.x-2.6
Component:Miscellaneous
Category:support request
Priority:normal
Assigned:Unassigned
Status:active
Description

Link works great to create a link in a directory I am publishing.

A link stored as 'www.domain.org.uk' appears as:

<a href="http://www.domain.org.uk">http://www.domain.org.uk</a>

What is the best way to remove the 'http://' in the link text? - ie:

<a href="http://www.domain.org.uk">www.domain.org.uk</a>

Thanks

#1

jmarkantes - September 2, 2009 - 22:27

You can override the link theme. This is the original theme function for the basic "url" link:

/**
* Theme function for 'url' text field formatter.
*/
function theme_link_formatter_url($element) {
  return $element['#item']['url'] ? l($element['#item']['display_url'], $element['#item']['url'], $element['#item']) : '';
}

I put this function within my theme's template.php file:

function THEMENAME_link_formatter_url($element) {
$trimmed_url = substr($element['#item']['display_url'], 7);
  return $element['#item']['url'] ? l($trimmed_url, $element['#item']['url'], $element['#item']) : '';
}

replacing THEMENAME with your theme's name.

Good luck,
Jason

#2

mjpg - September 7, 2009 - 19:32

Thanks very much for this - it set me on the right path. In the end I used the following because of the way I have my title and url (static title set to [field_website-url]) set up and to allow for https urls:

/* Overriding Link module to remove 'http://' in link */
/**
* <a href="http://www.domain.org.uk">www.domain.org.uk</a>
* instead of:
* <a href="http://www.domain.org.uk">http://www.domain.org.uk</a>
*/

function THEMENAME_link_formatter_default($element) {
  // check if title has http or https in it
  $cleaned_title = preg_replace('/^http(s)?:\/\//i', '', $element['#item']['display_title']);
 
  // Display a normal link if both title and URL are available.
  if (!empty($element['#item']['display_title']) && !empty($element['#item']['url'])) {
    return l($cleaned_title, $element['#item']['url'], $element['#item']);
  }
  // If only a title, display the title.
  elseif (!empty($element['#item']['display_title'])) {
    return check_plain($cleaned_title);
  }
}

#3

Summit - December 19, 2009 - 17:39

Bookmarking, seems the way to go! Greetings, Martijn

 
 

Drupal is a registered trademark of Dries Buytaert.