array(
'label' => t('Link'),
'description' => t('Store a title, href, and attributes in the database to assemble a link.'),
'settings' => array(
'target' => LINK_TARGET_DEFAULT,
'class' => '',
'rel' => '',
),
'default_widget' => 'link_field',
'default_formatter' => 'default',
),
);
}
/**
* Implements hook_field_schema().
*/
function link_field_schema($field) {
return array(
'columns' => array(
'url' => array(
'type' => 'varchar',
'length' => LINK_URL_MAX_LENGTH,
'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
),
),
);
}
/**
* Implements hook_field_settings_form().
*/
function link_field_settings_form($field, $instance, $has_data) {
$form = array(
'#element_validate' => array('link_field_settings_form_validate'),
/* '#theme' => 'link_field_settings', */
);
$form['url'] = array(
'#type' => 'checkbox',
'#title' => t('Optional URL'),
'#default_value' => isset($field['settings']['url']) ? $field['settings']['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 omitted, 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['settings']['title']) ? $field['settings']['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 token module 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 strong, em, img, span, etc.'),
);
$form['title_value'] = array(
'#type' => 'textfield',
'#title' => t('Static title'),
'#default_value' => isset($field['settings']['title_value']) ? $field['settings']['title_value'] : '',
/* '#size' => '46', */
'#description' => t('This title will always be used if “Static Title” is selected above.'),
);
// Add token module replacements if available
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['settings']['enable_tokens']) ? $field['settings']['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['settings']['display']['url_cutoff']) ? $field['settings']['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(
LINK_TARGET_DEFAULT => t('Default (no target attribute)'),
LINK_TARGET_TOP => t('Open link in window root'),
LINK_TARGET_NEW_WINDOW => t('Open link in new window'),
LINK_TARGET_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['settings']['attributes']['target']) ? LINK_TARGET_DEFAULT : $field['settings']['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 rel="nofollow" which prevents some search engines from spidering entered links.'),
'#default_value' => empty($field['settings']['attributes']['rel']) ? '' : $field['settings']['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['settings']['attributes']['class']) ? '' : $field['settings']['attributes']['class'],
);
return $form;
}
/**
* Validate the field settings form.
*/
function link_field_settings_form_validate($element, &$form_state, $complete_form) {
if ($form_state['values']['field']['settings']['title'] === 'value' && empty($form_state['values']['field']['settings']['title_value'])) {
form_set_error('title_value', t('A default title must be provided if the title is a static value.'));
}
}
/**
* Implementation of hook_field_settings().
*/
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 omitted, 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 token module 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 strong, em, img, span, etc.'),
);
$form['title_value'] = array(
'#type' => 'textfield',
'#default_value' => $field['title_value'],
'#size' => '46',
);
// Add token module replacements if available
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(
LINK_TARGET_DEFAULT => t('Default (no target attribute)'),
LINK_TARGET_TOP => t('Open link in window root'),
LINK_TARGET_NEW_WINDOW => t('Open link in new window'),
LINK_TARGET_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']) ? LINK_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 rel="nofollow" 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);
}
}
/**
* Theme the settings form for the link field.
*/
/*
function theme_link_field_settings($form) {
$title_value = drupal_render($form['title_value']);
$title_checkbox = drupal_render($form['title']['value']);
// Set Static Title radio option to include the title_value textfield.
$form['title']['value'] = array('#value' => '
'. $title_checkbox . $title_value .'
');
// Reprint the title radio options with the included textfield.
return drupal_render($form);
}
/**
* Implement hook_field_is_empty().
*/
function link_field_is_empty($item, $field) {
return empty($item['title']) && empty($item['url']);
}
function link_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
foreach ($entities as $id => $entity) {
foreach ($items[$id] as $delta => $item) {
$items[$id][$delta]['attributes'] = _link_load($field, $item);
}
}
}
/**
* Implements hook_field_validate().
*/
function link_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
$optional_field_found = FALSE;
foreach ($items as $delta => $value) {
_link_validate($items[$delta], $delta, $field, $entity, $instance, $optional_field_found);
}
}
/**
* Implements hook_field_presave().
*/
function link_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
foreach ($items as $delta => $value) {
_link_sanitize($items[$delta], $delta, $field, $entity);
_link_process($items[$delta], $delta, $field, $entity);
}
}
/**
* Implements hook_field_widget_info().
*/
function link_field_widget_info() {
return array(
'link_field' => array(
'label' => 'Link',
'field types' => array('link_field'),
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
),
);
}
/**
* Implements hook_field_widget_form().
*/
function link_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$element += array(
'#type' => $instance['widget']['type'],
'#default_value' => isset($items[$delta]) ? $items[$delta] : '',
);
return $element;
}
/**
* Implements hook_field_formatter_view().
*/
/*function link_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
if ($display['type'] === 'link_default') {
foreach ($items as $delta => $item) {
$element[$delta] = array(
'#markup' => theme('link_field', $item),
);
}
}
return $element;
}*/
function _link_load($field, &$item) {
return $item['attributes'] = isset($item['attributes']) ? unserialize($item['attributes']) : $field['settings']['attributes'];
}
function _link_process(&$item, $delta = 0, $field, $entity) {
// Trim whitespace from URL.
$item['url'] = trim($item['url']);
// if no attributes are set then make sure $item['attributes'] is an empty array - this lets $field['attributes'] override it.
if (empty($item['attributes'])) {
$item['attributes'] = array();
}
// Serialize the attributes array.
$item['attributes'] = serialize($item['attributes']);
// Don't save an invalid default value (e.g. 'http://').
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, $instance, &$optional_field_found) {
if ($item['url'] && !(isset($field['widget']['default_value'][$delta]['url']) && $item['url'] === $field['widget']['default_value'][$delta]['url'] && !$item['required'])) {
// Validate the link.
if (link_validate_url(trim($item['url'])) == FALSE) {
form_set_error($field['field_name'] .']['. $delta .'][url', t('Not a valid URL.'));
}
// Require a title for the link if necessary.
if ($field['settings']['title'] == 'required' && strlen(trim($item['title'])) == 0) {
form_set_error($field['field_name'] .']['. $delta .'][title', t('Titles are required for all links.'));
}
}
// Require a link if we have a title.
if ($field['settings']['url'] !== 'optional' && strlen(isset($item['title']) ? $item['title'] : NULL) > 0 && strlen(trim($item['url'])) == 0) {
form_set_error($field['field_name'] .']['. $delta .'][url', t('You cannot enter a title without a link url.'));
}
// In a totally bizzaro case, where URLs and titles are optional but the field is required, ensure there is at least one link.
if ($field['settings']['url'] === 'optional' && $field['settings']['title'] === 'optional' && (strlen(trim($item['url'])) !== 0 || strlen(trim($item['title'])) !== 0)) {
$optional_field_found = TRUE;
}
// Require entire field
if ($field['settings']['url'] === 'optional'
&& $field['settings']['title'] === 'optional'
&& $instance['required'] == 1 && !$optional_field_found && isset($instance['id'])) {
form_set_error($instance['field_name'] .'][0][title',
t('At least one title or URL must be entered.'));
}
}
/**
* Cleanup user-entered values for a link field according to field settings.
*
* @param $item
* A single link item, usually containing url, title, and attributes.
* @param $delta
* The delta value if this field is one of multiple fields.
* @param $field
* The CCK field definition.
* @param $node
* The node containing this link.
*/
function _link_sanitize(&$item, $delta, &$field, &$node) {
// Don't try to process empty links.
if (empty($item['url']) && empty($item['title'])) {
return;
}
// 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) {
$item['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);
parse_str($query, $query_array);
$item['query'] = $query_array;
$url = substr($url, 0, strpos($url, '?'));
}
// Create a shortened URL for display.
$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['settings']['display']['url_cutoff'] && strlen($display_url) > $field['settings']['display']['url_cutoff']) {
$display_url = substr($display_url, 0, $field['settings']['display']['url_cutoff']) ."...";
}
$item['display_url'] = $display_url;
// Use the title defined at the field level.
if ($field['settings']['title'] == 'value' && strlen(trim($field['settings']['title_value']))) {
$title = $field['settings']['title_value'];
}
// Use the title defined by the user at the widget level.
else if (isset($item['title'])) {
$title = $item['title'];
}
// Replace tokens.
if (module_exists('token') && ($field['title'] == 'value' || $field['enable_tokens'])) {
// Load the node if necessary for nodes in views.
$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['title'] = empty($title) ? $item['display_url'] : $title;
if (!isset($item['attributes'])) {
$item['attributes'] = array();
}
// Unserialize attributtes array if it has not been unserialized yet.
if (!is_array($item['attributes'])) {
$item['attributes'] = (array)unserialize($item['attributes']);
}
// Add default attributes.
$field['settings']['attributes'] += _link_default_attributes();
// Merge item attributes with attributes defined at the field level.
$item['attributes'] += $field['settings']['attributes'];
// If user is not allowed to choose target attribute, use default defined at
// field level.
if ($field['settings']['attributes']['target'] != LINK_TARGET_USER) {
$item['attributes']['target'] = $field['settings']['attributes']['target'];
}
// Remove the target attribute if the default (no target) is selected.
if (empty($item['attributes']) || $item['attributes']['target'] == LINK_TARGET_DEFAULT) {
unset($item['attributes']['target']);
}
// Remove the rel=nofollow for internal links.
if ($type != LINK_EXTERNAL && strpos($item['attributes']['rel'], 'nofollow') !== FALSE) {
$item['attributes']['rel'] = str_replace('nofollow', '', $item['attributes']);
}
// Remove empty attributes.
$item['attributes'] = array_filter($item['attributes']);
// Sets title to trimmed url if one exists
// @TODO: Do we need this? It seems not.
if(!empty($item['display_url']) && empty($item['title'])) {
$item['title'] = $item['display_url'];
}
elseif(!isset($item['title'])) {
$item['title'] = $item['url'];
}
}
/**
* Implements hook_theme().
*/
function link_theme() {
return array(
/*'link_field_settings' => array(
'variables' => array('element' => NULL),
),*/
'link_formatter_default' => array(
'variables' => array('element' => NULL),
),
'link_formatter_plain' => array(
'variables' => array('element' => NULL),
),
'link_formatter_url' => array(
'variables' => array('element' => NULL),
),
'link_formatter_short' => array(
'variables' => array('element' => NULL),
),
'link_formatter_label' => array(
'variables' => array('element' => NULL),
),
'link_formatter_separate' => array(
'variables' => array('element' => NULL),
),
'link_field' => array(
'render element' => 'element',
),
);
}
/**
* FAPI theme for an individual text elements.
*/
function theme_link_field($vars) {
drupal_add_css(drupal_get_path('module', 'link') .'/link.css');
$element = $vars['element'];
// Prefix single value link fields with the name of the field.
if (empty($element['#field']['multiple'])) {
if (isset($element['url']) && !isset($element['title'])) {
unset($element['url']['#title']);
}
}
$output = '';
$output .= '';
if (isset($element['title'])) {
$output .= '
'. drupal_render($element['title']) .'
';
}
$output .= '
'. drupal_render($element['url']) .'
';
$output .= '
';
if (!empty($element['attributes']['target'])) {
$output .= ''. drupal_render($element['attributes']['target']) .'
';
}
return $output;
}
/**
* Implements hook_element_info().
*/
function link_element_info() {
$elements = array();
$elements['link_field'] = array(
'#input' => TRUE,
'#process' => array('link_field_process'),
'#theme' => 'link_field',
'#theme_wrappers' => array('form_element'),
);
return $elements;
}
function _link_default_attributes() {
return array(
'target' => LINK_TARGET_DEFAULT,
'class' => '',
'rel' => '',
);
}
/**
* Process the link type element before displaying the field.
*
* Build the form element. When creating a form using FAPI #process,
* note that $element['#value'] is already set.
*
* The $fields array is in $form['#field_info'][$element['#field_name']].
*/
function link_field_process($element, $form_state, $form) {
$settings = &$form_state['field'][$element['#field_name']][$element['#language']]['field']['settings'];
$element['url'] = array(
'#type' => 'textfield',
'#maxlength' => LINK_URL_MAX_LENGTH,
'#title' => t('URL'),
'#required' => ($element['#delta'] == 0 && $settings['url'] !== 'optional') ? $element['#required'] : FALSE,
'#default_value' => isset($element['#value']['url']) ? $element['#value']['url'] : NULL,
);
if ($settings['title'] !== 'none' && $settings['title'] !== 'value') {
$element['title'] = array(
'#type' => 'textfield',
'#maxlength' => '255',
'#title' => t('Title'),
'#required' => ($settings['title'] == 'required' && !empty($element['#value']['url'])) ? TRUE : FALSE,
'#default_value' => isset($element['#value']['title']) ? $element['#value']['title'] : NULL,
);
}
// Initialize field attributes as an array if it is not an array yet.
if (!is_array($settings['attributes'])) {
$settings['attributes'] = array();
}
// Add default atrributes.
$settings['attributes'] += _link_default_attributes();
$attributes = isset($element['#value']['attributes']) ? $element['#value']['attributes'] : $settings['attributes'];
if (!empty($settings['attributes']['target']) && $settings['attributes']['target'] == LINK_TARGET_USER) {
$element['attributes']['target'] = array(
'#type' => 'checkbox',
'#title' => t('Open URL in a New Window'),
'#return_value' => LINK_TARGET_NEW_WINDOW,
'#default_value' => isset($attributes['target']) ? $attributes['target'] : FALSE,
);
}
return $element;
}
/**
* Implementation of hook_field_formatter_info().
*/
function link_field_formatter_info() {
return array(
'default' => array(
'label' => t('Title, as link (default)'),
'field types' => array('link_field'),
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
),
'url' => array(
'label' => t('URL, as link'),
'field types' => array('link_field'),
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
),
'plain' => array(
'label' => t('URL, as plain text'),
'field types' => array('link_field'),
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
),
'short' => array(
'label' => t('Short, as link with title "Link"'),
'field types' => array('link_field'),
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
),
'label' => array(
'label' => t('Label, as link with label as title'),
'field types' => array('link_field'),
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
),
'separate' => array(
'label' => t('Separate title and URL'),
'field types' => array('link_field'),
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
),
);
}
function link_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$elements = array();
foreach ($items as $delta => $item) {
$elements[$delta] = array(
'#markup' => theme('link_formatter_'. $display['type'], array('element' => $item, 'field' => $instance)),
);
}
return $elements;
}
/**
* Theme function for 'default' text field formatter.
*/
function theme_link_formatter_default($element) {
//drupal_set_message(''. print_r($element['element'], TRUE) .'
');
// Display a normal link if both title and URL are available.
if (!empty($element['element']['title']) && !empty($element['element']['url'])) {
return l($element['element']['title'], $element['element']['url'], array('attributes' => $element['element']['attributes']));
}
// If only a title, display the title.
elseif (!empty($element['element']['title'])) {
return check_plain($element['element']['title']);
}
elseif (!empty($element['element']['url'])) {
return l($element['element']['title'], $element['element']['url'], array('attributes' => $element['element']['attributes']));
}
}
/**
* Theme function for 'plain' text field formatter.
*/
function theme_link_formatter_plain($element) {
return empty($element['element']['url']) ? check_plain($element['element']['title']) : url($element['element']['url'], array('attributes' => $element['element']['attributes']));
}
/**
* Theme function for 'url' text field formatter.
*/
/*
function theme_link_formatter_url($element) {
return $element['element']['url'] ? l($element['element']['display_url'], $element['element']['url'], array('attributes' => $element['element']['attributes']) : '';
}
*/
/**
* Theme function for 'short' text field formatter.
*/
function theme_link_formatter_short($element) {
return $element['element']['url'] ? l(t('Link'), $element['element']['url'], array('attributes' => $element['element']['attributes'])) : '';
}
/**
* Theme function for 'label' text field formatter.
*/
function theme_link_formatter_label($element) {
return $element['element']['url'] ? l($element['field']['label'], $element['element']['url'], array('attributes' => $element['element']['attributes'])) : '';
}
/**
* Theme function for 'separate' text field formatter.
*/
function theme_link_formatter_separate($element) {
$class = empty($element['element']['attributes']['class']) ? '' : ' '. $element['element']['attributes']['class'];
unset($element['element']['attributes']['class']);
$title = empty($element['element']['title']) ? '' : check_plain($element['element']['title']);
$output = '';
$output .= '';
if (!empty($title)) {
$output .= '
'. $title .'
';
}
$output .= '
'. l($element['element']['url'], $element['element']['url'], array('attributes' => $element['element']['attributes'])) .'
';
$output .= '
';
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;
}
}
/**
* Implements hook_views_api().
*/
function link_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'link') .'/views',
);
}
/**
* Forms a valid URL if possible from an entered address.
* Trims whitespace and automatically adds an http:// to addresses without a protocol specified
*
* @param string $url
* @param string $protocol The protocol to be prepended to the url if one is not specified
*/
function link_cleanup_url($url, $protocol = "http") {
$url = trim($url);
$type = link_validate_url($url);
if ($type === LINK_EXTERNAL) {
// Check if there is no protocol specified.
$protocol_match = preg_match("/^([a-z0-9][a-z0-9\.\-_]*:\/\/)/i", $url);
if (empty($protocol_match)) {
// But should there be? Add an automatic http:// if it starts with a domain name.
$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;
}
/**
* A lenient verification for URLs. Accepts all URLs following RFC 1738 standard
* for URL formation and all email addresses following the RFC 2368 standard for
* mailto address formation.
*
* @param string $text
* @return mixed Returns boolean FALSE if the URL is not valid. On success, returns an object with
* the following attributes: protocol, hostname, ip, and port.
*/
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}))';
// Pattern specific to eternal links.
$external_pattern = '/^'. $protocol .'?'. $authentication .'?('. $domain .'|'. $ipv4 .'|'. $ipv6 .' |localhost)'. $port .'?';
// Pattern specific to internal links.
$internal_pattern = "/^([a-z0-9_\-+\[\]]+)";
$directories = "(\/[a-z0-9_\-\.~+%=&,$'!():;*@\[\]]*)*";
// Yes, four backslashes == a single backslash.
$query = "(\/?\?([?a-z0-9+_|\-\.\/\\\\%=&,$'():;*@\[\]]*))";
$anchor = "(#[a-z0-9_\-\.~+%=&,$'():;*@\[\]]*)";
// The rest of the path for a standard URL.
$end = $directories .'?'. $query .'?'. $anchor .'?'.'$/i';
$user = '[a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\`\|\{\}~\'\[\]]+';
$email_pattern = '/^mailto:'. $user .'@'.'('. $domain .'|'. $ipv4 .'|'. $ipv6 .'|localhost)'. $query .'?$/';
if (strpos($text, '') === 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;
}