Skip to content

Instantly share code, notes, and snippets.

@jbroadway
Last active December 14, 2015 12:38
Show Gist options
  • Save jbroadway/5087622 to your computer and use it in GitHub Desktop.
Save jbroadway/5087622 to your computer and use it in GitHub Desktop.
Alternative link generation proposal for Elefant 2. Note that this also proposes changing line 370 of `lib/I18n.php` to `$this->prefix = '/' . $matches[1];` so that a prefix is set when a URL like `/en` is matched.
<?php
require_once ('lib/Autoloader.php');
class LinkTest extends PHPUnit_Framework_TestCase {
function setUp () {
$nav = new Navigation;
$nav->tree = array (
(object) array (
'data' => 'Home',
'attr' => (object) array ('id' => 'index')
),
(object) array (
'data' => 'Other',
'attr' => (object) array ('id' => 'other')
),
(object) array (
'data' => 'English',
'attr' => (object) array ('id' => 'en'),
'children' => array (
(object) array (
'data' => 'About',
'attr' => (object) array ('id' => 'about'),
'children' => array (
(object) array (
'data' => 'News',
'attr' => (object) array ('id' => 'news')
)
)
),
(object) array (
'data' => 'Contact us',
'attr' => (object) array ('id' => 'contact-us')
)
)
),
(object) array (
'data' => 'Français',
'attr' => (object) array ('id' => 'fr'),
'children' => array (
(object) array (
'data' => 'À propos',
'attr' => (object) array ('id' => 'a-propos')
),
(object) array (
'data' => 'Contactez-nous',
'attr' => (object) array ('id' => 'contactez-nous')
)
)
)
);
Link::nav ($nav);
}
function test_single_with_http_and_flat () {
Link::negotiation_method ('http');
Link::url_style ('flat');
$i18n = new I18n;
Link::i18n ($i18n);
$i18n->prefix = '';
Link::current ('index');
$expected = "<li class=\"current\"><a href=\"/index\">Home</a></li>\n";
$this->assertEquals ($expected, Link::single ('index', 'Home'));
$i18n->prefix = '/fr';
$this->assertEquals ($expected, Link::single ('index', 'Home'));
$expected = "<li><a href=\"/news\">News</a></li>\n";
$this->assertEquals ($expected, Link::single ('news', 'News'));
Link::current ('news');
$expected = "<li class=\"current\"><a href=\"/news\">News</a></li>\n";
$this->assertEquals ($expected, Link::single ('news', 'News'));
$expected = "<li class=\"active\"><a href=\"/about\">About</a></li>\n";
$this->assertEquals ($expected, Link::single ('about', 'About'));
}
function test_single_with_http_and_nested () {
Link::negotiation_method ('http');
Link::url_style ('nested');
$i18n = new I18n;
Link::i18n ($i18n);
$i18n->prefix = '';
Link::current ('index');
$expected = "<li class=\"current\"><a href=\"/index\">Home</a></li>\n";
$this->assertEquals ($expected, Link::single ('index', 'Home'));
$i18n->prefix = '/fr';
$this->assertEquals ($expected, Link::single ('index', 'Home'));
$expected = "<li><a href=\"/en/about\">About</a></li>\n";
$this->assertEquals ($expected, Link::single ('about', 'About'));
Link::current ('about');
$expected = "<li class=\"current\"><a href=\"/en/about\">About</a></li>\n";
$this->assertEquals ($expected, Link::single ('about', 'About'));
$expected = "<li><a href=\"/en/about/news\">News</a></li>\n";
$this->assertEquals ($expected, Link::single ('news', 'News'));
Link::current ('news');
$expected = "<li class=\"current\"><a href=\"/en/about/news\">News</a></li>\n";
$this->assertEquals ($expected, Link::single ('news', 'News'));
$expected = "<li class=\"active\"><a href=\"/en/about\">About</a></li>\n";
$this->assertEquals ($expected, Link::single ('about', 'About'));
}
function test_single_with_url_and_flat () {
Link::negotiation_method ('url');
Link::url_style ('flat');
$i18n = new I18n;
Link::i18n ($i18n);
$i18n->prefix = '';
Link::current ('index');
$expected = "<li class=\"current\"><a href=\"/index\">Home</a></li>\n";
$this->assertEquals ($expected, Link::single ('index', 'Home'));
$i18n->prefix = '/en';
$expected = "<li class=\"current\"><a href=\"/en/index\">Home</a></li>\n";
$this->assertEquals ($expected, Link::single ('index', 'Home'));
// Note: This would have presumably forwarded to /{$i18n->language} in any case
$i18n->prefix = '';
$expected = "<li><a href=\"/en\">English</a></li>\n";
$this->assertEquals ($expected, Link::single ('en', 'English'));
$i18n->prefix = '/en';
$expected = "<li><a href=\"/en\">English</a></li>\n";
$this->assertEquals ($expected, Link::single ('en', 'English'));
$i18n->prefix = '';
$expected = "<li><a href=\"/about\">About</a></li>\n";
$this->assertEquals ($expected, Link::single ('about', 'About'));
$i18n->prefix = '/en';
$expected = "<li><a href=\"/en/about\">About</a></li>\n";
$this->assertEquals ($expected, Link::single ('about', 'About'));
$expected = "<li><a href=\"/en/news\">News</a></li>\n";
$this->assertEquals ($expected, Link::single ('news', 'News'));
}
function test_single_with_url_and_nested () {
Link::negotiation_method ('url');
Link::url_style ('nested');
$i18n = new I18n;
Link::i18n ($i18n);
$i18n->prefix = '';
Link::current ('index');
$expected = "<li class=\"current\"><a href=\"/index\">Home</a></li>\n";
$this->assertEquals ($expected, Link::single ('index', 'Home'));
$i18n->prefix = '/en';
$expected = "<li class=\"current\"><a href=\"/index\">Home</a></li>\n";
$this->assertEquals ($expected, Link::single ('index', 'Home'));
// Note: This would have presumably forwarded to /{$i18n->language} in any case
$i18n->prefix = '';
$expected = "<li><a href=\"/en\">English</a></li>\n";
$this->assertEquals ($expected, Link::single ('en', 'English'));
$i18n->prefix = '/en';
$expected = "<li><a href=\"/en\">English</a></li>\n";
$this->assertEquals ($expected, Link::single ('en', 'English'));
$i18n->prefix = '';
$expected = "<li><a href=\"/en/about\">About</a></li>\n";
$this->assertEquals ($expected, Link::single ('about', 'About'));
$i18n->prefix = '/en';
$expected = "<li><a href=\"/en/about\">About</a></li>\n";
$this->assertEquals ($expected, Link::single ('about', 'About'));
$expected = "<li><a href=\"/en/about/news\">News</a></li>\n";
$this->assertEquals ($expected, Link::single ('news', 'News'));
}
}
?>

Navigation Reference

A breakdown of expected values for the various combinations of the page_url_style and negotiation_method settings.

page_url_style=flat, negotiation_method!=url

Expected settings for URLs:

url					page id		lang		prefix
--------------------------------------------------
/					index		en			''
/index				index		en			''
/about				about		en			''
/about/news			about		en			''
/fr					fr			en			''
/fr/about			fr			en			''
/fr/about/news		fr			en			''

Expected URL format in navigation/*:

/about
/news

In this case, we never need to worry about the prefix containing a value because the language isn't being determined by the URL at all.

page_url_style=nested, negotiation_method!=url

Expected settings for URLs:

url					page id		lang		prefix
--------------------------------------------------
/					index		en			''
/index				index		en			''
/about				about		en			''
/about/news			news		en			''
/fr					fr			en			''
/fr/about			about		en			''
/fr/about/news		news		en			''

Expected URL format in navigation/*:

/about
/about/news

In this case, we never need to worry about the prefix containing a value because the language isn't being determined by the URL at all.

page_url_style=flat, negotiation_method=url

Expected settings for URLs:

url					page id		lang		prefix
--------------------------------------------------
/					index		en			''
/index				index		en			''
/about				about		en			''
/about/news			about		en			''
/fr					fr			fr			/fr
/fr/about			about		fr			/fr
/fr/about/news		about		fr			/fr

Expected URL format in navigation/*:

/ -> redirect to /{$i18n->language} via user setup
/fr
/fr/about

This is the only case where we actually need the prefix in the URL, and even then, only if the page ID doesn't match one of the language codes. Otherwise we would have URLs like /fr/fr which we want to avoid as well.

page_url_style=nested, negotiation_method=url

Expected settings for URLs:

url					page id		lang		prefix
--------------------------------------------------
/					index		en			''
/index				index		en			''
/about				about		en			''
/about/news			news		en			''
/fr					fr			fr			/fr
/fr/about			about		fr			/fr
/fr/about/news		news		fr			/fr

Expected URL format in navigation/*:

/ -> redirect to /{$i18n->language} via user setup
/fr
/fr/about
/fr/about/news

In this case, we don't need the prefix because the top-level page in the nested URL will already contain it, so we should leave it out to be safe and not end up with URLs like /fr/fr/about/news.

<?php
/**
* Displays a single section of the navigation as a
* bulleted list, with `class="current"` added to
* the current page's `<li>` element for custom styling.
*/
$n = Link::nav ();
$section = $n->node ($data['section']);
if (is_array ($section->children)) {
echo '<ul>';
foreach ($section->children as $item) {
echo Link::single ($item->attr->id, $item->data);
}
echo '</ul>';
}
?>
<?php
/**
* Displays the top-level navigation as a bulleted list,
* with `class="current"` added to the current page's
* `<li>` element for custom styling.
*/
if (conf ('I18n', 'multilingual')) {
echo $this->run ('navigation/section', array ('section' => $i18n->language));
return;
}
$n = Link::nav ();
echo '<ul>';
foreach ($n->tree as $item) {
echo Link::single ($item->attr->id, $item->data);
}
echo '</ul>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment