4

I am trying to create a a box in a template in Joomla! that will display all of the keywords and link them to their appropriate search page. I have a menu item set, however, I don't want to hard-code the menu item into the template, so I want to use the JRoute object to generate the SEF url.

I am using this function:

JRoute::_('index.php?option=com_search&searchword='.$keyword);

or this:

JRoute::_('index.php?option=com_search&view=search&searchword='.$keyword);

however, this generates a url like this:

/component/search/?searchword=africa

when it ought to create a search url like this:

/searchmenuitem?searchword=africa

I have searched extensivly online and havn't found a solution to this problem. Any ideas would be greatly appreciated.

Ok, so some additional information for you.. I am only experiencing the problem when I try and route the URL from a template in com_content. If I try and route the url from a template in com_search everything works perfectly. So, what is it about com_content that is causing this to not work properly?

thanks! david

David Barratt
  • 546
  • 1
  • 6
  • 24

3 Answers3

4

In joomla administration page go to the menu item you've chosen for the search results page and get the id of that menu item (itemId).

Than you can try using:

JRoute::_('index.php?option=com_search&view=search&Itemid=256&searchword=asdsadasdsa');

or even

JRoute::_('index.php?Itemid=256&searchword=asdsadasdsa');

both should result in: /searchmenuitem.html?searchword=asdsadasdsa

EDIT: To make it more comforable you could add itemId as a param to your template.

There is another way, where u can get the itemId from the database (this method is required on multilingual websites). Let me know if you want it.

EDIT2: Here it is:

$db   =& JFactory::getDBO();
$lang =& JFactory::getLanguage()->getTag();
$uri  = 'index.php?option=com_search&view=search';

$db->setQuery('SELECT id FROM #__menu WHERE link LIKE '. $db->Quote( $uri .'%' ) .' AND language='. $db->Quote($lang) .' LIMIT 1' );

$itemId = ($db->getErrorNum())? 0 : intval($db->loadResult());
WooDzu
  • 4,771
  • 6
  • 31
  • 61
  • yeah, that would be really helpful as the menu id could change on the site. – David Barratt Aug 11 '11 at 13:35
  • if your are not using multi-language feature, you can omit checking language in the db query – WooDzu Aug 11 '11 at 15:00
  • ugh.. it doesn't work. The first example you gave throws a blank page and the second you gave just adds it to the end of the menu path to the article. – David Barratt Aug 12 '11 at 13:50
  • can you tell me how the final uri looks like and whether the itemId from the query is correct (matches the one from Manu Manager) – WooDzu Aug 12 '11 at 14:07
  • yep, the first one you provided.. `JRoute::_('index.php?option=com_search&view=search&itemId=256&searchword=asdsadasdsa');` returns this url (in an article template): `/component/search/?itemId=1008&searchword=natural%20disasters` this returns a blank page. the second one you gave: `JRoute::_('index.php?itemId=256&searchword=asdsadasdsa');` returns this url: `/[category_menu]/[category_menu]?itemId=1008&searchword=natural%20disasters` (I used [category_menu] as a placeholder. – David Barratt Aug 12 '11 at 14:19
  • nevermind... figured it out.. you had the name of the variable wrong.. it's "Itemid" not "itemId" thanks! – David Barratt Aug 12 '11 at 14:28
1

I use this kind of method to get a menu item id of specific component and view

 function getSearchItemId() {

        $menu = &JSite::getMenu();
        $component = &JComponentHelper::getComponent('com_search');
        //get only com_search menu items
            $items  = $menu->getItems('componentid', $component->id);

        foreach ($items as $item) {
            if (isset($item->query['view']) && $item->query['view'] === 'search') {
                        return $item->id;

                    }
        }

        return false;

        }

Then I use this method to get the sef url

function getRouteUrl($route) {

    jimport('joomla.application.router');

    // Get the global site router.
    $config = &JFactory::getConfig();
    $router = JRouter::getInstance('site');
    $router->setMode($config->getValue('sef', 1));

    $uri    = &$router->build($url);
    $path   = $uri->toString(array('path', 'query', 'fragment'));

    return $path;
}

This just works in any template.

use like this

$itemid = getSearchItemId();

//returns valid sef url
$url = getRouteUrl('index.php?Itemid='.$itemid);

You really do not need to do sql on the menu table to get ids. Just search the menu object.

Andy N
  • 1,013
  • 9
  • 25
0

Try to create new menu in the joomla backend called for instance 'hidden-menu'. It will never be shown in the front. But it will be used by JRoute Then add to this menu new menuitem called 'searchmenuitem' with link to com_search. That is all. Now you can call

JRoute::_('index.php?option=com_search&view=search&searchword=asdsadasdsa');

and it will be ceonverted into this

/searchmenuitem.html?searchword=asdsadasdsa
Ruslan Polutsygan
  • 4,401
  • 2
  • 26
  • 37
  • I already created a menu item. When using the Search Box Module, it sends it to the correct menu item, but it uses POST to achieve this. so even with a menu item, I'm still getting this `/component/search/?searchword=africa` – David Barratt Jul 27 '11 at 17:51
  • Not clear for me.`it uses POST to achieve this` - to achieve what? SEF url? And what? `I'm still getting this` - what are you getting? Please clarify. – Ruslan Polutsygan Jul 27 '11 at 17:59
  • Sorry, I didn't see your full comment. I had submitted mine before yours had been edited. Can edit the search form? If yes, you cloud just set `methos="get"` and `action=""`. This should help. – Ruslan Polutsygan Jul 27 '11 at 19:22
  • I just looked at the search form as an example of how to route to a search url, but it uses POST not GET, so the example doesn't help. I understand what you are writting, but if you look at my original post, I took exactly what you gave and it does not work properly in Joomla 1.6. – David Barratt Jul 27 '11 at 20:32
  • Ok, so some additional information for you.. I am only experiencing the problem when I try and route the URL from a template in com_content. If I try and route the url from a template in com_search everything works perfectly. So, what is it about com_content that is causing this to not work properly? – David Barratt Jul 28 '11 at 16:13