Page 1 of 2

J1.5 Plugin: How to get content id, Itemid?

Posted: Sun Mar 16, 2008 5:30 am
by swese44
I've been searching the forums, API references, etc. and I'm really having a tough time finding good information. Hopefully some developers can answer these questions directly.

I'm updating a plugin to Joomla! 1.5 from 1.0.x. Using the J1.5 framework properly, how do I do the following?

Get the current content item's content id?
Get the current content item's Itemid? (there are probably multiple ways to do this depending on menu structure, could somebody go into detail on this one? I know there was a big debate on the Itemid functionality a while back in J1.0.x, it would be nice if somebody could clarify how it is used exactly)
Use JRoute to build the URL to a content item? Can I always use JRoute, or should I only use JRoute if SEF URLs are enabled?
How do I check if SEF URLs are enabled? Is it still $mosConfig_sef like J1.0.x?

Thanks!

By the way, the J1.5 plugin tutorial in the API reference is so generic that it is no help at all, it only shows how to set up the basic format of a plugin. It sure would be helpful if a knowledgeable developer would write out a better Plugin tutorial that outlined a bunch of common tasks and best practices, available variables, and anything else that somebody would need to know or would find useful to know while writing plugins.

Re: J1.5 Plugin: How to get content id, Itemid?

Posted: Mon Mar 17, 2008 5:54 pm
by swese44
Here's what I have so far:
swese44 wrote:Get the current content item's content id?
$article->id;
swese44 wrote:Get the current content item's Itemid?
I found some info on JRoute which makes the Itemid not required for SEF URLs, but I think I still need it to build non-SEF URLs.
swese44 wrote:Use JRoute to build the URL to a content item? Can I always use JRoute, or should I only use JRoute if SEF URLs are enabled?
Here's what I've figured out from the documentation:

Code: Select all

$link = JRoute::_( 'index.php?view=article&id='. $contentid .':'. $contentalias );
This doesn't seem to work for non-SEF URLs. What is the best way to build the URL to the current article, whether SEF URLs are turned on or not? Ideally I would want to build the URL using a standard Itemid, so no matter which route a visitor took to get to the article my plugin would still create a single link to it - a "permalink".
swese44 wrote:How do I check if SEF URLs are enabled? Is it still $mosConfig_sef like J1.0.x?
Still haven't figured this one out.

Re: J1.5 Plugin: How to get content id, Itemid?

Posted: Mon Mar 17, 2008 9:39 pm
by swese44
I found this in the documentation:

Code: Select all

getItemid (line 1028)
Deprecated, use ContentHelper::getItemid instead.
    * see: ContentHelperRoute::getArticleRoute()
    * deprecated: As of version 1.5
    * since: 1.0

void getItemid ( $id)
    * $id
It says that getItemid is deprecated and to use ContentHelper::getItemid instead, and see ContentHelperRoute::getArticleRoute(). I tried using ContentHelperRoute::getArticleRoute() but it doesn't include an Itemid in the URL. I can't seem to figure out how to use ContentHelper::getItemid, the error 'Fatal error: Class 'ContentHelper' not found' and there is no documentation on it. Is this an error in the documentation?

Could a developer give me a hand here? Thanks.

Re: J1.5 Plugin: How to get content id, Itemid?

Posted: Thu Mar 20, 2008 12:18 am
by swese44
Well I've figured out that the JRoute() function can be used whether SEF URLs are enabled or not, if not then the non-SEF URL is returned. Here is how I implemented the JRoute function:

Code: Select all

$link = JRoute::_('index.php?option=com_content&view=article&id='. $article->slug .'&catid='. $article->catslug .'&Itemid='. JApplication::getItemid($article->id));
As long as the non-SEF URL is properly recreated using the id slug and category slug then the proper URL will be generated.

I'm also using JApplication::getItemid(), which is deprecated but still available natively for Joomla! 1.5. Is this function scheduled to be removed from the native code any time soon? I don't understand why it would be deprecated if there is no replacement.

The only question I still have is how to tell if SEF URLs are enabled. In Joomla! 1.0.x it was $mosConfig_sef but this is not available in Joomla! 1.5.

Re: J1.5 Plugin: How to get content id, Itemid?

Posted: Thu Mar 20, 2008 1:37 am
by ianmac
I think you want to do something like:

$menu = &JFactory::getMenu();
$defaultMenu = & $menu->getDefault();

$itemid = $defaultMenu->id;

If you are creating links to content items, you are better to use the ContentHelperRoute::getArticleRoute() method that is defined in components/com_content/helpers/route.php

