joomla

How do I point a menu item to a component's task?


How do I point a Joomla menu item to a specific task in my custom component? I have had no trouble creating menu items that point to my views, and each view is available in the list when I select my component in the menu. But I have not found a way to route the menu to one of my component's tasks.

In some cases, I am using a controller method (i.e. a task) to decide which models and views to present to the user. Usually this is when I need to display data from multiple models, or I need to use information in the session state to determine which view is appropriate for the user. It is in these cases that I need a menu item that points to the task.

So, how do I get a menu item to bring a user to "index.php?option=com_mycomponent&task=do.something"?

What have I tried already, you ask? I've looked at just about every Google reference I could find, and none seem to address this issue. I'm either looking for the wrong thing, or it's so dead easy that nobody has ever had to ask for help. I have also looked through the Joomla components & menu items, finding no examples of a menu item pointing to a controller task.


Solution

  • I found the above answer not flexible enough and finally found this to work perfectly, you can override the menu item form xml ( administrator/components/com_menus/models/forms/item.xml ) and make the link field editable so you can add any request parameters you want, including tasks.

    Make sure you leave/add the view name of the request as otherwise the link type cannot be determined, so add it even if it is not relevant eg. &view=yourview.

    See system plugin onContentPrepareForm below

        function onContentPrepareForm(&$form, $data) {
    
            $option = JFactory::getApplication()->input->get('option');
    
            // allow us to edit the link field of the menu item for this component to include some custom request values
            if($option == 'com_menus' && $data->type=='component' && $data->request['option']=='com_your_component_name'){
    
                $form->setFieldAttribute('link','readonly','false');
    
            }
    
        }
    
    

    enter image description here