Jump to content

Blocktopmenu links class active


antoinedan

Recommended Posts

Hi! In the module blocktopmenu i need the class active.

I test this code but not function. What can I do? Thanks!


case 'LNK':

    $selected = ($link->getPageLink == $page_name ) ? ' class="sfHover"' : '';

	 $link = MenuTopLinks::get((int)$id, (int)$id_lang, (int)$id_shop);

	 if (count($link))

	 {

	  if (!isset($link[0]['label']) || ($link[0]['label'] == ''))

	  {

	   $default_language = Configuration::get('PS_LANG_DEFAULT');

	   $link = MenuTopLinks::get($link[0]['id_linksmenutop'], $default_language, (int)Shop::getContextShopID());

	  }

	  $this->_menu .= '<li'.$selected.' ><a href="'.$link[0]['link'].'"'.(($link[0]['new_window']) ? ' target="_blank"': '').'>'.$link[0]['label'].'</a></li>'.PHP_EOL;

	 }

	 break;
Link to comment
Share on other sites

I have the same problem! My links "index", "new-products" and "prices-drop" aren't "class="sfHoverForce" when i'm on the page but the others (products category and cms) are "class="sfHoverForce" when i click on. If someone has a solution for that, it would be awesome!

Link to comment
Share on other sites

you need to edit code for each kind of page.

but the question is: how you added links to top horizontal menu to: new products, prices drop etc. im asking because by default there is no possibility to add these links there

 

(a part of "left" box from module configurataion page)

7fhWAdU.png

as you can see there is no option to add these links

Link to comment
Share on other sites

well, in this case is almost impossible to achieve what you expect.

If I were you I will create these links manually, then it will be much easier to achieve it.

<li class="'.($this->context->smarty->tpl_vars['page_name']->value=="new-products" ? 'selected':'').'"><a href="'.$link->getPageLink('new-products').'">New products</a></li>

add this link (and any other link you want) to $this->_menu .=

Link to comment
Share on other sites

Your solution not works, so i've add this on blocktopmenu.tpl:

<li {if $page_name == 'new-products'} class="sfHoverForce"{/if}><a href="{$link->getPageLink('new-products')}">{l s='Nouveautés'}</a></li>
<li {if $page_name == 'index'} class="sfHoverForce"{/if}><a href="{$link->getPageLink('index')}">{l s='Accueil'}</a></li>
<li {if $page_name == 'prices-drop'} class="sfHoverForce"{/if}><a href="{$link->getPageLink('prices-drop')}">{l s='Promotions'}</a></li>

The links appear but when i click on one of them, this is the link "Index" who is always in class="sfhoverForce". The two others not. Why?

Link to comment
Share on other sites

you're talking about "hover" effect, and we are talking here about "active" page.

 

it mean something like if you're in "iPods" category - then in top menu iPods menu item has got "active" class.

In this case it is easy to achieve.

 

but for links defined manually it isn't 

Link to comment
Share on other sites

I see ...
The easy way is using get variable.
It mean you should add an unique id for each custom link that you add to the blocktopmenu, e.g: ?id_lnk=x
If your original link was ./myCustomLink , then you should type it like this ./myCustomLink?id_lnk=x
where x = your unique id (can be a number)

Then you can modify the blocktopmenu module core file like this :

case 'LNK':
    $link = MenuTopLinks::get((int)$id, (int)$id_lang, (int)$id_shop);
    if (count($link))
    {
        if (!isset($link[0]['label']) || ($link[0]['label'] == ''))
        {
            $default_language = Configuration::get('PS_LANG_DEFAULT');
            $link = MenuTopLinks::get($link[0]['id_linksmenutop'], $default_language, (int)Shop::getContextShopID());
        }
        // start-modification
        $id_lnk = Tools::getValue('id_lnk');
        $part = explode('?id_lnk=', htmlentities($link[0]['link']));
        $selected = (isset($id_lnk) && isset($part[1]) && $id_lnk == $part[1]) ? ' class="actived"' : '';
        // end-modification
        $this->_menu .= '<li'.$selected.'><a href="'.htmlentities($link[0]['link']).'"'.(($link[0]['new_window']) ? ' target="_blank"': '').'>'.$link[0]['label'].'</a></li>'.PHP_EOL;
    }
