Add a Menu Type to Joomla Site Part
After the component is installed, you can create a menu Item to access it. This allows to access the component through a menu rather than having to remember what to type into the address bar. Each view and layout can have its own menu item.
This is a very simple process. You need to create a menu configuration XML file alongside the template file with the matching name.
For example, if the template file is default.php, then the menu configuration details are stored in the default.xml file.
Step 1: Layout XML File
site/tmpl/planet/default.xml
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<layout title="Single Planet">
<message>Display a single planet.</message>
</layout>
</metadata>
Now, you can create a new menu item from the backend. After creating the menu item, you can check the link that relates to the menu item. Here, you can see the component (like option=com_stars), the view (like view=planet), the layout (like layout=blog) and the id (like id=4) of the item.
Menu Parameters
You can add options or configuration settings for the menu. You can use these options to determine how or what to display at the frontend.
For example,
<fields name="params">
<fieldset name="basic" label="Options">
<field
name="show_title"
type="list"
label="JGLOBAL_SHOW_TITLE_LABEL"
class="form-select-color-state">
<option value="1">JSHOW</option>
<option value="0">JHIDE</option>
</field>
</fieldset>
</fields>
The values of these fields are stored as JSON in the params column of the menu database table. To access these parameters, add the following code in the view file:
$app = Factory::getApplication();
$active = $app->getMenu()->getActive();
$this->params = $active->params;
Now, in the view file or in the layout file, you can get any parameter:
$show_title = $this->params->get('show_title');
Accordingly, you can display the title of the record.
Request Parameters
You can add request parameters to the menu. The request parameters are added to the URL as query string.
For example,
<fields name="request">
<fieldset name="request">
<field
name="id"
type="number"
label="Planet"
required="true" />
</fieldset>
</fields>
The request fields are mandatory fields. The name attribute can be passed to the component in the URL. For example,
index.php?option=com_planets&view=planet&id=5
Now, you can get this id in the Model class or View class or layout file.
$input = Factory::getApplication()->input;
$id = $input->get('id', 0, 'int');
Generally, you get the request parameters in the Model class. Then, call the method from the View class.