View source
<?php
define('LINK_EXTERNAL', 'external');
define('LINK_INTERNAL', 'internal');
define('LINK_FRONT', 'front');
define('LINK_EMAIL', 'email');
define('LINK_DOMAINS', 'aero|arpa|asia|biz|com|cat|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel|mobi|local');
function link_field_info() {
return array(
'link' => array(
'label' => t('Link'),
'description' => t('Store a title, href, and attributes in the database to assemble a link.'),
),
);
}
function link_field_settings($op, $field) {
switch ($op) {
case 'form':
$form = array(
'#theme' => 'link_field_settings',
);
$form['url'] = array(
'#type' => 'checkbox',
'#title' => t('Optional URL'),
'#default_value' => $field['url'],
'#return_value' => 'optional',
'#description' => t('If checked, the URL field is optional and submitting a title alone will be acceptable. If the URL is ommitted, the title will be displayed as plain text.'),
);
$title_options = array(
'optional' => t('Optional Title'),
'required' => t('Required Title'),
'value' => t('Static Title: '),
'none' => t('No Title'),
);
$form['title'] = array(
'#type' => 'radios',
'#title' => t('Link Title'),
'#default_value' => isset($field['title']) ? $field['title'] : 'optional',
'#options' => $title_options,
'#description' => t('If the link title is optional or required, a field will be displayed to the end user. If the link title is static, the link will always use the same title. If <a href="http://drupal.org/project/token">token module</a> is installed, the static title value may use any other node field as its value. Static and token-based titles may include most inline XHTML tags such as <em>strong</em>, <em>em</em>, <em>img</em>, <em>span</em>, etc.'),
);
$form['title_value'] = array(
'#type' => 'textfield',
'#default_value' => $field['title_value'],
'#size' => '46',
);
if (module_exists('token')) {
$form['tokens'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => t('Placeholder tokens'),
'#description' => t("The following placeholder tokens can be used in both paths and titles. When used in a path or title, they will be replaced with the appropriate values."),
);
$form['tokens']['help'] = array(
'#value' => theme('token_help', 'node'),
);
$form['enable_tokens'] = array(
'#type' => 'checkbox',
'#title' => t('Allow user-entered tokens'),
'#default_value' => isset($field['enable_tokens']) ? $field['enable_tokens'] : 1,
'#description' => t('Checking will allow users to enter tokens in URLs and Titles on the node edit form. This does not affect the field settings on this page.'),
);
}
$form['display'] = array(
'#tree' => TRUE,
);
$form['display']['url_cutoff'] = array(
'#type' => 'textfield',
'#title' => t('URL Display Cutoff'),
'#default_value' => isset($field['display']['url_cutoff']) ? $field['display']['url_cutoff'] : '80',
'#description' => t('If the user does not include a title for this link, the URL will be used as the title. When should the link title be trimmed and finished with an elipsis (…)? Leave blank for no limit.'),
'#maxlength' => 3,
'#size' => 3,
);
$target_options = array(
'default' => t('Default (no target attribute)'),
'_top' => t('Open link in window root'),
'_blank' => t('Open link in new window'),
'user' => t('Allow the user to choose'),
);
$form['attributes'] = array(
'#tree' => TRUE,
);
$form['attributes']['target'] = array(
'#type' => 'radios',
'#title' => t('Link Target'),
'#default_value' => empty($field['attributes']['target']) ? 'default' : $field['attributes']['target'],
'#options' => $target_options,
);
$form['attributes']['rel'] = array(
'#type' => 'textfield',
'#title' => t('Rel Attribute'),
'#description' => t('When output, this link will have this rel attribute. The most common usage is <a href="http://en.wikipedia.org/wiki/Nofollow">rel="nofollow"</a> which prevents some search engines from spidering entered links.'),
'#default_value' => empty($field['attributes']['rel']) ? '' : $field['attributes']['rel'],
'#field_prefix' => 'rel = "',
'#field_suffix' => '"',
'#size' => 20,
);
$form['attributes']['class'] = array(
'#type' => 'textfield',
'#title' => t('Additional CSS Class'),
'#description' => t('When output, this link will have have this class attribute. Multiple classes should be separated by spaces.'),
'#default_value' => empty($field['attributes']['class']) ? '' : $field['attributes']['class'],
);
return $form;
case 'validate':
if ($field['title'] == 'value' && empty($field['title_value'])) {
form_set_error('title_value', t('A default title must be provided if the title is a static value'));
}
break;
case 'save':
return array(
'attributes',
'display',
'url',
'title',
'title_value',
'enable_tokens',
);
case 'database columns':
return array(
'url' => array(
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
'sortable' => TRUE,
),
'title' => array(
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
'sortable' => TRUE,
),
'attributes' => array(
'type' => 'text',
'size' => 'medium',
'not null' => FALSE,
),
);
case 'views data':
module_load_include('inc', 'link', 'views/link.views');
return link_views_content_field_data($field);
}
}
function theme_link_field_settings($form) {
$title_value = drupal_render($form['title_value']);
$title_checkbox = drupal_render($form['title']['value']);
$form['title']['value'] = array(
'#value' => '<div class="container-inline">' . $title_checkbox . $title_value . '</div>',
);
return drupal_render($form);
}
function link_content_is_empty($item, $field) {
if (empty($item['title']) && empty($item['url'])) {
return TRUE;
}
return FALSE;
}
function link_field($op, &$node, $field, &$items, $teaser, $page) {
switch ($op) {
case 'load':
foreach ($items as $delta => $item) {
_link_load($items[$delta], $delta);
}
break;
case 'validate':
$optional_field_found = FALSE;
foreach ($items as $delta => $value) {
_link_validate($items[$delta], $delta, $field, $node, $optional_field_found);
}
if ($field['url'] == 'optional' && $field['title'] == 'optional' && $field['required'] && !$optional_field_found) {
form_set_error($field['field_name'] . '][0][title', t('At least one title or URL must be entered.'));
}
break;
case 'presave':
foreach ($items as $delta => $value) {
_link_process($items[$delta], $delta, $field, $node);
}
break;
case 'sanitize':
foreach ($items as $delta => $value) {
_link_sanitize($items[$delta], $delta, $field, $node);
}
break;
}
}
function link_widget_info() {
return array(
'link' => array(
'label' => 'Link',
'field types' => array(
'link',
),
'multiple values' => CONTENT_HANDLE_CORE,
),
);
}
function link_widget(&$form, &$form_state, $field, $items, $delta = 0) {
$element = array(
'#type' => $field['widget']['type'],
'#default_value' => isset($items[$delta]) ? $items[$delta] : '',
'#title' => $field['widget']['label'],
'#weight' => $field['widget']['weight'],
'#description' => $field['widget']['description'],
'#required' => $field['required'],
'#field' => $field,
);
return $element;
}
function _link_load(&$item, $delta = 0) {
$item['attributes'] = unserialize($item['attributes']);
}
function _link_process(&$item, $delta = 0, $field, $node) {
if (!$item['attributes']['target'] || $item['attributes']['target'] == "default") {
unset($item['attributes']['target']);
}
$item['url'] = trim($item['url']);
$item['attributes'] = serialize($item['attributes']);
if (isset($field['widget']['default_value'][$delta]['url']) && $item['url'] == $field['widget']['default_value'][$delta]['url'] && is_object($node)) {
if (!link_validate_url($item['url'])) {
unset($item['url']);
}
}
}
function _link_validate(&$item, $delta, $field, $node, &$optional_field_found) {
if ($item['url'] && !(isset($field['widget']['default_value'][$delta]['url']) && $item['url'] == $field['widget']['default_value'][$delta]['url'] && !$field['required'])) {
if (link_validate_url(trim($item['url'])) == FALSE) {
form_set_error($field['field_name'] . '][' . $delta . '][url', t('Not a valid URL.'));
}
if ($field['title'] == 'required' && strlen(trim($item['title'])) == 0) {
form_set_error($field['field_name'] . '][' . $delta . '][title', t('Titles are required for all links.'));
}
}
if ($field['url'] !== 'optional' && strlen($item['title']) > 0 && strlen(trim($item['url'])) == 0) {
form_set_error($field['field_name'] . '][' . $delta . '][url', t('You cannot enter a title without a link url.'));
}
if ($field['url'] == 'optional' && $field['title'] == 'optional' && (strlen(trim($item['url'])) != 0 || strlen(trim($item['title'])) != 0)) {
$optional_field_found = TRUE;
}
}
function _link_sanitize(&$item, $delta, &$field, &$node) {
if (empty($item['url']) && empty($item['title'])) {
return;
}
if (module_exists('token') && $field['enable_tokens']) {
$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']);
if (strpos($url, '#') !== FALSE) {
$item['fragment'] = substr($url, strpos($url, '#') + 1);
$url = substr($url, 0, strpos($url, '#'));
}
if (strpos($url, '?') !== FALSE) {
$item['query'] = substr($url, strpos($url, '?') + 1);
$url = substr($url, 0, strpos($url, '?'));
}
$item['url'] = $url;
$display_url = $type == LINK_EMAIL ? str_replace('mailto:', '', $url) : url($url, array(
'query' => isset($item['query']) ? $item['query'] : NULL,
'fragment' => isset($item['fragment']) ? $item['fragment'] : NULL,
'absolute' => TRUE,
));
if ($field['display']['url_cutoff'] && strlen($display_url) > $field['display']['url_cutoff']) {
$display_url = substr($display_url, 0, $field['display']['url_cutoff']) . "...";
}
$item['display_url'] = $display_url;
if ($field['title'] == 'value' && strlen(trim($field['title_value']))) {
$title = $field['title_value'];
}
else {
$title = $item['title'];
}
if (module_exists('token') && ($field['title'] == 'value' || $field['enable_tokens'])) {
$token_node = isset($node->nid) ? node_load($node->nid) : $node;
$title = filter_xss(token_replace($title, 'node', $token_node), array(
'b',
'br',
'code',
'em',
'i',
'img',
'span',
'strong',
'sub',
'sup',
'tt',
'u',
));
$item['html'] = TRUE;
}
$item['display_title'] = empty($title) ? $item['display_url'] : $title;
$attributes = array();
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;
}
}
}
if (is_array($field['attributes'])) {
foreach ($field['attributes'] as $attribute => $attbvalue) {
if (!empty($attbvalue) && $attbvalue != 'default' && $attbvalue != 'user') {
$attributes[$attribute] = $attbvalue;
}
}
}
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']);
}
}
$item['attributes'] = $attributes;
$item['label'] = $field['widget']['label'];
}
function link_theme() {
return array(
'link_field_settings' => array(
'arguments' => array(
'element' => NULL,
),
),
'link_formatter_default' => array(
'arguments' => array(
'element' => NULL,
),
),
'link_formatter_plain' => array(
'arguments' => array(
'element' => NULL,
),
),
'link_formatter_url' => array(
'arguments' => array(
'element' => NULL,
),
),
'link_formatter_short' => array(
'arguments' => array(
'element' => NULL,
),
),
'link_formatter_label' => array(
'arguments' => array(
'element' => NULL,
),
),
'link_formatter_separate' => array(
'arguments' => array(
'element' => NULL,
),
),
'link' => array(
'arguments' => array(
'element' => NULL,
),
),
);
}
function theme_link($element) {
drupal_add_css(drupal_get_path('module', 'link') . '/link.css');
if (empty($element['#field']['multiple'])) {
if (isset($element['url']) && isset($element['title'])) {
$element['url']['#title'] = $element['#title'] . ' ' . $element['url']['#title'];
$element['title']['#title'] = $element['#title'] . ' ' . $element['title']['#title'];
}
elseif ($element['url']) {
$element['url']['#title'] = $element['#title'];
}
}
$output = '';
$output .= '<div class="link-field-subrow clear-block">';
if (isset($element['title'])) {
$output .= '<div class="link-field-title link-field-column">' . theme('textfield', $element['title']) . '</div>';
}
$output .= '<div class="link-field-url' . (isset($element['title']) ? ' link-field-column' : '') . '">' . theme('textfield', $element['url']) . '</div>';
$output .= '</div>';
if (!empty($element['attributes']['target'])) {
$output .= '<div class="link-attributes">' . theme('checkbox', $element['attributes']['target']) . '</div>';
}
return $output;
}
function link_elements() {
$elements = array();
$elements['link'] = array(
'#input' => TRUE,
'#process' => array(
'link_process',
),
);
return $elements;
}
function link_process($element, $edit, $form_state, $form) {
$field = $form['#field_info'][$element['#field_name']];
$delta = $element['#delta'];
$element['url'] = array(
'#type' => 'textfield',
'#maxlength' => '255',
'#title' => t('URL'),
'#description' => $element['#description'],
'#required' => $delta == 0 && $field['url'] !== 'optional' ? $element['#required'] : FALSE,
'#default_value' => isset($element['#value']['url']) ? $element['#value']['url'] : NULL,
);
if ($field['title'] != 'none' && $field['title'] != 'value') {
$element['title'] = array(
'#type' => 'textfield',
'#maxlength' => '255',
'#title' => t('Title'),
'#required' => $delta == 0 && $field['title'] == 'required' ? $field['required'] : FALSE,
'#default_value' => isset($element['#value']['title']) ? $element['#value']['title'] : NULL,
);
}
if (!empty($field['attributes']['target']) && $field['attributes']['target'] == 'user') {
$attributes = is_array($element['#value']['attributes']) ? $element['#value']['attributes'] : unserialize($element['#value']['attributes']);
$element['attributes']['target'] = array(
'#type' => 'checkbox',
'#title' => t('Open URL in a New Window'),
'#return_value' => "_blank",
'#default_value' => isset($attributes['target']) ? $attributes['target'] : NULL,
);
}
return $element;
}
function link_field_formatter_info() {
return array(
'default' => array(
'label' => t('Title, as link (default)'),
'field types' => array(
'link',
),
'multiple values' => CONTENT_HANDLE_CORE,
),
'url' => array(
'label' => t('URL, as link'),
'field types' => array(
'link',
),
'multiple values' => CONTENT_HANDLE_CORE,
),
'plain' => array(
'label' => t('URL, as plain text'),
'field types' => array(
'link',
),
'multiple values' => CONTENT_HANDLE_CORE,
),
'short' => array(
'label' => t('Short, as link with title "Link"'),
'field types' => array(
'link',
),
'multiple values' => CONTENT_HANDLE_CORE,
),
'label' => array(
'label' => t('Label, as link with label as title'),
'field types' => array(
'link',
),
'multiple values' => CONTENT_HANDLE_CORE,
),
'separate' => array(
'label' => t('Separate title and URL'),
'field types' => array(
'link',
),
'multiple values' => CONTENT_HANDLE_CORE,
),
);
}
function theme_link_formatter_default($element) {
if (!empty($element['#item']['display_title']) && !empty($element['#item']['url'])) {
return l($element['#item']['display_title'], $element['#item']['url'], $element['#item']);
}
elseif (!empty($element['#item']['display_title'])) {
return check_plain($element['#item']['display_title']);
}
}
function theme_link_formatter_plain($element) {
return empty($element['#item']['url']) ? check_plain($element['#item']['title']) : url($element['#item']['url'], $element['#item']);
}
function theme_link_formatter_url($element) {
return $element['#item']['url'] ? l($element['#item']['display_url'], $element['#item']['url'], $element['#item']) : '';
}
function theme_link_formatter_short($element) {
return $element['#item']['url'] ? l(t('Link'), $element['#item']['url'], $element['#item']) : '';
}
function theme_link_formatter_label($element) {
return $element['#item']['url'] ? l($element['#item']['label'], $element['#item']['url'], $element['#item']) : '';
}
function theme_link_formatter_separate($element) {
$class = empty($element['#item']['attributes']['class']) ? '' : ' ' . $element['#item']['attributes']['class'];
unset($element['#item']['attributes']['class']);
$output = '';
$output .= '<div class="link-item ' . $class . '">';
$output .= '<div class="link-title">' . (empty($element['#item']['title']) ? '' : $element['#item']['display_title']) . '</div>';
$output .= '<div class="link-url">' . l($element['#item']['display_url'], $element['#item']['url'], $element['#item']) . '</div>';
$output .= '</div>';
return $output;
}
function link_token_list($type = 'all') {
if ($type == 'field' || $type == 'all') {
$tokens = array();
$tokens['link']['url'] = t("Link URL");
$tokens['link']['title'] = t("Link title");
$tokens['link']['view'] = t("Formatted html link");
return $tokens;
}
}
function link_token_values($type, $object = NULL) {
if ($type == 'field') {
$item = $object[0];
$tokens['url'] = $item['url'];
$tokens['title'] = $item['title'];
$tokens['view'] = isset($item['view']) ? $item['view'] : '';
return $tokens;
}
}
function link_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'link') . '/views',
);
}
function link_cleanup_url($url, $protocol = "http") {
$url = trim($url);
$type = link_validate_url($url);
if ($type == LINK_EXTERNAL) {
$protocol_match = preg_match("/^([a-z0-9][a-z0-9\\.\\-_]*:\\/\\/)/i", $url);
if (empty($protocol_match)) {
$domain_match = preg_match('/^(([a-z0-9]([a-z0-9\\-_]*\\.)+)(' . LINK_DOMAINS . '|[a-z]{2}))/i', $url);
if (!empty($domain_match)) {
$url = $protocol . "://" . $url;
}
}
}
return $url;
}
function link_validate_url($text) {
$allowed_protocols = variable_get('filter_allowed_protocols', array(
'http',
'https',
'ftp',
'news',
'nntp',
'telnet',
'mailto',
'irc',
'ssh',
'sftp',
'webcal',
));
$protocol = '((' . implode("|", $allowed_protocols) . '):\\/\\/)';
$authentication = '([a-z0-9]+(:[a-z0-9]+)?@)';
$domain = '(([a-z0-9]([a-z0-9\\-_\\[\\]])*)(\\.(([a-z0-9\\-_\\[\\]])+\\.)*(' . LINK_DOMAINS . '|[a-z]{2}))?)';
$ipv4 = '([0-9]{1,3}(\\.[0-9]{1,3}){3})';
$ipv6 = '([0-9a-fA-F]{1,4}(\\:[0-9a-fA-F]{1,4}){7})';
$port = '(:([0-9]{1,5}))';
$external_pattern = '/^' . $protocol . '?' . $authentication . '?(' . $domain . '|' . $ipv4 . '|' . $ipv6 . ' |localhost)' . $port . '?';
$internal_pattern = "/^([a-z0-9_\\-+\\[\\]]+)";
$directories = "(\\/[a-z0-9_\\-\\.~+%=&,\$'!():;*@\\[\\]]*)*";
$query = "(\\/?\\?([?a-z0-9+_|\\-\\.\\/\\\\%=&,\$'():;*@\\[\\]]*))";
$anchor = "(#[a-z0-9_\\-\\.~+%=&,\$'():;*@\\[\\]]*)";
$end = $directories . '?' . $query . '?' . $anchor . '?' . '$/i';
$user = '[a-zA-Z0-9_\\-\\.\\+\\^!#\\$%&*+\\/\\=\\?\\`\\|\\{\\}~\'\\[\\]]+';
$email_pattern = '/^mailto:' . $user . '@' . '(' . $domain . '|' . $ipv4 . '|' . $ipv6 . '|localhost)' . $query . '?$/';
if (strpos($text, '<front>') === 0) {
return LINK_FRONT;
}
if (in_array('mailto', $allowed_protocols) && preg_match($email_pattern, $text)) {
return LINK_EMAIL;
}
if (preg_match($internal_pattern . $end, $text)) {
return LINK_INTERNAL;
}
if (preg_match($external_pattern . $end, $text)) {
return LINK_EXTERNAL;
}
return FALSE;
}