break;

Note: Don't forget to clear smarty cache

Link to comment
Share on other sites

ID would be the better choice, so I need something like this in the menu:

<li id="welcome">...</li>
<li id="shop">...</li>
<li id="contact">...</li>

or maybe

<li id="id_1">...</li>
<li id="id_2">...</li>

Any idea how to archive this in the blocktopmenu.php?

Link to comment
Share on other sites

 

and what about simple js script like:

$(document).ready(function() {
for (var i=0; i <= 9; i++)
{ 
$(".sf-menu > li:nth-child(" + i + ")").attr("id","menu_" + i);
}
});

Because this isn't really unique - when i.e. adding an extra cms-page to the project, I've to update it manually. Isn't it possible to add the page name as a unique id? Or the page ID as css-ID?

Link to comment
Share on other sites

  • 1 year later...

After reading your proposal, I've made some modifications to the makeMenu() function of the blocktopmenu module class as follow:

case 'LNK':
	$link = MenuTopLinks::get((int)$id, (int)$id_lang, (int)$id_shop);
	if (count($link))
	{
		if (!isset($link[0]['label']) || ($link[0]['label'] == ''))
		{
			$default_language = Configuration::get('PS_LANG_DEFAULT');
			$link = MenuTopLinks::get($link[0]['id_linksmenutop'], $default_language, (int)Shop::getContextShopID());
		}
		
		//start mod
		$current = explode('?', Tools::safeOutput(urldecode($_SERVER['REQUEST_URI'])));
		$selected = (Tools::HtmlEntitiesUTF8($link[0]['link']) == $current[0]) ? ' class="sfHover"' : '';
		
		$this->_menu .= '<li'.$selected.'><a href="'.Tools::HtmlEntitiesUTF8($link[0]['link']).'"'.(($link[0]['new_window']) ? ' onclick="return !window.open(this.href);"': '').' title="'.Tools::safeOutput($link[0]['label']).'">'.Tools::safeOutput($link[0]['label']).'</a></li>'.PHP_EOL;
		// end mod
	}
	break;

I've simply parsed the request uri contained into the $_SERVER var and compared it with the link saved into the menu.

 

It works great and may be helpful for others.

Link to comment
Share on other sites

  • 6 months later...

I need to add class to every first level top menu element.

 

 

and what about simple js script like:

$(document).ready(function() {
for (var i=0; i <= 9; i++)
{ 
$(".sf-menu > li:nth-child(" + i + ")").attr("id","menu_" + i);
}
});

 Will it work in 1.6.0.14? Where exactly I should put this code?

Link to comment
Share on other sites

  • 3 months later...

For Prestashop 1.6.1.0:

 

makeMenu() function in the blocktopmenu module (blocktopmenu.php):

case 'LNK':
    $link = MenuTopLinks::get((int)$id, (int)$id_lang, (int)$id_shop);
    if (count($link)) {
        if (!isset($link[0]['label']) || ($link[0]['label'] == '')) {
            $default_language = Configuration::get('PS_LANG_DEFAULT');
            $link = MenuTopLinks::get($link[0]['id_linksmenutop'], $default_language, (int)Shop::getContextShopID());
        }
        $current = explode('?', Tools::safeOutput(urldecode($_SERVER['REQUEST_URI'])));
        $selected = (Tools::HtmlEntitiesUTF8($link[0]['link']) == $current[0]) ? ' class="sfHoverForce"' : '';
        $this->_menu .= '<li'.$selected.'><a href="'.Tools::HtmlEntitiesUTF8($link[0]['link']).'"'.(($link[0]['new_window']) ? ' onclick="return !window.open(this.href);"': '').' title="'.Tools::safeOutput($link[0]['label']).'">'.Tools::safeOutput($link[0]['label']).'</a></li>'.PHP_EOL;
    }
break;

in themes\default-bootstrap\css\modules\blocktopmenu\css\superfish-modified.css:

 

I made the following modification:

.sf-menu > li.sfHoverForce > a{
    background-color: white;
    color: #000 !important;
    text-shadow: none;
}

I would recommend you do a module override for this by placing blocktopmenu.php in in themes\default-bootstrap\modules\blocktopmenu\ otherwise you are going to lose your changes next time you update the module.

 

J

Edited by jucedupp (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...