Auto-Nav with Unique CSS ID

Permalink
Hi all,

So I've been playing around with the Auto-Nav block trying to figure out how to get each list item to have a unique ID so that each item in the list can different CSS attributes.

The first thing I tried was making an integer variable that increased each time the foreach loop started over and changed the code from:

echo '<li class="'.$navSelected.' '.$isFirstClass.'">';

to:

echo '<li id=nav-"'.$uniqueID.'" class="'.$navSelected.' '.$isFirstClass.'">';

Where $uniqueID was a number that increased as the loop continued. This actually works for what I need it to, but I'm not completely satisfied. Rather than having the ID be nav-# I was wanted to have it correspond to the actual link. So I trashed the $uniqueID variable idea and used $ni->getName() instead. Great!... except, a CSS selector can't have spaces.

So my next idea was to use the link's alias. Except, getAlias() isn't a function.

So I found where the getName() function was and created a new function:

function getAlias() {
return $this->cAlias;
}

However... that doesn't actually return anything.

Is there a way to get the Alias of the link and make the list item have that as it's CSS ID?

Thanks,
- IJ

ijessup
 
ijessup replied on at Permalink Reply
ijessup
Well, I feel like there should be an easier way, and this is by no means perfect, but here's what I did.

In the controller.php file for Auto-Nav I added this function @line105:

/**
* Gets the alias of the page or link.
* @return string
*/
function getAlias() {
$alias = $this->cvName;
$underscore = Array("~","`","!","@","#","$","^","*","(",")","+","=","{","}","[","]","/",";",":",".","<",">"," ");
$alias = str_replace($underscore, "_", $alias);
$alias = str_replace("&", "and", $alias);
$alias = strtolower($alias);
return $alias;
}

Then like my previous post, in the header_menu.php file I modified this line:

echo '<li id="'.$ni->getAlias().'" class="'.$navSelected.' '.$isFirstClass.'">';

This is working pretty well, but I think I'm over working the solution like I normally do.

Is there an easier/cleaner way to do this?
ijessup replied on at Permalink Reply
ijessup
Not trying to hog the forum, but I wanted to give this a bump.

If there is a way to get the Alias of the of a page from a link in the Auto_Nav block in a manor that is more precise than what I have coded above, then I have a .patch file I can contribute! :)

Otherwise, I can just contribute what I have so far.
LucasAnderson replied on at Permalink Reply
LucasAnderson
getCollectionHandle()

That should do it... I'm working on testing this and will let you know what I come up with.
ijessup replied on at Permalink Best Answer Reply 1 Attachment
ijessup
Appreciate the help!

However, I'm not sure where to put this function.

I keep getting an undefined function error...

Fatal error: Call to undefined method AutonavBlockItem::getCollectionHandle() in C:\wamp\www\concrete\concrete\blocks\autonav\controller.php on line 106

...if I put it in the controller file as:

function getAlias() {
return $this->getCollectionHandle();
}

Or in the header_menu file as:

echo '<li id="'.$n->getCollectionHandle().'" class="'.$navSelected.' '.$isFirstClass.'">';

Not to toot my own horn, but to show the advantages of having unique IDs...
Along with a theme I'm designing, the attached picture is what I have been able to do so far by making each item have a unique ID.
DavidMIRV replied on at Permalink Reply
DavidMIRV
First of all I think what you want is ->getCollectionID().. I believe in the existing autonav you could modify the AutoNavBlock item subclass to set this on each object (which then in turn gets passed in to the view).