A little background: while working on the EventList Twitter Status plugin I got frustrated at having to have an Itemid parameter in the plugin parameters. I wanted the script to automatically append the correct Itemid to the URL that gets submitted to Twitter (via TinyURL). However, it seemed that there’s no built-in way to do this using the Joomla API – not when we’re talking about a backend script. If it were a frontend one, we could easily detect the Itemid contextually, i.e. based on which page is being viewed at the time, for example:

JRequest::getVar(‘Itemid‘);

//(detects current page’s Itemid)

Or to detect the URL of a specified menu item:

$menuitem = “34″; //this can be set manually or by your script
$item = JFactory::getApplication()->getMenu()->getItem( $menuitem );
$url = JRoute::_($item->link . ‘&Itemid=’ . $item->id);

However, the above method means that you must already know the Itemid, since the menu id *is* the Itemid. So it’s only good for detecting the URL.

So how do we find out an appropriate Itemid for URLs created on the fly in the backend?

After scouring a lot of posts on the Joomla forum and elsewhere, and also after reading all the available documentation, I got frustrated and just wrote a quick and dirty query:

$queryitemid = “SELECT * FROM #__menu WHERE type=’component’ AND link LIKE ‘%com_eventlist%view=eventlist%’ ORDER BY id ASC LIMIT 1″;
$db->setQuery($queryitemid);
$itemid = $db->loadResult();

if((!itemid) || ($itemid == ”) || ($itemid == NULL)){
//echo “resorting to backup Itemid detection<br>”;
//if default ‘eventlist’ view not found in menu, look for other menu items and use the first one (lowest itemid)

$queryitemid = “SELECT * FROM #__menu WHERE type=’component’ AND link LIKE ‘%com_eventlist%’ ORDER BY id ASC LIMIT 1″;
$db->setQuery($queryitemid);
$itemid = $db->loadResult();

}

if(($itemid != ”) && ($itemid != NULL)){
$itemid = $itemid;
}

else{
//if EventList is now in a menu, use the Itemid set in the plugin parameters.
$itemid = $this->_params->get(‘itemid’);
}

I used a LIKE query to select the “eventlist” view type for the com_eventlist component. I could have avoided use of LIKE by doing a more complex query, and I’m not sure it would have offered much of a performance gain over this LIKE usage. Maybe if this were a script that’s being run a lot, but this only runs when someone adds an event or venue.

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • MySpace
  • RSS
  • Slashdot
  • StumbleUpon
  • Technorati
  • Yahoo! Bookmarks
  • Twitter