From c5fbd1f4d1dd2d4f4e21246baaf92b7cd4f810bd Mon Sep 17 00:00:00 2001 From: Jonathan Hedstrom Date: Mon, 18 Jul 2011 10:50:27 -0700 Subject: [PATCH] - #535948: Add favicon next to external links. --- link.css | 5 +++++ link.module | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 0 deletions(-) diff --git a/link.css b/link.css index 7bdec9e..d826fee 100644 --- a/link.css +++ b/link.css @@ -6,3 +6,8 @@ div.link-field-column { div.link-field-column .form-text { width: 95%; } + +img.link-favicon { + margin-right: 0.5em; /* RTL */ + vertical-align: middle; +} \ No newline at end of file diff --git a/link.module b/link.module index 9e52de5..9707dbe 100644 --- a/link.module +++ b/link.module @@ -23,6 +23,11 @@ define('LINK_TARGET_USER', 'user'); define('LINK_URL_MAX_LENGTH', 2048); /** + * URL to retrieve favicons from if using the favicon formatter. + */ +define('LINK_FAVICON_URL', 'https://s2.googleusercontent.com/s2/favicons?alt=p&domain='); + +/** * Implements hook_field_info(). */ function link_field_info() { @@ -493,6 +498,9 @@ function link_theme() { 'link_formatter_link_default' => array( 'variables' => array('element' => NULL), ), + 'link_formatter_link_favicon' => array( + 'variables' => array('element' => NULL), + ), 'link_formatter_link_plain' => array( 'variables' => array('element' => NULL), ), @@ -619,6 +627,11 @@ function link_field_formatter_info() { 'field types' => array('link_field'), 'multiple values' => FIELD_BEHAVIOR_DEFAULT, ), + 'link_favicon' => array( + 'label' => t('Title, as link, with image'), + 'field types' => array('link_field'), + 'multiple values' => FIELD_BEHAVIOR_DEFAULT, + ), 'link_url' => array( 'label' => t('URL, as link'), 'field types' => array('link_field'), @@ -681,6 +694,31 @@ function theme_link_formatter_link_default($vars) { } /** + * Theme function for 'favicon' link field formatter. + */ +function theme_link_formatter_link_favicon($vars) { + drupal_add_css(drupal_get_path('module', 'link') .'/link.css'); + + $link_options = $vars['element']; + unset($link_options['element']['title']); + unset($link_options['element']['url']); + $url = parse_url($vars['element']['url']); + $favicon_url = LINK_FAVICON_URL . $url['host']; + $favicon = theme('image', array('path' => $favicon_url, 'width' => 16, 'height' => 16, 'attributes' => array('class' => 'link-favicon'))); + // Display a normal link if both title and URL are available. + if (!empty($vars['element']['title']) && !empty($vars['element']['url'])) { + return $favicon . l($vars['element']['title'], $vars['element']['url'], $link_options); + } + // If only a title, display the title. + elseif (!empty($vars['element']['title'])) { + return check_plain($vars['element']['title']); + } + elseif (!empty($vars['element']['url'])) { + return $favicon . l($vars['element']['title'], $vars['element']['url'], $link_options); + } +} + +/** * Theme function for 'plain' text field formatter. */ function theme_link_formatter_link_plain($vars) { -- 1.7.4.1