2

I need to be able to get a menu item based on the Itemid, then temporarily override one of the values used in the query string in the PHP, and finally generate the link that Joomla would then use. I do NOT want to change the actual menu item, or have alternate menu items, etc.

The actual requirement is a bit complicated, so for the sake of simplicity, let's say that I need to use article ID #1 during the day, and article ID#2 after 6:00PM.

So,the actually menu item is: option=com_content&view=article&id=1&Itemid=100

If it's after 6:00PM, I need something like $menu->getItem($itemid)->link to return option=com_content&view=article&id=2&Itemid=100 instead.

I've been trying this with $app->getMenu(); and new JMenu, but am obviously missing something. I would imagine the code would be something like this:

$menu = $app->getMenu();
$menuitem = $menu->getItem(100);
$menu->setProperties('the_article_id',2);
$templink = $menuitem->link;
// $templink should be: option=com_content&view=article&id=2&Itemid=100
...continue my processing with $templink

To clarify, the highly specialized actual requirement is essentially generating a series of links based on existing menu items, who they are, and where they are on the site, thus using JInput and/or redirecting is not an option. Getting and manipulating the menu object is necessary to come up with the various links I need to display (not actually going to them, unless the user clicks on one which has been displayed to them).

4

There are no hooks for JMenu->getItem() or JMenu->load(). Depending on your interest you could write a plugin which hooks into the routing.

  • If got some code doing the job for me now, but I'm hoping to find the correct Joomla/MVC approach - my new rule with J3.x - use only core code whenEVER possible, lol. – GDP Jan 22 '15 at 20:09
2

A system plugin should be used like this:

public function onAfterRoute()
{
    $app = JFactory::getApplication();
    $jinput = $app->input;
    $time_condition = true; // Set the time condition here!
    if ($time_condition && $jinput->get('option') == 'com_content' && $jinput->get('view') == 'article' && $jinput->get('Itemid') == 100 && $jinput->get('id') == 1)
    {
        $jinput->set('id', '2');
    }
}
  • +1 since this gets around not being able to tap into jMenu, and would work if the example were the actual requirment, but its the menu object that I need to work with for other processing requirements – GDP Jan 23 '15 at 1:23
  • Would you tell us where your codes are placed? – Farahmand Jan 23 '15 at 8:09
  • It's in a system plugin to do some processing, but it is not for regular input/output - it's very specialize processing that would take pages and pages to outline and detail here. – GDP Jan 23 '15 at 13:54
1

Apparently the hooks I need aren't there, so here's the code I used to get it working:

$menu = $app->getMenu();
$menuitem = $menu->getItem(100);
$querystring = str_ireplace('index.php?','',$menuitem->link);
parse_str($querystring, $array);
$array['id'] = 12345;
$temp_url = 'index.php?'.http_build_query($array);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.