Joomla : how to get the url of a specific Menu itemID? - php

Friends a newbie question.........I need help in getting the URL of a specific Menu itemID. The situation is like this:
I am running Joomla and asking for a user to input for a menu ID and choose a layout for that menu ID.
I want to do something else with this URL of the Menu itemID.
How can I get the URL of this Menu itemID provided by the user?
For Example if the user input is liek $this->get ('menulayoutid'>; and he inputs and ID of 54 then how do I get the URL for Menu ID 54.
Please note: I want to get this URL from within my PHP file and not in the browser so that I can use the value of that URL for some other purpose.
Kindly help.

$itemid = JRequest::getVar('Itemid');
$application = JFactory::getApplication();
$menu = $application->getMenu();
$item = $menu->getItem($itemid);
$link = new JURI($item->link);
$link->setVar('ItemId', $itemid);
Source: http://forum.joomla.org/viewtopic.php?p=1836005

However, we get the Itemid from anywhere (user input, from our own developed module using the "menu item" field type in the xml file as described in the Joomla Docs - Standard form field types)
// get the menuItemId from wherever...
// as described above or as in other posts here and do whatever with that!
$menuItemId = 'fromWherever'; // as an example "107";
// build the link to the menuItemId is just easy and simple
$url = JRoute::_('index.php?Itemid=' . $menuItemId);
i think if we need only a link to a specific menu id, this is the best solution, because we have absolutely less requests and a clean code
this works also in Joomla 3.0, 3.1

I just want to add that if you need to target a specific menu you pass the menu name as an argument to getMenu().
$itemid = JRequest::getVar('Itemid');
$application = JFactory::getApplication();
$menu = $application->getMenu( 'menu-name' );
$item = $menu->getItem($itemid);
$link = new JURI($item->link);
$link->setVar('ItemId', $itemid);

I'm not sure if Joomla changed the way this works since 2.5 or even 1.7 but I spent the worse half of 2 hours looking for this.
Hopefully it helps someone.
$menuID = $params->get('menuItem'); // from module field menu ex. '105'
$js = new JSite;
$menu = $js->getMenu();
$link = $menu->getItem($menuID)->route;
//Returns URL Friendly Link -> menu/article
//Then format it ->
$link = 'http://www.yoursite.com/index.php/'.$link;
echo 'Borrowed Menu Link Path";

When you need to get your active menu item ID in Joomla to display some specific content for only that menu item or just to show the ID of the menu item, insert the following code where you wish to display the active menu item ID:
<?php
$currentMenuId = JSite::getMenu()->getActive()->id;
echo $currentMenuId;
?>

Related

How can i check if an item is part of a specific menu [Joomla 3.3]

i already know how to parse id/name and stuff from my current/active menu-item, but i want to know if it's part of a specific menu for example:
I have this Menu with the name "mainmenu" and type "mainmenu": Home | About | Contact | Testing
If i click on About i want to know if About is part of "mainmenu" - how can i do that?
like this:
$app = JFactory::getApplication();
$menu = $app->getMenu();
$activeitem = $menu->getActive()->id;
// And now the if-statement - which asks if activeitem is part of "mainmenu" and if yes do that, and if not do that...
I hope you know what i mean...
Thanks for any help
You can check menutype using below code -
$app = JFactory::getApplication();
$menu = $app->getMenu();
$menutype = $menu->getActive()->menutype;
if($menutype =='mainmenu'){//check if active menu part of 'mainmenu'
}

Magento seo friendly url in custom module

I would like to create porfolio module, but I need it to be seo friendly, so url should look like:
example.com/portfolio/projectid
where portfolio - module, so it will be index action
projectid - based on field alias
its easy to create example.com/portfolio but how to create controller, or detect what is after?
The same URL hierarchy is in products, so it's category/product
Anybody have idea how to do it?
Below code works for item/id. you can change with your requirement.
$model = Mage::getModel('items/items');
/*Rewrite */
$isSystem = 0; // set 0 for custom url as we have created custom for profile extension
$_itemId = $model->getModuleItemId();//module_item_id field
$_itemName = $model->getTitle();//title field
$_itemName = strtolower(str_replace(" ", "", $_itemName));
// save profile view url rewrite
$viewIdPath = 'item/id'.'/'.$_itemId;
$viewRequestPath = 'items/'.$_itemName;
$viewTargetPath = 'items/index/item/id/'.$_itemId;//controller is itemsController.php, Action is itemAction()
$_coreUrlRewrite = Mage::getModel('core/url_rewrite');
$_coreUrlRewrite->load($viewIdPath, 'id_path'); // check if item path already saved? If yes, $_coreUrlRewrite will contain existing data.
$_coreUrlRewrite->setStoreId($_storeId)
->setIdPath($viewIdPath)
->setRequestPath($viewRequestPath)
->setTargetPath($viewTargetPath)
->setIsSystem($isSystem)
->save();
if(isset($viewRequestPath)){
$model->setUrl($viewRequestPath);
$model->save();
}
/*Rewrite End*/
You can find more info here & here

Joomla get content by ajax

How to get joomla content using ajax? (I want to show content of specyfic page in popup), this is my code: (called by ajax)
$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
if ($option=="com_content" && $view=="article") {
$ids = explode(':',JRequest::getString('id'));
$article_id = $ids[0];
$article =& JTable::getInstance("content");
$article->load($article_id);
echo '<h2>'.$article->get("title").'</h2>';
echo $article->get("introtext"); // and/or fulltext
}
This works fine only for artilces, but the problem is when for example I want to show category, or component
Please see your if condition it checks if option is equal to com_content & view is equal to article only. If view contains category it won't work. So add the conditions in if statement so that your code gets executed.
for category you need to add view=category & like for other components as well.

joomla mvc component redirect with friendly url

I got a code like this in my controller
class MyController extends MyBaseController {
function redirectToCart() {
$link = JRoute::_('index.php?option=com_foo&view=cart');
$this->setRedirect($link);
}
}
I also created a menu associated with my view cart in menu name "View Cart" every time i click this button the url is domainname.com/view-cart but when use redirect in mvc the url is domainname.com/index.php?option=com_foo&view=cart
How can i create a redirect in mvc that it work with the front-end link or at least create a user friendly url
You need to pass Itemid in url to create desired seo:
$link = JRoute::_('index.php?option=com_foo&view=cart&Itemid=your_itemid');
You should turn of SEO and look what Itemid is in your menu item a use it in your url.
Or you can do it dynamically like this:
$itemid = JRequest::getint( 'Itemid' );
Then the $link would look:
$link = JRoute::_('index.php?option=com_foo&view=cart&Itemid='.$itemid);
Or you can take it from whatewer menu item you want like this:
$item = JFactory::getApplication()->getMenu()->getItem( $menuitem );//$menuitem is the id of menu
$itemid = $item->id;

Drupal Menu-How to get Hierarchy

In Drupal how can I get selected menu items hierarchy?
If I selected item3, how can I get the the entire hierarchy...(eg:item1->item2-item3)
If I use $menu_item = menu_get_item();, I will get active menu item name (eg: item3). But I want to get it like (item1->item2->item3).
$parent = menu_get_item($vars['mid']);
$vars['parent'] = $parent['pid'];
$hasParent = menu_get_menu();
$hasParent = $hasParent['items'][$parent]['pid'];
this code will give you the parent menus information and you can print that.

Categories