8

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

$this->setMessage(JText::_('SOME_TEXT'));
$this->setRedirect(JRoute::_('index.php?option=com_foo&view=foo', false));

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:

$app = JFactory::getApplication();
$menu = $app->getMenu();
$active = $menu->getActive();
$itemId = $active->id;
$message = $active->params->get('message');

I get notice & error:

Notice: Trying to get property of non-object...
Fatal error: Call to a member function get() on a non-object...

$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().

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

$app = JFactory::getApplication();
$menu = $app->getMenu();
$active = $menu->getActive();
$itemId = $active->id;
$this->setRedirect(JRoute::_('index.php?option=com_foo&view=foo&Itemid='.$itemId, false));

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

6
  • Are you able var_dumb the menu item object? var_dump($active); ? I have tried you code in my custom component, and it worked in the helper file and in the view. Commented Dec 11, 2015 at 22:39
  • As I've mentioned, it is working in my view also, but not in controller.
    – weezle
    Commented Dec 14, 2015 at 9:03
  • Please provide the code of your save function (+ the way the controller is implemented and gets called) and the xml of your custom field. I have tested loading menu params in the main component controller and it works for me. Commented Dec 14, 2015 at 10:23
  • This is the function inside components/com_jpainter/controller.php: gist.github.com/esedic/6ed98466be93008ab777 Controller is called like this from components/com_jpainter/jpainter.php gist.github.com/esedic/5fc78d8fd1ceed5e2f05 And this is xml inside components/com_jpainter/views/jpainter/tmpl/default.xml (fieldset advanced) gist.github.com/esedic/4cd7e9bf4f2a4253562d
    – weezle
    Commented Dec 16, 2015 at 18:44
  • Thanx, I will try to replicate the issue. Commented Dec 17, 2015 at 0:06

3 Answers 3

3

Use the following:

$menu           =& JSite::getMenu();
$activeMenu     = $menu->getActive();
$Itemid         = $activeMenu->id;

or

$menu =& JSite::getMenu();
$activeMenu     = $menu->getActive();
$Itemid         = $activeMenu->query['Itemid'];

To get your message being passed you need to retrieve it from the application input

$JInput = JFactory::getApplication()->input;
$message = $JInput->get('message','','string');
2

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:

component/mycomponent/myview

(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

In controller:

$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.

Back to question:

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.

Proof:

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

if(empty($itemId)){

      $itemId = $app->input->get('Itemid', $itemId,'int');
      $active = $menu->getItem($itemId);
 }

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.

3
  • I know I'm a little late, but thanks for your effort. I didn't test your solution, I was on a tight schedule so I've managed to solve it differently.
    – weezle
    Commented Jan 20, 2016 at 0:37
  • how did you solve the issue? Commented Jan 20, 2016 at 21:54
  • I was in a hurry so I just hardcoded itemid. I try to implement your solution in the future.
    – weezle
    Commented Jan 26, 2016 at 9:14
2

To get Menu Params

This one is perfect I was searching few hrs and this solve my issue of Joomla 3.6.5 & PHP 7.1

0: Using $this when not in object context

Solution:

$app = JFactory::getApplication();
$menu = $app->getMenu();
$active = $menu->getActive();
$itemId = $active->id;
$menuparams         = $menu->getParams($itemId);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.