function link_field_formatter in Link 5
Implementation of hook_field_formatter().
File
- ./
link.module, line 594 - Defines simple link field types.
Code
function link_field_formatter($field, $item, $formatter, $node) {
if (empty($item['url']) && ($field['url'] != 'optional' || empty($item['title']))) {
return '';
}
if ($formatter == 'plain') {
return empty($item['url']) ? check_plain($item['title']) : check_plain(link_cleanup_url($item['url']));
}
// Replace URL tokens.
if (module_exists('token') && $field['enable_tokens']) {
// Load the node if necessary for nodes in views.
$token_node = isset($node->nid) ? node_load($node->nid) : $node;
$item['url'] = token_replace($item['url'], 'node', $token_node);
}
$type = link_validate_url($item['url']);
$url = link_cleanup_url($item['url']);
// Separate out the anchor if any.
if (strpos($url, '#') !== FALSE) {
$fragment = substr($url, strpos($url, '#') + 1);
$url = substr($url, 0, strpos($url, '#'));
}
// Separate out the query string if any.
if (strpos($url, '?') !== FALSE) {
$query = substr($url, strpos($url, '?') + 1);
$url = substr($url, 0, strpos($url, '?'));
}
// Create a display URL.
$display_url = $type == LINK_EMAIL ? str_replace('mailto:', '', $url) : url($url, $query, $fragment, TRUE);
if ($field['display']['url_cutoff'] && strlen($display_url) > $field['display']['url_cutoff']) {
$display_url = substr($display_url, 0, $field['display']['url_cutoff']) . "...";
}
// Build a list of attributes.
$attributes = array();
$item['attributes'] = unserialize($item['attributes']);
// Add attributes defined at the widget level.
if (!empty($item['attributes']) && is_array($item['attributes'])) {
foreach ($item['attributes'] as $attribute => $attbvalue) {
if (isset($item['attributes'][$attribute]) && $field['attributes'][$attribute] == 'user') {
$attributes[$attribute] = $attbvalue;
}
}
}
// Add attributes defined at the field level.
if (is_array($field['attributes'])) {
foreach ($field['attributes'] as $attribute => $attbvalue) {
if (!empty($attbvalue) && $attbvalue != 'default' && $attbvalue != 'user') {
$attributes[$attribute] = $attbvalue;
}
}
}
// Remove the rel=nofollow for internal links.
if ($type != LINK_EXTERNAL && isset($attributes['rel']) && strpos($attributes['rel'], 'nofollow') !== FALSE) {
$attributes['rel'] = str_replace('nofollow', '', $attributes['rel']);
if (empty($attributes['rel'])) {
unset($attributes['rel']);
}
}
// Give the link the title 'Link'.
if ($formatter == 'short') {
$output = l(t('Link'), $url, $attributes, $query, $fragment);
}
elseif ($formatter == 'label') {
$output = l(t($field['widget']['label']), $url, $attributes, $query, $fragment);
}
elseif ($formatter == 'url') {
$output = l($display_url, $url, $attributes, $query, $fragment);
}
elseif ($formatter == 'separate') {
$title = check_plain(_link_field_formatter_title($field, $item, $node));
$class = empty($field['attributes']['class']) ? '' : ' ' . $field['attributes']['class'];
unset($field['attributes']['class']);
$output = '';
$output .= '<div class="link-item' . $class . '">';
$output .= '<div class="link-title">' . $title . '</div>';
$output .= '<div class="link-url">' . l($display_url, $url, $attributes, $query, $fragment, FALSE, $item['html']) . '</div>';
$output .= '</div>';
}
elseif (strlen(trim($item['title'])) || $field['title'] == 'value' && strlen(trim($field['title_value']))) {
$title = _link_field_formatter_title($field, $item, $node);
if (empty($url) && !empty($title)) {
$output = check_plain($title);
}
else {
$output = l($title, $url, $attributes, $query, $fragment, FALSE, $item['html']);
}
}
else {
$output = l($display_url, $url, $attributes, $query, $fragment);
}
return $output;
}