views:

35

answers:

1

How can I read the section a certain URI belongs to?

I want to enhance the mod_breadcrumb to put section and category into the HTML. JApplication->getPathway() returns a JPathway which basically holds an assiciative array combining a name and an URL (as $list[]->name and $list[]->link). I think, it should be possible to get the section and category from a link, but don't know how.

A starting point could be the parsing into JURI-Object, but from there I don't know how get get further. Any ideas?

A: 

Pretty straight forward...

I assume you want to add category and section for the article and not your custom component.

Check if requested current URL is for article. If it is for article you know the article ID, use this article Id to go database and get catid from #__content, Use this cat_id to go to #__categories and get section (this is section id), go to #__sections to get the proper section name. All this can be done in 1 sql statement.

$breadcrumbs =& JFactory::getApplication()->getPathway();
$breadcrumbs->addItem("SECTION_NAME", JRoute::_("index.php?option=com_content&view=section&id=SECTION_ID"));
$breadcrumbs->addItem("CATEGOY_NAME", JRoute::_("index.php?option=com_content&view=category&id=CATEGORY_ID"));
$breadcrumbs->addItem("Article");

Alternatively, if you know the URL from the breadcrumb item. You can parse it and get IDS. The trick here is not to get the default URI object by JFactory::getURI() because things will get ugly, use JFactory::getURI('YOU_URI_NAME').

<?php
//  You need to get Your own uri, you do not want to modify default URI
//  because this will messup a lot of things
$uri = JFactory::getURI('MyCustomURI');

//  Test # 1 [ID = SECTION_ID]
$url = "index.php?option=com_content&view=section&id=SECTION_ID";
$uri->parse($url);
echo "CURRENT SECTION = " . (int) $uri->getVar('id');

//  Test # 2 [ID = 123]
$url = "index.php?option=com_content&view=section&id=123";
$uri->parse($url);
echo "CURRENT SECTION = " . (int) $uri->getVar('id');
?>
Alex
MaoPU
Does alternative method solve your dillema?
Alex
Basically yes. To get a category (or section) for a certain item, I used the following code: `$uri = new JURI(); $uri->parse('index.php?option=com_content if ($uri->getVar('view') == 'category') {$categoryID = $uri->getVar('id');} if ($uri->getVar('view') == 'section') {$sectionID = $uri->getVar('id');}`. From there, I could get the category name (or section name) from the DB using a db query. I guess, there is no Joomla API call to solve this, right? So I guess, the problem is almost solved.
MaoPU
This only works for non-SEF URL, right? Is there a way to extend the solution to SEF as well?
MaoPU
Concerning the question itself, the provided solution works for non SEF. A better way to get the catID within the breadcrumb (which was the intention of the the question) is probably to read the needed variables (`id` and `view`) from `JRequest`. In case of `view=='category'`, it would be possible to read the `catid` from DBO (`JFactory::getDBO`).
MaoPU
You are absolutely right. If you have sef enabled and you are trying to get info for current page you can use `JRequest`, make sure to sanitize `JRequest` (cast it to the needed type), see documentation for JRequest on Joomla's docs site - http://docs.joomla.org/Retrieving_data_from_GET_and_POST_requests
Alex