Get current item id in joomla hosting

In my custom component controller I have function save(). At the end it displays message and redirects to url

I'm having problems to do the following:

1.Use menu parameter from custom field instead of JText string. If I use this inside of function:

I get notice & error:

$message is name of my custom menu field defined in xml file. If I call it directly from view, then it's ok.

2.Get active menu itemid and use it inside setRedirect().

Get current item id in joomla hosting menu item or redirects to

I get empty itemid in URL if I use it like this:

I've found a lot of information about calling menu parameters in components but none of it helped me

After some testing, I was able to replicate the issue:

**A form redirects to a view without a menu item or redirects to itself relatively to a url like this one:

(It doesn't matter if the view has a menu item or not, this is just stupid Joomla routing.)**

Therefore the following has to be done to prevent the error:

1) the view has to be linked with an menu item in the Joomla backend

2) the url to the form in the frontend should be /menu-alias

3) the forms action shouldn't redirect to another view, better be empty

4) the form should contain hidden inputs with the task

Then your post and save redirection in the controller works like a charm, because you are operating within the menu item context (url).

But, if you redirect to another view or via JRoute to the same view without saving the current item id, you will leave the menu item context ("component/mycomponent/myview").

This happens a lot in Joomla, because the current router system doesn't check the given url against the menu items in the database. To prevent that, you can do the following actions:

1) Extend the router of your component with a function which searches the menu item table for the current url and returns the item id / menu item. (I have done that already in the past and it works.)

2) Add a hidden fields in your form which contains the item id, task and target view (the correct way in Joomla to route).

3) Redirect urls like "component/mycomponent/myview" to the correct url path.

4) You can set fallbacks in the component params for every view. This will chain the item id to the component params and not the views to an url path. View1 -> Dropdown -> Select Chained Menu Item

$itemId = $params->get( $mycurrentviewname );

Don't use the $menu->getActive() if not absolutely necessary, because of the stupid Joomla routing system. I am recommending to set the menu item manually somehow.

How I said, your error is somewhere in the beginning of routing. This could be simply your url which leads to your form or a redirect after submitting the form to a view without a menu item or to a url which doesn't contain an itemId.

hosting

Add this to your save function between $itemid and $message:

Then add "&task=save&Itemid=101" to the url which returns your error message. (Change 101 to your item id!)

You should be redirected correctly now.

Related articles

Item 101 joomla hostingIn our last Joomla 3.0 tutorial, we walked you through the steps for setting up a menu in Joomla 3.0. The menu will not show until we add items to the menu, and so we'll now walk you through...
This site is down for maintenance joomla hostingIntroduction Joomla administrators / webmasters may want to make changes to their Joomla website without their users being able to see them take place, or they may want to perform generic...
Hosting website template joomla gratuitTumblr is a micro blogging platform with a amazing themes designed for it either free or premium ones. The thing that amazes users to this cute little blogging platform is that its minimal in...
Custom modules in joomla hostingLast updated January 17, 2017 11:08 Ecwid is a great tool to add an e-commerce functionality to your Joomla site. Ecwid is very different from usual "Joomla" shopping carts and has these unique...
Get item quantity wordpress hostingLearn how to fix the WordPress menu items limit On a regular basis we get requests from users about a particular WordPress problem: it’s impossible to have a larger number of allowed menu items...