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

For Joomla! 1.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
User avatar
swese44
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 110
Joined: Wed Jan 11, 2006 2:11 am
Location: Seattle
Contact:

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

Postby swese44 » Sun Mar 16, 2008 5:30 am

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.
Last edited by swese44 on Mon Mar 17, 2008 11:56 pm, edited 1 time in total.
http://joomladigger.com - Free Joomla extensions, news, information and tutorials.
http://www.fellowfishermen.com - Online fishing community with social networking, catches, fishing spot maps, fishing photos and videos, blogs, etc.

User avatar
swese44
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 110
Joined: Wed Jan 11, 2006 2:11 am
Location: Seattle
Contact:

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

Postby swese44 » Mon Mar 17, 2008 5:54 pm

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.
http://joomladigger.com - Free Joomla extensions, news, information and tutorials.
http://www.fellowfishermen.com - Online fishing community with social networking, catches, fishing spot maps, fishing photos and videos, blogs, etc.

User avatar
swese44
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 110
Joined: Wed Jan 11, 2006 2:11 am
Location: Seattle
Contact:

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

Postby swese44 » Mon Mar 17, 2008 9:39 pm

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.
http://joomladigger.com - Free Joomla extensions, news, information and tutorials.
http://www.fellowfishermen.com - Online fishing community with social networking, catches, fishing spot maps, fishing photos and videos, blogs, etc.

User avatar
swese44
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 110
Joined: Wed Jan 11, 2006 2:11 am
Location: Seattle
Contact:

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

Postby swese44 » Thu Mar 20, 2008 12:18 am

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.
http://joomladigger.com - Free Joomla extensions, news, information and tutorials.
http://www.fellowfishermen.com - Online fishing community with social networking, catches, fishing spot maps, fishing photos and videos, blogs, etc.

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4785
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

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

Postby ianmac » Thu Mar 20, 2008 1:37 am

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

User avatar
swese44
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 110
Joined: Wed Jan 11, 2006 2:11 am
Location: Seattle
Contact:

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

Postby swese44 » Thu Mar 20, 2008 3:08 am

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.
http://joomladigger.com - Free Joomla extensions, news, information and tutorials.
http://www.fellowfishermen.com - Online fishing community with social networking, catches, fishing spot maps, fishing photos and videos, blogs, etc.

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4785
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

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

Postby ianmac » Thu Mar 20, 2008 4:14 am

Yeah... sorry... the code is:

$menu = &JSite::getMenu();


Ian

User avatar
swese44
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 110
Joined: Wed Jan 11, 2006 2:11 am
Location: Seattle
Contact:

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

Postby swese44 » Thu Mar 20, 2008 5:09 am

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?
http://joomladigger.com - Free Joomla extensions, news, information and tutorials.
http://www.fellowfishermen.com - Online fishing community with social networking, catches, fishing spot maps, fishing photos and videos, blogs, etc.

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4785
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

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

Postby ianmac » Thu Mar 20, 2008 11:37 am

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

User avatar
swese44
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 110
Joined: Wed Jan 11, 2006 2:11 am
Location: Seattle
Contact:

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

Postby swese44 » Thu Mar 20, 2008 5:48 pm

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?
http://joomladigger.com - Free Joomla extensions, news, information and tutorials.
http://www.fellowfishermen.com - Online fishing community with social networking, catches, fishing spot maps, fishing photos and videos, blogs, etc.

User avatar
swese44
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 110
Joined: Wed Jan 11, 2006 2:11 am
Location: Seattle
Contact:

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

Postby swese44 » Sat Mar 22, 2008 6:10 am

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));
http://joomladigger.com - Free Joomla extensions, news, information and tutorials.
http://www.fellowfishermen.com - Online fishing community with social networking, catches, fishing spot maps, fishing photos and videos, blogs, etc.

kirblam
Joomla! Intern
Joomla! Intern
Posts: 62
Joined: Wed May 03, 2006 4:47 pm

Itemid

Postby kirblam » Wed Jul 16, 2008 6:12 pm

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;

willyram
Joomla! Apprentice
Joomla! Apprentice
Posts: 24
Joined: Mon Jun 30, 2008 8:51 pm

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

