Where is Itemid used to flag active menu item in Joomla 2.5.6? - joomla

Can anyone please help me with the process flow through which the Joomla Itemid parameter ends up being the highlighted menu item?
I have embedded a 3rd party application in Joomla and by temporarily changing the php environment within the application.
I am able to get joomla html and insert the 3rd party html by replacing a token.
Simplified Code:
if ($_SERVER['REQUEST_METHOD'] == 'GET' ) {
$_SERVER['REQUEST_METHOD'] = '';
}
$_SERVER['REQUEST_URI'] = '/joomla/index.php?view=mycom&option=com_mycom&Itemid=103';
$_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'] = '/joomla/index.php';
$_SERVER['QUERY_STRING'] = 'view=mycom&option=com_mycom&Itemid=103';
ob_start();
require_once '/joomla/index.php';
$joomlaHTML = ob_get_clean();
echo str_replace($replacementToken, $thirdPartyHTML, $joomlaHTML);
In v1.5.x, the menu item with ID 103 is properly highlighted but in v2.5.6, it isn't and the Home item is always highlighted. I think it used to be highlighted correctly in v1.6.x and earlier versions of 2.5.x as well but not sure.
I wanted to find how the process flows (where this is set) so I can see what tweaks I need to make.
DELETED WRONG INFO

Thanks Dayo! you saved my day with this:
// force highlight the external url menu item
$Itemid = JRequest::getVar('Itemid');
$menu = JSite::getMenu();
$menu->setActive($Itemid);

I don't fully understand the breadcrumb part, but I managed to get it working by editing my component's controller.php to read:
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// force highlight the external url menu item
$Itemid = JRequest::getVar('Itemid');
$menu = JSite::getMenu();
$menu->setActive($Itemid);
// force fix the breadcrumb
$app = JFactory::getApplication('site');
$pathway =& $app->getPathway();
$bcrumbs = &JPathway::getInstance('site');
// import Joomla controller library
jimport('joomla.application.component.controller');
/**
* MyCom Component Controller
*/
class MyComController extends JController
{
}

Look in the following File
Check the
/modules/mod_menu/mod_menu.php
File and you will see two functions has been called "getActive" and "getDefault"
Which can be find in following file
/libraries/joomla/application/menu.php
I think it can be customized easily now

Related

Joomla MVC Module delete model

I'm a newbie on Joomla developing and I'm trying to fix an old administration module made by 'someone before me'. Module's been developed using MVC Components, it has several CRUDs and I'm stucked at deleting an item. The template view adds the toolbar icon like this:
JToolbarHelper::deleteList('', 'paises.delete', JTOOLBAR_DELETE);
It also has at the list controller (DistribuidoresControllerPaises), the getModel function:
public function getModel($name = 'Pais', $prefix = 'DistribuidoresModel', $config = array('ignore_request' => true))
{
$model = parent::getModel($name, $prefix, $config);
return $model;
}
The model class:
class DistribuidoresModelPais extends JModelAdmin
When selecting an item on the list, and clicking the trash button an empty page opens with this ending:
administrator/index.php?option=com_distribuidores&view=pais
If I come back to grid, the item still remains.
Any suggestion?
Thanks in advance
You can debug this by enabling debugging from Joomla configuration or you can try to to check with exit with in "delete" function of "paises" controller and can check you get item ids in post request or not.
Also you are using view "pais" also using model "pais" then why you are using "paises" controller for delete function, you should use "pais" controller to delete.
Also provide delete function which you are using to delete items, it may contain some issue.

Joomla! - Load editor-xtd plugin layout from specified file in button's iframe handler

