Posted by mjpg on July 25, 2009 at 10:19pm
10 followers
| Project: | Link |
| Version: | 7.x-1.1 |
| Component: | Code |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Issue Summary
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
Comments
#1
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
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
Bookmarking, seems the way to go! Greetings, Martijn
#4
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.
#5
Thank you mjpg. I was looking around for hours till I found your solution which worked perfectly for me.
#6
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!
#7
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;
#8
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?
#9
Not in my experience with 7.x-1.0 - see attached example.
#10
I can confirm I have the same results with adam_b.
I am using 7.x-1.1.