diff --git a/link.info b/link.info index 2b65825..6f4e246 100644 --- a/link.info +++ b/link.info @@ -14,6 +14,7 @@ files[] = tests/link.crud_browser.test files[] = tests/link.token.test files[] = tests/link.entity_token.test files[] = tests/link.validate.test +files[] = tests/link.convert_alias.test ; Views Handlers files[] = views/link_views_handler_argument_target.inc diff --git a/link.module b/link.module index e17c26f..f7bf546 100644 --- a/link.module +++ b/link.module @@ -49,6 +49,7 @@ function link_field_info() { 'title_label_use_field_label' => FALSE, 'title_maxlength' => 128, 'enable_tokens' => 1, + 'convert_aliases' => 0, 'display' => array( 'url_cutoff' => 80, ), @@ -151,6 +152,13 @@ function link_field_instance_settings_form($field, $instance) { ); } + $form['convert_aliases'] = array( + '#type' => 'checkbox', + '#title' => t('Convert local aliases'), + '#default_value' => isset($instance['settings']['convert_aliases']) ? $instance['settings']['convert_aliases'] : '', + '#description' => t('If checked, a path alias is converted to the internal system path, the same way as when saving menu links.'), + ); + $form['display'] = array( '#tree' => TRUE, ); @@ -291,7 +299,7 @@ function link_field_validate($entity_type, $entity, $field, $instance, $langcode */ function link_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) { foreach ($items as $delta => $value) { - _link_process($items[$delta], $delta, $field, $entity); + _link_process($items[$delta], $delta, $field, $instance, $entity); } } @@ -300,7 +308,7 @@ function link_field_insert($entity_type, $entity, $field, $instance, $langcode, */ function link_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) { foreach ($items as $delta => $value) { - _link_process($items[$delta], $delta, $field, $entity); + _link_process($items[$delta], $delta, $field, $instance, $entity); } } @@ -372,12 +380,57 @@ function _link_load($field, $item, $instance) { /** * Prepares the item attributes and url for storage. */ -function _link_process(&$item, $delta, $field, $entity) { +function _link_process(&$item, $delta, $field, $instance, $entity) { // Trim whitespace from URL. if (!empty($item['url'])) { $item['url'] = trim($item['url']); } + // Optionally convert aliases to the system path. + if (!empty($instance['settings']['convert_aliases'])) { + // These variables are used to ensure + global $base_url; + + // Work out the correct base_path to use based on the HTTPS settings. + if (variable_get('https', FALSE) && isset($GLOBALS['base_secure_url'])) { + $real_base_url = $GLOBALS['base_secure_url']; + } + elseif (isset($GLOBALS['base_insecure_url']) && $GLOBALS['base_insecure_url'] != $base_url) { + $real_base_url = $GLOBALS['base_insecure_url']; + } + else { + $real_base_url = $base_url; + } + + // Check if either the site's absolute URL or the relative base URL are at + // the start of the URL, if so remove them. + $base_paths = array( + $real_base_url, + base_path(), + ); + $paths_to_test = array(); + foreach ($base_paths as $path) { + // Verify the path is at the beginning of the URL string. + if (strpos($item['url'], $path) === 0) { + $strlen = drupal_strlen($path); + $paths_to_test[] = drupal_substr($item['url'], $strlen); + } + } + $paths_to_test[] = $item['url']; + + // Remove any possible duplicates. + $paths_to_test = array_unique($paths_to_test); + + // Check each of the paths to see if one of them is a system path. + foreach ($paths_to_test as $path) { + $normal_path = drupal_get_normal_path($path); + if ($normal_path != $item['url']) { + $item['url'] = $normal_path; + break; + } + } + } + // If no attributes are set then make sure $item['attributes'] is an empty // array, so $field['attributes'] can override it. if (empty($item['attributes'])) {