- Posts: 214
COMMUNITY FORUM
[SOLVED] get k2 item route
- George Nasis
-
Topic Author
- Offline
- Elite Member
Less
More
9 years 11 months ago - 9 years 11 months ago #144073
by George Nasis
get k2 item route was created by George Nasis
hallo there!
i am trying to create a function outsite of k2 to retrieve the sef url of the k2 item. i write this one but it is not working :/
any help is appreciated!
thank you
i am trying to create a function outsite of k2 to retrieve the sef url of the k2 item. i write this one but it is not working :/
function getK2ItemRoute ($cb_api_version, $id, $catid, $k2item)
{
require_once(JPATH_SITE.DS.'components'.DS.'com_k2'.DS.'helpers'.DS.'route.php');
$needles = array(
'item' => (int) $id,
'category' => (int) $catid
);
$db = JFactory::getDBO();
$select = 'SELECT a.id AS id, a.catid AS catid, a.alias AS alias, c.alias AS catalias FROM #__k2_items AS a LEFT JOIN #__k2_categories AS c ON ( a.catid = c.id ) WHERE a.published = 1 AND a.catid = '.$catid;
$db->setQuery($select);
$k2item = $db->loadObjectList();
//Create the link
$link = JRoute::_(K2HelperRoute::getItemRoute($k2item->id.':'.urlencode($k2item->alias),$k2item->catid.':'.urlencode($k2item->catalias)));
return $link;
}
function _findItem ($cb_api_version, $needles)
{
$component =& JComponentHelper::getComponent('com_k2');
$menus = &JApplication::getMenu('site', array());
if ($cb_api_version < 2) {
$items = $menus->getItems('componentid', $component->id);
} else {
$items = $menus->getItems('component_id', $component->id);
}
$match = null;
foreach ($needles as $needle => $id)
{
foreach($items as $item)
{
if ((@$item->query['view'] == $needle) && (@$item->query['id'] == $id))
{
$match = $item;
break;
}
}
if (isset($match)) {
break;
}
}
return $match;
}
any help is appreciated!
thank you
Last edit: 9 years 11 months ago by George Nasis. Reason: wrong edit
Please Log in or Create an account to join the conversation.
- Lefteris
-
- Offline
- Platinum Member
Less
More
- Posts: 8743
9 years 11 months ago - 9 years 11 months ago #144096
by Lefteris
Replied by Lefteris on topic get k2 item route
Hi,
There is no need to write a function. The function is already there. All you need is the item's id, catid, alias as well as the category alias:
There is no need to write a function. The function is already there. All you need is the item's id, catid, alias as well as the category alias:
require_once JPATH_SITE.'/components/com_k2/helpers/route.php';
$link = K2HelperRoute::getItemRoute($item->id.':'.urlencode($item->alias), $item->catid.':'.urlencode($item->category->alias));
Last edit: 9 years 11 months ago by Lefteris.
Please Log in or Create an account to join the conversation.
- George Nasis
-
Topic Author
- Offline
- Elite Member
Less
More
- Posts: 214
9 years 11 months ago - 9 years 11 months ago #144097
by George Nasis
Replied by George Nasis on topic get k2 item route
hallo lefteris,
thanks for the response
if i write this two lines i have blank page :/
to be more specific, this is a plugin of community builder which redirects to the k2 items
then i write this one
and i have the redirect url: www.example.com/item
thank you
thanks for the response
if i write this two lines i have blank page :/
to be more specific, this is a plugin of community builder which redirects to the k2 items
then i write this one
function getK2ItemRoute ()
{
require_once JPATH_SITE.'/components/com_k2/helpers/route.php';
$link = K2HelperRoute::getItemRoute($item->id.':'.urlencode($item->alias), $item->catid.':'.urlencode($item->category->alias));
return $link;
}
and i have the redirect url: www.example.com/item
thank you
Last edit: 9 years 11 months ago by George Nasis. Reason: wrong
Please Log in or Create an account to join the conversation.
- Lefteris
-
- Offline
- Platinum Member
Less
More
- Posts: 8743
9 years 11 months ago #144130
by Lefteris
Replied by Lefteris on topic get k2 item route
A blank page indicates a PHP error. Turn on error reporting under Joomla! global configuration to see what's the actual error.
Note that the code i provided is sample code. You are supposed to replace the variables regarding the item that i mentioned with your own variables.
Note that the code i provided is sample code. You are supposed to replace the variables regarding the item that i mentioned with your own variables.
Please Log in or Create an account to join the conversation.
- George Nasis
-
Topic Author
- Offline
- Elite Member
Less
More
- Posts: 214
9 years 11 months ago #144134
by George Nasis
Replied by George Nasis on topic get k2 item route
hi lefteris,
to be more specific:
it is a plugin for community builder with links to k2 items. in this php file i have write two functions and with them i have made to make the links to the k2 items but wihout sef.
so what exactly i have to changed in this?or what should i replace?
thank you
to be more specific:
it is a plugin for community builder with links to k2 items. in this php file i have write two functions and with them i have made to make the links to the k2 items but wihout sef.
function getK2ItemRoute ($cb_api_version, $id, $catid)
{
$needles = array(
'item' => (int) $id,
'category' => (int) $catid
);
$db = JFactory::getDBO();
$select = "SELECT alias FROM #__k2_items WHERE id = ".$id;
$db->setQuery($select);
$k2alias = $db->loadResult();
//Create the link
$link = 'index.php?option=com_k2&view=item&id='. $id.':'.$k2alias;
if ($item = $this->_findItem($cb_api_version, $needles)) {
$link .= '&Itemid='.$item->id;
}
return $link;
}
function _findItem ($cb_api_version, $needles)
{
$component =& JComponentHelper::getComponent('com_k2');
$menus = &JApplication::getMenu('site', array());
if ($cb_api_version < 2) {
$items = $menus->getItems('componentid', $component->id);
} else {
$items = $menus->getItems('component_id', $component->id);
}
$match = null;
foreach ($needles as $needle => $id)
{
foreach($items as $item)
{
if ((@$item->query['view'] == $needle) && (@$item->query['id'] == $id))
{
$match = $item;
break;
}
}
if (isset($match)) {
break;
}
}
return $match;
}
so what exactly i have to changed in this?or what should i replace?
thank you
Please Log in or Create an account to join the conversation.
- Lefteris
-
- Offline
- Platinum Member
Less
More
- Posts: 8743
9 years 11 months ago #144153
by Lefteris
Replied by Lefteris on topic get k2 item route
In Joomla! you can get the SEF url using the JRoute function. You can take a look at docs.joomla.org/Supporting_SEF_URLs_in_your_component .
However, as i already wrote there is no need to write your own function. The code i provided should work if you have K2 installed on your system.
However, as i already wrote there is no need to write your own function. The code i provided should work if you have K2 installed on your system.
Please Log in or Create an account to join the conversation.
- George Nasis
-
Topic Author
- Offline
- Elite Member
Less
More
- Posts: 214
9 years 11 months ago #144159
by George Nasis
Replied by George Nasis on topic get k2 item route
hallo lefteris,
i have modify the function and change to this:
and now the link has become with sef and the item but not the path
www.example.com/item/itemalias
so now the categories of the path are missing
should i add something in the select?because if i remove the select then it is not working
thank you
i have modify the function and change to this:
$db = JFactory::getDBO();
$select = "SELECT alias FROM #__k2_items WHERE id = ".$id;
$db->setQuery($select);
$k2item = $db->loadResult();
$link = K2HelperRoute::getItemRoute('index.php?option=com_k2&view=item&id='. $id.':'.$k2item);
and now the link has become with sef and the item but not the path
www.example.com/item/itemalias
so now the categories of the path are missing
should i add something in the select?because if i remove the select then it is not working
thank you
Please Log in or Create an account to join the conversation.
- Lefteris
-
- Offline
- Platinum Member
Less
More
- Posts: 8743
9 years 11 months ago #144175
by Lefteris
Replied by Lefteris on topic get k2 item route
Just use the code i provided in my first answer. As i wrote there you also need the category id and the category alias.
The code is quite simple. If you are not familiar with it i suggest to hire a developer to do it for you.
The code is quite simple. If you are not familiar with it i suggest to hire a developer to do it for you.
Please Log in or Create an account to join the conversation.
- George Nasis
-
Topic Author
- Offline
- Elite Member
Less
More
- Posts: 214
9 years 11 months ago #144184
by George Nasis
Replied by George Nasis on topic get k2 item route
i think i have only to modify the query.we talk about a simple query it is not necessary to hire a developer just for that
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
Less
More
- Posts: 15920
9 years 11 months ago #144285
by Krikor Boghossian
Replied by Krikor Boghossian on topic get k2 item route
You need to use Lefteri's code.
Since you already know the item's id, you need to get the category's id (catid) and alias ( alias ) and use K2's function.
Since you already know the item's id, you need to get the category's id (catid) and alias ( alias ) and use K2's function.
Please Log in or Create an account to join the conversation.
- George Nasis
-
Topic Author
- Offline
- Elite Member
Less
More
- Posts: 214
9 years 11 months ago #144303
by George Nasis
Replied by George Nasis on topic get k2 item route
thank you too krikor,
you were both correct with the code. i simply provided the two lines code for the url to be generated.
BUT somewhere in the code where missing the alias of the item in the select query
you can close this
thank you guys
you were both correct with the code. i simply provided the two lines code for the url to be generated.
BUT somewhere in the code where missing the alias of the item in the select query
you can close this
thank you guys
Please Log in or Create an account to join the conversation.