I'm working on a Joomla! 2.5/3.x editor-xtd button and I have a problem loading a layout from file on button click.
I have tried this method:
$link = 'plugins/editors-xtd/myplugin/myplugin.layout.php?name='.$name;
$button = new JObject;
$button->modal = true;
$button->class = 'btn';
$button->link = $link;
$button->text = 'Insert something';
$button->name = 'myplugin';
$button->options = "{handler: 'iframe', size: {x: 500, y: 300}}";
... but the full generated link in admin looks like http://my.local.host/mywebsite/administrator/plugins/editor-xtd/link-etc.. and it doesn't work. I also have tried including JURI::base in my $link, but the administrator path is still loaded.
I'm new in plugin dev with Joomla! and I have search a lot but no solution found.
** I also tried a link like this index.php?folder=plugins.editors-xtd.myplugin&file=myplugin.layout.php&name=$name but still nothing.
Is there a workout for this or I'll have to create&use a javascript function to run on button click?
Solution
Modify link variable like this (if application is admin):
$link = '../plugins/editors-xtd/myplugin/myplugin.layout.php?name='.$name;
... and delete button options (this means that file contents will be loaded via ajax inside modal)
Further more, in myplugin.layout.php we can add a little security check and we can import Joomla! framework library and defines so that we can make use of Joomla! framework in our file (things like language load for eg.)
This is my actual header of file:
<?php
// No direct access
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
if( ! IS_AJAX) die;
// Include J!Framework for later use
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../../..'));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE.DS.'includes'.DS.'defines.php');
require_once ( JPATH_BASE.DS.'includes'.DS.'framework.php');
//more magic goes here...
Unfortunately there is a bit of a gotcha here in that the JED checker process requires that ALL php files start with defined('_JEXEC') or die; on the very first line of code so if you want to share it on extensions.joomla.org then you are stymied...
Back on the OP you can detect whether you are in the Admin or Site before generating the link:
$app = JFactory::getApplication();
// ...
if ($app->isAdmin()) {
$root = '../'; // Joomla expects a relative path, leave site folder "administrator"
} else {
$root = '';
}
$button->link = $root.'/plugins/editors-xtd/myplugin/myplugin.layout.php?name='.$name;
Also, as you may already know the $button->name = 'myplugin'; needs to be the name of the icon from the Joomla icomoon set - you can see them here https://ma.tvtmarine.com/en/blog/112-joomla-icomoon-icons-directory
The name needs to be the icon name without the .icon- bit eg:
$button->name = 'warning-2';
code block doesn't seem to be working properly...sorry about the formatting

JToolbar::save() redirection

I'm going through the Joomla 2.5 tutorial to build a custom component. Now I'm facing an issue on the redirection after using JToolbar::save() or JToolBarHelper::cancel for that matter. By default Joomla wants to redirect to the default layout (from the edit layout). However I don't want it to do that. I want it to redirect back to another view. In Joomla 1.5 I would have done this through adding the function into the controller - something like
function cancel()
{
//redirects user back to blog homepage with Cancellation Message
$msg = JText::_( 'COM_BLOG_POST_CANCELLED' );
$this->setRedirect( 'index.php?option=com_jjblog&view=jjblog', $msg );
}
Now that works beautifully for the cancel function, however for save this is a much more complex thing. If I want to overwrite the url do I have to redirect the controller to the model and then write in all the code for the model interaction? Because that seems slightly excessive just for a url redirection like you would in Joomla 1.5?
Hope you have added the save toolbar code with the proper controller name like this
JToolBarHelper::save('controllerName.save');
Create a save function in appropriate controller.
Add the task in the form
Finnally make sure you have added form action withthe corresponding component name.
You can try this-
In the controller firstly you call the parent save function than redirect to url.
function save(){
parent::save();
$this->setredirect('index.php?option=com_mycomponent');
}
OK it didn't need to $this->setRedirect at all. Just needed me to change the value to
protected $view_list = 'jjBlog';
which then sets the redirects of everything back to that list view.
Source link for this is here.
Thanks for all the responses though!!
view.html.php
protected function addToolbar ()
{
JRequest::setVar ('hidemainmenu', false);
JToolBarHelper::title (JText::_ ('Configuration'), 'configuration.gif');
JToolBarHelper::save($task = 'save', $alt = 'JTOOLBAR_SAVE');
}
controller.php
public function save()
{
$mainframe = JFactory::getApplication();
$mainframe->enqueueMessage (JText::_ ('COM_SOCIALLOGIN_SETTING_SAVED'));
$this->setRedirect (JRoute::_ ('index.php', false));
}
I think you can use
global $mainframe;
$mainframe->redirect("index.php?option=com_user&task=activate&activation=".$activation);
If you are overriding joomla's default save function in your custom component like
function save( $task = 'CustomSave', $alt = 'Save' ) // or even same name Save
Inside your controller you can use the CustomSave as the task and use $mainframe for redirect.
or
$mainframe = &JFactory::getApplication();
$mainframe->redirect("index.php?option=com_user&task=activate&activation=".$activation);
Hope this may help you..