Ian

Re: J1.5 Plugin: How to get content id, Itemid?

Posted: Thu Mar 20, 2008 3:08 am
by swese44
Wouldn't that just return the Itemid of the current page? I can't tell if your solution would be correct, it gives me the following error:

Code: Select all

Fatal error: Call to undefined method JFactory::getmenu()
I need to create URLs that never change, regardless of how a user navigated to the current page (or if on a content listing page where multiple articles are displayed). Sort of permalink URLs with the most logical Itemid, based on the main menu.

Re: J1.5 Plugin: How to get content id, Itemid?

Posted: Thu Mar 20, 2008 4:14 am
by ianmac
Yeah... sorry... the code is:

$menu = &JSite::getMenu();


Ian

Re: J1.5 Plugin: How to get content id, Itemid?

Posted: Thu Mar 20, 2008 5:09 am
by swese44
Thanks but that example returns an Itemid of 1 every time, it doesn't find the best Itemid based on the main menu.

I think I'll stick with JApplication::getItemid($article->id), it looks like it's the only function that scans the main menu and returns the best-matching Itemid for the given article. Is there a reason this function would be deprecated if there's no replacement?

Re: J1.5 Plugin: How to get content id, Itemid?

Posted: Thu Mar 20, 2008 11:37 am
by ianmac
So you tried also class ContentHelperRoute and nothing in there was helpful? It looks to me like this should do what you are after (IMO).

Ian

Re: J1.5 Plugin: How to get content id, Itemid?

Posted: Thu Mar 20, 2008 5:48 pm
by swese44
ContentHelperRoute::getArticleRoute($article->id) returns the following:

Code: Select all

index.php?option=com_content&view=article&id=62
This seems to be a Joomla! 1.0.x version URL though. It also doesn't have an Itemid. Maybe I'm not using it correctly?

Re: J1.5 Plugin: How to get content id, Itemid?

Posted: Sat Mar 22, 2008 6:10 am
by swese44
Ahh, got it. I found an example of the function being properly used in another extensions:

Code: Select all

$link = JRoute::_(ContentHelperRoute::getArticleRoute($article->slug, $article->catslug, $article->sectionid));

Itemid

Posted: Wed Jul 16, 2008 6:12 pm
by kirblam
Just in case this helps anyone looking through this thread, I also tried that itemid code above and it always brought back 1. This seems to work though:

Code: Select all

$menus = &JSite::getMenu();
$menu  = $menus->getActive();
$itemid = $menu->id;

Re: J1.5 Plugin: How to get Itemid? - Current state of affairs?

Posted: Wed Dec 10, 2008 1:58 am
by willyram
Hi everyone! Don't want to waste your team introducing me, but let me just say that after almost one year working with Joomla! I cannot look back and I find myself more and more involved with it.

On a similar note to this thread, I am currently tailoring a tags oriented component that allows you to browse through tags (something like custom properties but simpler). I am learning the Joomla Core in the process so perhaps my question is the outcome of not getting a full knowledge on Joomla classes and the MVC framework.

The point is that on the plugin view, I want to build a "readon" link that points to a best-matching "itemid" for the article so the original intended modules placement is kept: Say you have article "art1" and you tag it with "tag1, tag2, tag3". The idea is to browse content by section / category blog view accessible through menu items, and alternatively by a new component that allows you to view by artciles that contain specific tags (like navigating through a tag cloud).

So on the plugin to build the excerpts for tags-browsing, I find myself I have the article id, but no clues on the itemids. What I am trying to do is to get the best-matching itemid for the article id, if that article happens to appear by itself linked in a menu, or as part of a category/section blog view on a menu option.

Right now I am getting excellent results using JApplication::getItemid($article_id), but I am learning here that this function is deprecated and I'd better use getArticleRoute instead. The point as other user states here, is that I cannot get the "best-matching" itemid with it, I get either 1 or null.

So, in a few words, question is: given an article_id, what is the most efficient and current way of getting the best-matching menu option that would display the same article (that is, the old itemid) so I can ensure the intended modules placement is kept as designed?

Feel free to post technically speaking, I'll do the googling to decipher it :)

Thanks in adance, Willie!!

Re: J1.5 Plugin: How to get Itemid? - Current state of affairs?

Posted: Wed Dec 10, 2008 4:21 am
by ianmac
willyram wrote:Hi everyone! Don't want to waste your team introducing me, but let me just say that after almost one year working with Joomla! I cannot look back and I find myself more and more involved with it.

On a similar note to this thread, I am currently tailoring a tags oriented component that allows you to browse through tags (something like custom properties but simpler). I am learning the Joomla Core in the process so perhaps my question is the outcome of not getting a full knowledge on Joomla classes and the MVC framework.

The point is that on the plugin view, I want to build a "readon" link that points to a best-matching "itemid" for the article so the original intended modules placement is kept: Say you have article "art1" and you tag it with "tag1, tag2, tag3". The idea is to browse content by section / category blog view accessible through menu items, and alternatively by a new component that allows you to view by artciles that contain specific tags (like navigating through a tag cloud).

So on the plugin to build the excerpts for tags-browsing, I find myself I have the article id, but no clues on the itemids. What I am trying to do is to get the best-matching itemid for the article id, if that article happens to appear by itself linked in a menu, or as part of a category/section blog view on a menu option.

Right now I am getting excellent results using JApplication::getItemid($article_id), but I am learning here that this function is deprecated and I'd better use getArticleRoute instead. The point as other user states here, is that I cannot get the "best-matching" itemid with it, I get either 1 or null.

So, in a few words, question is: given an article_id, what is the most efficient and current way of getting the best-matching menu option that would display the same article (that is, the old itemid) so I can ensure the intended modules placement is kept as designed?

Feel free to post technically speaking, I'll do the googling to decipher it :)

Thanks in adance, Willie!!
Have you tried ContentHelperRoute::getArticleRoute() ?

You have to do require_once( JPATH_SITE . DS . 'components' . DS . 'com_content' . DS . 'helpers' . DS . 'route.php' ); first.

Ian

Re: J1.5 Plugin: How to get Itemid? - Current state of affairs?

Posted: Wed Dec 10, 2008 12:39 pm
by willyram
ianmac wrote: Have you tried ContentHelperRoute::getArticleRoute() ?
Thanks for your fast answer Ian, it works if I pass the catid and sectionid, else it does not find the article within the menu system which is what I want. I looked at getItemid and it loads the article row table, perhaps something not that efficient for just getting the catid and sectionid from an article id.

Any other way to get this data or else find a matching menu-item out of an article id??

Tks!!

Re: J1.5 Plugin: How to get Itemid? - Current state of affairs?

Posted: Wed Dec 10, 2008 2:26 pm
by ianmac
willyram wrote:
ianmac wrote: Have you tried ContentHelperRoute::getArticleRoute() ?
Thanks for your fast answer Ian, it works if I pass the catid and sectionid, else it does not find the article within the menu system which is what I want. I looked at getItemid and it loads the article row table, perhaps something not that efficient for just getting the catid and sectionid from an article id.

Any other way to get this data or else find a matching menu-item out of an article id??

Tks!!
Does the article row not have the category id and section id? That would just be loading a single row from the database - actually, two fields, searching on an indexed field. IMO, that should be pretty quick and isn't too inefficient at all.

Ian

Re: J1.5 Plugin: How to get Itemid? - Current state of affairs?

Posted: Wed Dec 10, 2008 2:49 pm
by willyram
ianmac wrote: Does the article row not have the category id and section id? That would just be loading a single row from the database - actually, two fields, searching on an indexed field. IMO, that should be pretty quick and isn't too inefficient at all.
Ian
Yes, actually I realized that on a few lines of code above (not mine...) I was loading title, id and introtext, so I added catid and section id. I also added alias so I now build a slug and getArticleRoute now works wonderfully, giving nice, readable SEF urls.

My only hope is that this keeps working okay while I add more things. ...

Tks for the help!! Really got me into getting to know more on Joomla!

Re: J1.5 Plugin: How to get content id, Itemid?

Posted: Thu Feb 05, 2009 5:11 pm
by ayolov
Hi everyone, I'll post the solution to a problem I had related to itemid, maybe it will help someone.

I was using dynamic links created with mod_categories, the problem was that the file output did not include an itemid, the itemid I managed to get was the one in the current user's webpage, not the links that were generated.

I had:
require_once( JPATH_SITE . DS . 'components' . DS . 'com_content' . DS . 'helpers' . DS . 'route.php' );
at the beginning of my script and then added the following code where the link was going to be displayed:


$menuname = $params->get( 'menu', 'mainmenu' ); // I used mainmenu to get the itemid, you can use something else by changing 'mainmenu'
$disp=ContentHelperRoute::getArticleRoute($menuname,$item->id); // this gets the whole string, now we just need to strip the information we want, the $item->id is where I have the id from the article, you may use your own.

$finder_itemid= explode('&',$disp); // create an array
foreach ($finder_itemid as $aa =>$value) {

if (strstr($value,'Itemid=')) {
$split_id= explode('=',$value);// there might be more elegant ways to get the value, but this is quick and does the job!
$final_id=$split_id[1]; // this variable will have the $itemid value
} //finder_itemid
}// foreach

hope this is useful, I had a hard time finding this one out!

Mario.

Re: J1.5 Plugin: How to get content id, Itemid?

Posted: Fri Mar 13, 2009 1:23 am
by 092098jvm
Hey, thanks! That last bit of code produced something close to what I've been trying to get for hours:
  • index.php?option=com_content&view=article&id=mainmenu&Itemid=51
Is there any way to display the SEF URL? Or, ideally, get it into a PHP variable? I'm trying to echo the current page SEF URL in a module and it seems incredibly difficult.

Re: J1.5 Plugin: How to get content id, Itemid?

Posted: Sun May 17, 2009 3:24 pm
by Maurizio1230
swese44, I love you. Finally I find it. I was searching for more than 1 month.
I hope it works for my module :)

Re: J1.5 Plugin: How to get content id, Itemid?

Posted: Mon Feb 22, 2010 10:08 pm
by circsoc
I know this is an old thread, but just wanted to say thanks. This information is still relavent and a huge help to me.

Re: J1.5 Plugin: How to get content id, Itemid?

Posted: Wed Feb 24, 2010 2:25 am
by modhumiah
Shouldn't there be some tutorial on this kind of stuff. Took me more than half an hour to find this. Thanks anyway, It was very helpfull.

Re: J1.5 Plugin: How to get content id, Itemid?

Posted: Wed May 05, 2010 10:44 pm
by rodolfo
This is an old thread, but I think you might find this relevant and valuable.
I tried usin avolov's code, but it produced Itemid=51 no matter what.

To obtain the Itemid of the current page on Joomla 1.5, try this:
$itemID = JSite::getMenu()->getActive()->tree[0];

It's really the same Kirblan's code from and older post, but on a single line.
$menus = &JSite::getMenu();
$menu = $menus->getActive();
$itemid = $menu->id;

Re: J1.5 Plugin: How to get content id, Itemid?

Posted: Thu May 27, 2010 4:38 am
by satzdom
I have an article on this. I think it will help to resolve it.

http://www.tutorials2learn.com/2010/05/ ... es-joomla/

Re: J1.5 Plugin: How to get content id, Itemid?

Posted: Fri Jul 02, 2010 9:53 pm
by panosgr
satzdom wrote:I have an article on this. I think it will help to resolve it.

http://www.tutorials2learn.com/2010/05/ ... es-joomla/
Very nice tutorial... thanks :D

Re: J1.5 Plugin: How to get content id, Itemid?

Posted: Tue Mar 01, 2011 10:34 am
by chetanmadaan
Alright... Getting into this discussion..

we have a module which is using

Code: Select all

$link = JRoute::_(ContentHelperRoute::getArticleRoute($article->slug, $article->catslug, $article->sectionid)); 
to display the link... we want to add a override Itemid to it???

right now its automatically adding the itemid at the end of the url from the page it is on!

Any tips!

Re: J1.5 Plugin: How to get content id, Itemid?

Posted: Tue Jun 14, 2011 1:40 am
by perspective
Let's say that I have my main navigation, and I used the above methods to pull the parent so my parent id is 115.

Parent has children, is there away to render the children from the parent? Kind of like making menu modules, without having to make them?

Re: J1.5 Plugin: How to get content id, Itemid?

Posted: Sun Aug 14, 2011 10:43 am
by seoras
I'm trying to debug a component in Joomla (1.5.15) that is using:
global $Itemid;
to get it's itemid.

I tried using
JSite::getMenu()->getActive()->id ;
however it too fails.

I've worked out the problem is caused by the setting "Global Configuration->Search Engine Friendly URLs->Yes"
If I change this setting to "No", both methods work fine.

Is there a method for getting the Itemid when SEF is switch on?

Re: J1.5 Plugin: How to get content id, Itemid?

Posted: Sat Aug 20, 2011 7:05 pm
by kballou
Doesn't JRequest::getVar('Itemid') work? It is possible, of course, that there is no current item id. Nothing prevents someone from manually entering a non-SEF URL that does not correspond to a particular menu item,

Re: J1.5 Plugin: How to get content id, Itemid?

Posted: Thu Oct 13, 2011 12:02 pm
by fexnok
God I never thought I was going to find my answer on this old post!
I've tried everything suggested above but the one it really worked for me problem was JRequest::getVar('id');

Many thanks!!!