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'])) { diff --git a/tests/link.convert_alias.test b/tests/link.convert_alias.test new file mode 100644 index 0000000..ac412b6 --- /dev/null +++ b/tests/link.convert_alias.test @@ -0,0 +1,72 @@ + 'Conversion of internal path aliases', + 'description' => 'Test that internal path aliases are saved as system paths.', + 'group' => 'Link', + ); + } + + function setup() { + parent::setup('path'); + } + + function testInternalPathConversion() { + // Create 2 fields, one which converts aliases and one which doesn't. + $settings = array( + 'instance[settings][convert_aliases]' => TRUE, + ); + $field_name_converts = $this->createLinkField('page', $settings); + $field_name_plain = $this->createLinkField('page'); + + // Programatically create a node with an alias to link to. + $aliased_node = (object) array( + 'type' => 'page', + 'uid' => 1, + 'title' => $this->randomName(), + 'path' => array( + 'alias' => $this->randomName(), + ), + // This is needed for path alias to be saved. + 'language' => LANGUAGE_NONE, + ); + node_save($aliased_node); + + $this->drupalGet($aliased_node->path['alias']); + $this->assertText($aliased_node->title, 'Aliased node created.'); + + $this->drupalGet('node/add/page'); + + $label = $this->randomName(); + $edit = array( + 'title' => $label, + $field_name_converts . '[und][0][title]' => $label, + $field_name_converts . '[und][0][url]' => $aliased_node->path['alias'], + $field_name_plain . '[und][0][title]' => $label, + $field_name_plain . '[und][0][url]' => $aliased_node->path['alias'], + ); + $this->drupalPost(NULL, $edit, t('Save')); + $this->assertRaw(' has been created.', 'Node created'); + + // Load the node we just created. + $url = $this->getUrl(); + $split = explode('/', $url); + $nid = array_pop($split); + + $node = node_load($nid); + + $link_field_converts_items = field_get_items('node', $node, $field_name_converts); + $this->assertEqual($link_field_converts_items[0]['url'], "node/{$aliased_node->nid}", "The field value was saved as the internal path for the alias."); + + $link_field_plain_items = field_get_items('node', $node, $field_name_plain); + $this->assertEqual($link_field_plain_items[0]['url'], $aliased_node->path['alias'], "The field value was saved the given alias."); + } + +} \ No newline at end of file