Joomla pagination across different components

Because pagination is using getUserStateFromRequest method to get the limit and limitstart variable, I'm having a problem where as I navigate from one component to another, I'm shown a no items found message.
To clarify, I have a products component that has 3 pages worth of products listed. Then I have a branches component with 2 pages worth of branch information. So if I navigate to the third page in the products list, and then go to the branches component, nothing is displayed.
Has anyone any idea how to stop this from happening? Any way to maybe clear the session data?
What I ended up doing was this,
in line 624 in libraries/joomla/application/application.php file I added the following lines
$this->setUserState('option','default');
$curr_comp = JRequest::getCmd( 'option' );;
if($this->getUserState('option') != $curr_comp)
{
$this->setUserState($option . 'limitstart',0);
$this->setUserState('option',$curr_comp);
}
so the whole function reads this,
public function getUserStateFromRequest($key, $request, $default = null, $type = 'none')
{
$this->setUserState('option','default');
$curr_comp = JRequest::getCmd( 'option' );
if($this->getUserState('option') != $curr_comp)
{
$this->setUserState($option . 'limitstart',0);
$this->setUserState('option',$curr_comp);
}
$cur_state = $this->getUserState($key, $default);
$new_state = JRequest::getVar($request, null, 'default', $type);
// Save the new value only if it was set in this request.
if ($new_state !== null)
{
$this->setUserState($key, $new_state);
}
else
{
$new_state = $cur_state;
}
return $new_state;
}
This seems to be working fine at the moment. But please test before implementing on a live site
To prevent editing the core files, but with the effect limited to your extension (so other extensions could load at the wrong page, but not yours), and if your model extends modellist, override the getStart() method:
public function getStart()
{
$store = $this->getStoreId('getstart');
$input = JFactory::getApplication()->input;
$start = $limitstart = $input->getInt('limitstart', 0);
$this->setState('list.start', $limitstart); // maybe redundant
$limit = $this->getState('list.limit');
$total = $this->getTotal();
if ($start > $total - $limit)
{
$start = max(0, (int) (ceil($total / $limit) - 1) * $limit);
}
// Add the total to the internal cache.
$this->cache[$store] = $start;
return $this->cache[$store];
}
If you want a solution that works system-wide and for all extensions, you should be able to override modellist with your implementation in a plugin. Start here.
This is an old question, but I just had the same issue as the OP, but in my case with Joomla 3.4.3.
After a lot of digging and testing, I discovered a solution for this that doesn't involve any plugin or core change:
If you put limitstart=0 in the URL, the pagination will restart for that page, and this solves the problem between menus.
The way to implement this could be either with javascript, or by overriding the menu module, I chose the override:
I just need this in some menus, so I placed a CSS class into the
menu link (edit the menu, and in the "Link Type" tab, place the CSS
class in the "Link CSS Style" field), in my case it was "
video-area" (without the quotes).
Add override (add the module to the html folder of your template,
in my case it was the menu module, so it was a matter of adding the
mod_menu folder: templatefolder/html/mod_menu)
In the override of the component part of the module
(default_component.php), check to see if we have the CSS class, if
so, add the extra query to the URL (I edited case 0):
case 0: $paginationLinks = ""; if(isset($class) && strpos($class, '
video-area') !== false){ $paginationLinks =
"?limitstart=0&limit=12"; } ?><a <?php echo $class; ?>href="<?php
echo $item->flink; ?><?php echo $paginationLinks;?>" <?php echo
$title; ?>><span><?php echo $linktype; ?></span></a><?php break;
That's it! it solved my problem, and even the pagination links have the extra query :)
BONUS: notice that I have &limit=12, this changes the limit for the pagination into 12 per page, without any extra code!, (before, I had a lot of code to implement this, and by adding this to the menu it calculates the correct page number and totals, and filters the query, nice one Joomla!)

How do you use JRoute in Joomla to route to a Search menu item?

I am trying to create a a box in a template in Joomla! that will display all of the keywords and link them to their appropriate search page. I have a menu item set, however, I don't want to hard-code the menu item into the template, so I want to use the JRoute object to generate the SEF url.
I am using this function:
JRoute::_('index.php?option=com_search&searchword='.$keyword);
or this:
JRoute::_('index.php?option=com_search&view=search&searchword='.$keyword);
however, this generates a url like this:
/component/search/?searchword=africa
when it ought to create a search url like this:
/searchmenuitem?searchword=africa
I have searched extensivly online and havn't found a solution to this problem. Any ideas would be greatly appreciated.
Ok, so some additional information for you.. I am only experiencing the problem when I try and route the URL from a template in com_content. If I try and route the url from a template in com_search everything works perfectly. So, what is it about com_content that is causing this to not work properly?
thanks!
david
In joomla administration page go to the menu item you've chosen for the search results page and get the id of that menu item (itemId).
Than you can try using:
JRoute::_('index.php?option=com_search&view=search&Itemid=256&searchword=asdsadasdsa');
or even
JRoute::_('index.php?Itemid=256&searchword=asdsadasdsa');
both should result in: /searchmenuitem.html?searchword=asdsadasdsa
EDIT:
To make it more comforable you could add itemId as a param to your template.
There is another way, where u can get the itemId from the database (this method is required on multilingual websites). Let me know if you want it.
EDIT2:
Here it is:
$db =& JFactory::getDBO();
$lang =& JFactory::getLanguage()->getTag();
$uri = 'index.php?option=com_search&view=search';
$db->setQuery('SELECT id FROM #__menu WHERE link LIKE '. $db->Quote( $uri .'%' ) .' AND language='. $db->Quote($lang) .' LIMIT 1' );
$itemId = ($db->getErrorNum())? 0 : intval($db->loadResult());
I use this kind of method to get a menu item id of specific component and view
function getSearchItemId() {
$menu = &JSite::getMenu();
$component = &JComponentHelper::getComponent('com_search');
//get only com_search menu items
$items = $menu->getItems('componentid', $component->id);
foreach ($items as $item) {
if (isset($item->query['view']) && $item->query['view'] === 'search') {
return $item->id;
}
}
return false;
}
Then I use this method to get the sef url
function getRouteUrl($route)
{
jimport('joomla.application.router');
// Get the global site router.
$config = &JFactory::getConfig();
$router = JRouter::getInstance('site');
$router->setMode($config->getValue('sef', 1));
$uri = &$router->build($url);
$path = $uri->toString(array('path', 'query', 'fragment'));
return $path;
}
This just works in any template.
use like this
$itemid = getSearchItemId();
//returns valid sef url
$url = getRouteUrl('index.php?Itemid='.$itemid);
You really do not need to do sql on the menu table to get ids. Just search the menu object.
Try to create new menu in the joomla backend called for instance 'hidden-menu'. It will never be shown in the front. But it will be used by JRoute Then add to this menu new menuitem called 'searchmenuitem' with link to com_search. That is all. Now you can call
JRoute::_('index.php?option=com_search&view=search&searchword=asdsadasdsa');
and it will be ceonverted into this
/searchmenuitem.html?searchword=asdsadasdsa

Resources