Commit 50761cbd authored by John C Fiala's avatar John C Fiala
Browse files

task #696678 by tristanoneil:Link module now showing basic functionality -...

task #696678 by tristanoneil:Link module now showing basic functionality - still not done, but better.
parent 3c962bfd
......@@ -11,7 +11,6 @@
*/
function link_install() {
drupal_load('module', 'content');
content_notify('install', 'link');
}
/**
......@@ -19,7 +18,6 @@ function link_install() {
*/
function link_uninstall() {
drupal_load('module', 'content');
content_notify('uninstall', 'link');
}
/**
......@@ -27,7 +25,6 @@ function link_uninstall() {
*/
function link_enable() {
drupal_load('module', 'content');
content_notify('enable', 'link');
}
/**
......@@ -35,7 +32,6 @@ function link_enable() {
*/
function link_disable() {
drupal_load('module', 'content');
content_notify('disable', 'link');
}
/**
......
......@@ -76,7 +76,7 @@ function link_field_settings_form($field, $instance, $has_data) {
'#element_validate' => array('link_field_settings_form_validate'),
/* '#theme' => 'link_field_settings', */
);
$form['url'] = array(
'#type' => 'checkbox',
'#title' => t('Optional URL'),
......@@ -84,14 +84,14 @@ function link_field_settings_form($field, $instance, $has_data) {
'#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'),
......@@ -99,7 +99,7 @@ function link_field_settings_form($field, $instance, $has_data) {
'#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',
'#title' => t('Static title'),
......@@ -107,7 +107,7 @@ function link_field_settings_form($field, $instance, $has_data) {
/* '#size' => '46', */
'#description' => t('This title will always be used if &ldquo;Static Title&rdquo; is selected above.'),
);
// Add token module replacements if available
if (module_exists('token')) {
$form['tokens'] = array(
......@@ -120,7 +120,7 @@ function link_field_settings_form($field, $instance, $has_data) {
$form['tokens']['help'] = array(
'#value' => theme('token_help', 'node'),
);
$form['enable_tokens'] = array(
'#type' => 'checkbox',
'#title' => t('Allow user-entered tokens'),
......@@ -128,7 +128,7 @@ function link_field_settings_form($field, $instance, $has_data) {
'#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,
);
......@@ -140,7 +140,7 @@ function link_field_settings_form($field, $instance, $has_data) {
'#maxlength' => 3,
'#size' => 3,
);
$target_options = array(
LINK_TARGET_DEFAULT => t('Default (no target attribute)'),
LINK_TARGET_TOP => t('Open link in window root'),
......@@ -183,13 +183,6 @@ function link_field_settings_form_validate($element, &$form_state, $complete_for
}
}
/**
* Implement hook_field_sanitize().
*/
function link_field_sanitize($obj_type, $object, $field, $instance, $langcode, &$items) {
}
/**
* Implementation of hook_field_settings().
*/
......@@ -341,7 +334,7 @@ function link_field_is_empty($item, $field) {
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] = _link_load($field, $item);
$items[$id][$delta]['attributes'] = _link_load($field, $item);
}
}
}
......@@ -352,14 +345,7 @@ function link_field_load($entity_type, $entities, $field, $instances, $langcode,
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, $optional_field_found);
}
if ($field['settings']['url'] === 'optional'
&& $field['settings']['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.'));
_link_validate($items[$delta], $delta, $field, $entity, $instance, $optional_field_found);
}
}
......@@ -368,6 +354,7 @@ function link_field_validate($entity_type, $entity, $field, $instance, $langcode
*/
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);
}
}
......@@ -412,7 +399,7 @@ function link_field_widget_form(&$form, &$form_state, $field, $instance, $langco
}*/
function _link_load($field, &$item) {
$item['attributes'] = isset($item['attributes']) ? unserialize($item['attributes']) : $field['settings']['attributes'];
return $item['attributes'] = isset($item['attributes']) ? unserialize($item['attributes']) : $field['settings']['attributes'];
}
function _link_process(&$item, $delta = 0, $field, $entity) {
......@@ -435,7 +422,7 @@ function _link_process(&$item, $delta = 0, $field, $entity) {
}
}
function _link_validate(&$item, $delta, $field, $node, &$optional_field_found) {
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) {
......@@ -447,13 +434,20 @@ function _link_validate(&$item, $delta, $field, $node, &$optional_field_found) {
}
}
// Require a link if we have a title.
if ($field['settings']['url'] !== 'optional' && strlen($item['title']) > 0 && strlen(trim($item['url'])) == 0) {
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.'));
}
}
/**
......@@ -499,14 +493,14 @@ function _link_sanitize(&$item, $delta, &$field, &$node) {
// 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['display']['url_cutoff'] && strlen($display_url) > $field['display']['url_cutoff']) {
$display_url = substr($display_url, 0, $field['display']['url_cutoff']) ."...";
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['title'] == 'value' && strlen(trim($field['title_value']))) {
$title = $field['title_value'];
if ($field['settings']['title'] == 'value' && strlen(trim($field['settings']['title_value']))) {
$item['title'] = $field['settings']['title_value'];
}
// Use the title defined by the user at the widget level.
else {
......@@ -531,15 +525,15 @@ function _link_sanitize(&$item, $delta, &$field, &$node) {
}
// Add default attributes.
$field['attributes'] += _link_default_attributes();
$field['settings']['attributes'] += _link_default_attributes();
// Merge item attributes with attributes defined at the field level.
$item['attributes'] += $field['attributes'];
$item['attributes'] += $field['settings']['attributes'];
// If user is not allowed to choose target attribute, use default defined at
// field level.
if ($field['attributes']['target'] != LINK_TARGET_USER) {
$item['attributes']['target'] = $field['attributes']['target'];
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.
......@@ -555,8 +549,13 @@ function _link_sanitize(&$item, $delta, &$field, &$node) {
// Remove empty attributes.
$item['attributes'] = array_filter($item['attributes']);
// Add the widget label.
$item['label'] = $field['widget']['label'];
// Sets title to trimmed url if one exists
if(!empty($item['display_url']) && empty($item['title'])) {
$item['title'] = $item['display_url'];
}
elseif(!isset($item['title'])) {
$item['title'] = $item['url'];
}
}
/**
......@@ -664,7 +663,7 @@ function link_field_process($element, $form_state, $form) {
'#type' => 'textfield',
'#maxlength' => '255',
'#title' => t('Title'),
'#required' => /* ($element['#delta'] == 0 && $settings['title'] == 'required') ? $settings['required'] : FALSE, */ $settings['title'] === 'required',
'#required' => ($settings['title'] == 'required' && !empty($element['#value']['url'])) ? TRUE : FALSE,
'#default_value' => isset($element['#value']['title']) ? $element['#value']['title'] : NULL,
);
}
......@@ -681,7 +680,7 @@ function link_field_process($element, $form_state, $form) {
'#type' => 'checkbox',
'#title' => t('Open URL in a New Window'),
'#return_value' => LINK_TARGET_NEW_WINDOW,
'#default_value' => $attributes['target'],
'#default_value' => isset($attributes['target']) ? $attributes['target'] : FALSE,
);
}
return $element;
......@@ -729,7 +728,7 @@ function link_field_formatter_view($entity_type, $entity, $field, $instance, $la
$elements = array();
foreach ($items as $delta => $item) {
$elements[$delta] = array(
'#markup' => theme('link_formatter_'. $display['type'], array('element' => $item)),
'#markup' => theme('link_formatter_'. $display['type'], array('element' => $item, 'field' => $instance)),
);
}
return $elements;
......@@ -739,14 +738,17 @@ function link_field_formatter_view($entity_type, $entity, $field, $instance, $la
* Theme function for 'default' text field formatter.
*/
function theme_link_formatter_default($element) {
//drupal_set_message('<pre>'. print_r($element['#item'], TRUE) .'</pre>');
//drupal_set_message('<pre>'. print_r($element['element'], TRUE) .'</pre>');
// Display a normal link if both title and URL are available.
if (!empty($element['#item']['display_title']) && !empty($element['#item']['url'])) {
return l($element['#item']['display_title'], $element['#item']['url'], $element['#item']);
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['#item']['display_title'])) {
return check_plain($element['#item']['display_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']));
}
}
......@@ -754,7 +756,7 @@ function theme_link_formatter_default($element) {
* Theme function for 'plain' text field formatter.
*/
function theme_link_formatter_plain($element) {
return empty($element['#item']['url']) ? check_plain($element['#item']['title']) : url($element['#item']['url'], $element['#item']);
return empty($element['element']['url']) ? check_plain($element['element']['title']) : url($element['element']['url'], array('attributes' => $element['element']['attributes']));
}
/**
......@@ -762,7 +764,7 @@ function theme_link_formatter_plain($element) {
*/
/*
function theme_link_formatter_url($element) {
return $element['#item']['url'] ? l($element['#item']['display_url'], $element['#item']['url'], $element['#item']) : '';
return $element['element']['url'] ? l($element['element']['display_url'], $element['element']['url'], array('attributes' => $element['element']['attributes']) : '';
}
*/
......@@ -770,35 +772,35 @@ function theme_link_formatter_url($element) {
* Theme function for 'short' text field formatter.
*/
function theme_link_formatter_short($element) {
return $element['#item']['url'] ? l(t('Link'), $element['#item']['url'], $element['#item']) : '';
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['#item']['url'] ? l($element['#item']['label'], $element['#item']['url'], $element['#item']) : '';
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['#item']['attributes']['class']) ? '' : ' '. $element['#item']['attributes']['class'];
unset($element['#item']['attributes']['class']);
$title = empty($element['#item']['title']) ? '' : check_plain($element['#item']['title']);
$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 .= '<div class="link-item '. $class .'">';
if (!empty($title)) {
$output .= '<div class="link-title">'. $title .'</div>';
}
$output .= '<div class="link-url">'. l($element['#item']['display_url'], $element['#item']['url'], $element['#item']) .'</div>';
$output .= '<div class="link-url">'. l($element['element']['url'], $element['element']['url'], array('attributes' => $element['element']['attributes'])) .'</div>';
$output .= '</div>';
return $output;
}
*/
function link_token_list($type = 'all') {
if ($type === 'field' || $type === 'all') {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment