As of concrete5.6, the PHP port of URLify.js is used to generate url slugs from page titles. This should make for a more robust and predictable conversion from Page title to URL. By default URLify includes a base set of words to omit from generated urls.
public static $remove_list = array (
'a', 'an', 'as', 'at', 'before', 'but', 'by', 'for', 'from',
'is', 'in', 'into', 'like', 'of', 'off', 'on', 'onto', 'per',
'since', 'than', 'the', 'this', 'that', 'to', 'up', 'via',
'with'
);
While this may be good for most situations, there may be times where you might want to add or remove words from this list. concrete5.6 overrides to the rescue!
First lets override the TextHelper::urlify() method to add a couple additional words to the remove list.
Overriding the Text Helper
- Create a text.php file that we'll use to override the core TextHelper::urlify() method in the top level helpers directory: (your_site_root)/helpers/text.php.
- Override the TextHelper::urify() method with our new urlify method that includes our additional words.
Over in (your_site_root)/helpers/text.php:
defined('C5_EXECUTE') or die("Access Denied.");
class TextHelper extends Concrete5_Helper_Text {
public function urlify($handle) {
Loader::library('3rdparty/urlify');
// Add 'is' and 'was' to the words to remove
URLify::remove_words(array('is', 'was'));
$handle = URLify::filter($handle);
return $handle;
}
}
Because the url_slug.php tools file does not currently use the TextHelper::urlify() method, and instead calls URLify::filter directly, let's go ahead and modify this as well.
Overriding the Tools File
- Create a directory pages in the top
level tools directory and add a file
url_slug.php:
(your_site_root)/tools/pages/url_slug.php. - Copy the code from the core tools file and add our additional words to remove.
Over in (your_site_root)/tools/pages/url_slug.php:
defined('C5_EXECUTE') or die("Access Denied.");
if(Loader::helper('validation/token')->validate('get_url_slug', $_REQUEST['token'])) {
Loader::library('3rdparty/urlify');
// Add 'is' and 'was' to the words to remove
URLify::remove_words(array('is', 'was'));
print URLify::filter($_REQUEST['name']);
}
Congratulations, now our Urls will be free from the words 'is' and 'was'.
Another use case for overriding the url slug generation would be to add additional character maps for different languages.
Take a look at the original URLify PHP library for more information.
Hope this helps you get the url slugs you need and some insight into concrete5.6's shiny new overrides system.
Best Wishes and Happy Hacking!