Postby willyram » Wed Dec 10, 2008 1:58 am

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!!

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4785
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

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

Postby ianmac » Wed Dec 10, 2008 4:21 am

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

willyram
Joomla! Apprentice
Joomla! Apprentice
Posts: 24
Joined: Mon Jun 30, 2008 8:51 pm

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

Postby willyram » Wed Dec 10, 2008 12:39 pm

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!!

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4785
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

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

Postby ianmac » Wed Dec 10, 2008 2:26 pm

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

willyram
Joomla! Apprentice
Joomla! Apprentice
Posts: 24
Joined: Mon Jun 30, 2008 8:51 pm

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

Postby willyram » Wed Dec 10, 2008 2:49 pm

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!

ayolov
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Mon Aug 18, 2008 11:52 pm
Contact:

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

Postby ayolov » Thu Feb 05, 2009 5:11 pm

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.
Joomla will transform how people use computers - Be part of the revolution!

http://www.mkt400.com PHP scripts & SEO ASAP

User avatar
092098jvm
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 230
Joined: Wed Aug 09, 2006 5:59 pm
Contact:

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

Postby 092098jvm » Fri Mar 13, 2009 1:23 am

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.
[url]KisMac.com[/url]

User avatar
Maurizio1230
Joomla! Intern
Joomla! Intern
Posts: 62
Joined: Tue Sep 18, 2007 1:58 pm
Contact:

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

Postby Maurizio1230 » Sun May 17, 2009 3:24 pm

swese44, I love you. Finally I find it. I was searching for more than 1 month.
I hope it works for my module :)
LinuxInvent.info - If You Are Free to Think with Your Head, There is a Penguin in Your Heart.

circsoc
Joomla! Intern
Joomla! Intern
Posts: 61
Joined: Tue Feb 19, 2008 2:53 am

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

Postby circsoc » Mon Feb 22, 2010 10:08 pm

I know this is an old thread, but just wanted to say thanks. This information is still relavent and a huge help to me.

modhumiah
Joomla! Intern
Joomla! Intern
Posts: 56
Joined: Wed Feb 24, 2010 2:14 am

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

Postby modhumiah » Wed Feb 24, 2010 2:25 am

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.

rodolfo
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Tue Jun 17, 2008 5:57 pm

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

Postby rodolfo » Wed May 05, 2010 10:44 pm

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;

satzdom
Joomla! Apprentice
Joomla! Apprentice
Posts: 29
Joined: Mon Aug 24, 2009 6:22 am

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

Postby satzdom » Thu May 27, 2010 4:38 am

I have an article on this. I think it will help to resolve it.

http://www.tutorials2learn.com/2010/05/ ... es-joomla/
Warm Regards,
Satheesh K Dominic

http://www.tutorials2learn.com

panosgr
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 147
Joined: Fri Sep 28, 2007 8:40 pm

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

Postby panosgr » Fri Jul 02, 2010 9:53 pm

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

User avatar
chetanmadaan
Joomla! Ace
Joomla! Ace
Posts: 1553
Joined: Sun Feb 10, 2008 1:37 pm
Location: Little Office
Contact:

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

Postby chetanmadaan » Tue Mar 01, 2011 10:34 am

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!
Chetan Madaan - JM-Experts
http://www.Jm-Experts.com

perspective
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 154
Joined: Thu Oct 04, 2007 7:45 pm

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

Postby perspective » Tue Jun 14, 2011 1:40 am

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?

seoras
Joomla! Apprentice
Joomla! Apprentice
Posts: 14
Joined: Thu Aug 12, 2010 12:47 pm

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

Postby seoras » Sun Aug 14, 2011 10:43 am

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?

kballou
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 168
Joined: Mon Oct 08, 2007 10:51 pm

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

Postby kballou » Sat Aug 20, 2011 7:05 pm

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,

fexnok
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Tue Apr 27, 2010 2:47 pm

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

Postby fexnok » Thu Oct 13, 2011 12:02 pm

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!!!


Return to “Joomla! 1.5 Coding”

Who is online

Users browsing this forum: No registered users and 3 guests