Commit 5cb6837d authored by sos4nt's avatar sos4nt Committed by Damien McKenna
Browse files

Issue #836710 by DamienMcKenna, ciss, Johan den Hollander, joachim, mstrelan,...

Issue #836710 by DamienMcKenna, ciss, Johan den Hollander, joachim, mstrelan, sos4nt, Paul Lomax, rcross, rooby, timofey, seanB, Adirael, esbite, kay_v, joep.hendrix: Convert links to their internal link representation if they exist.
parent 1c815988
......@@ -2,6 +2,10 @@ Link 7.x-1.x-dev, xxxx-xx-xx
----------------------------
#2537248 by osopolar, Ronino, DamienMcKenna: Allow title without url on
migration.
#836710 by DamienMcKenna, ciss, Johan den Hollander, joachim, mstrelan, sos4nt,
Paul Lomax, rcross, rooby, timofey, seanB, Adirael, esbite, kay_v,
joep.hendrix: Convert links to their internal link representation if they
exist.
Link 7.x-1.7, 2019-11-14
......
......@@ -14,6 +14,7 @@ files[] = tests/link.token.test
files[] = tests/link.entity_token.test
files[] = tests/link.validate.test
files[] = tests/link.multilingual.test
files[] = tests/link.convert_alias.test
; Views Handlers
files[] = views/link_views_handler_argument_target.inc
......
......@@ -67,6 +67,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,
),
......@@ -187,6 +188,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,
);
......@@ -449,6 +457,68 @@ function _link_process(&$item, $delta, $field, $entity, $instance) {
$item['url'] = trim($item['url']);
}
// Optionally convert aliases to the system path.
if (!empty($instance['settings']['convert_aliases'])) {
global $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(
$base_url . base_path(),
base_path(),
);
// Work out the correct base_path to use based on the HTTPS settings.
if (isset($GLOBALS['base_secure_url'])) {
$base_paths[] = $GLOBALS['base_secure_url'] . base_path();
}
if (isset($GLOBALS['base_insecure_url'])) {
$base_paths[] = $GLOBALS['base_insecure_url'] . base_path();
}
// Add any additional paths
if ($extra_paths = variable_get('link_base_urls', array())) {
// Create versions with and without the base path.
foreach ($extra_paths as $extra_path) {
$base_paths[] = $extra_path;
$base_paths[] = $extra_path . base_path();
}
}
$paths_to_test = array(
$item['url']
);
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);
}
}
// Check each of the paths to see if one of them is a system path.
foreach (array_unique($paths_to_test) as $path) {
$language = NULL;
// If we have locale enabled attempt to remove the language prefix first.
if (module_exists('locale')) {
require_once DRUPAL_ROOT . '/includes/language.inc';
list($language, $path) = language_url_split_prefix($path, language_list());
}
// Attempt to get a system path.
$normal_path = drupal_get_normal_path($path, $language);
// If we get back a different path it means Drupal found a system path we
// can use.
if ($normal_path != $path) {
$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'])) {
......
<?php
/**
* @file
* Confirm path aliases are saved internally as the system path.
*/
/**
* Confirm path aliases are saved internally as the system path.
*/
class LinkConvertInternalTestCase extends LinkBaseTestClass {
/**
* Description of the tests.
*/
public static function getInfo() {
return array(
'name' => 'Conversion of internal path aliases',
'description' => 'Test that internal path aliases are saved as system paths.',
'group' => 'Link',
);
}
/**
* {@inheritdoc}
*/
function setUp(array $modules = []) {
$modules[] = 'path';
parent::setUp($modules);
}
/**
* Test the alias handling.
*/
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 that was 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 as the given alias.");
}
}
Markdown is supported
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