Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

All other Modules /

Discuss all other Modules here.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Trying to access slugized Album Name


Reply


2 Posts   805 Views

Avatar
phawley

Community Member, 21 Posts

17 September 2010 at 5:38am

Hello all. I'm using image_gallery_2.3 with SS 2.4.1 and I'm trying to dynamically create h1 selectors specific to each album, and obviously these CSS selectors need to be without spaces (i.e. slugized). So I would love to be able to call $URLSegment and get the name of the Album, but it only returns the main Photo Gallery SS page, which makes sense:

http://localhost:8888/about/photo-albums/album/a-look-back/

returns photo-albums

So I've added this function to ImageGalleryPage.php:

public function SlugizedAlbumTitle()
{
$str = $this->CurrentAlbum()->AlbumName;
$str = strtolower(trim($str));
$str = preg_replace('/[^a-z0-9-]/', '-', $str);
$str = preg_replace('/-+/', "-", $str);
return rtrim($str, '-');
}

and it works just as I need, but obviously I'd much prefer to put a function in to my *own* code (e.g. /mysite/code/Page.php), but there I'm stumped. I've tried Director::URLParam('ID'), but with no magic.

Any tips would be greatly appreciated,
Patrick

Avatar
UncleCheese

Forum Moderator, 4096 Posts

17 September 2010 at 6:17am

Couple things..

First, the album "slug" is just the Title property of the album's related folder.

$album->Folder()->Title;

Second, to generate slugs, just use SiteTree::generateURLSegment();

Lastly, rather than hacking the core of the module, just subclass it and add your custom method.

---------------
Silverstripe tips, tutorials, screencasts, and more. http://www.leftandmain.com