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

CommentFileSizeAuthor
#9 link_display.PNG1.93 KBadam_b
#9 link_preview.PNG27.62 KBadam_b
#9 link_entry.PNG1.95 KBadam_b

Comments

jmarkantes’s picture

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

mjpg’s picture

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);
  }
}
summit’s picture

Bookmarking, seems the way to go! Greetings, Martijn

eL’s picture

in contemplate or tpl.php or in other php code, we can use simple and exactly what we need function:

http://php.net/manual/en/function.parse-url.php

But, Link module definitively should have this function build in and admin only choose - output only HOST name.

ntg’s picture

Thank you mjpg. I was looking around for hours till I found your solution which worked perfectly for me.

David D’s picture

I am trying to achieve this with Link 7.x-1.0, and neither of the function codes above seem to do anything. Is there some change needed for this to work with D7?

Thank you!

Hess’s picture

Absolutely not the nicest or best solution, but I needed same thing really fast.
link.module file and i changed this:

Around line 475
$item['title'] = empty($title) ? str_replace('http://', '', $item['display_url']) : $title;

dqd’s picture

Title: How to output link text without http:// » Option for http://link input to link text without http:// missing
Component: Miscellaneous » Code

I am trying to achieve this with Link 7.x-1.0, and neither of the function codes above seem to do anything. Is there some change needed for this to work with D7?

David D, in D7 a link stored as 'www.domain.org.uk' appears as:

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

so I don't know what you try to achieve?

adam_b’s picture

StatusFileSize
new1.95 KB
new27.62 KB
new1.93 KB

in D7 a link stored as 'www.domain.org.uk' appears as:
<a href="http://www.domain.org.uk">www.domain.org.uk</a>

Not in my experience with 7.x-1.0 - see attached example.

Sinan Erdem’s picture

Version: 6.x-2.6 » 7.x-1.1

I can confirm I have the same results with adam_b.

I am using 7.x-1.1.

dqd’s picture

Issue summary: View changes
Status: Active » Closed (cannot reproduce)

Still not sure what this issue is about. Even after 12 years. The link field has the option to take different input for title and link which can be used when link and display needs to be different. Apart from that: Link module is in Drupal core now. I'll close this issue due to inactivity for 12 years and upcoming EOL of Drupal 7 in January 2025. While the project version for Drupal 7 will keep providing support for security issues and website breaking bug fixes for Drupal 7, we should try to minimize the open issues for Drupal 7 in the queue as much as possible. Thanks for the report and all the efforts in here. Feel free to re-open if you can provide a fix or found an additional related problem which increases the issue priority to "Critical" here.