× Please submit new Bug Reports on GitHub: github.com/Jensen-Technologies/component-creator-issues/issues

Add menu parameter

il y a 10 ans 5 mois #111 par Rob van Oudheusden
Add menu parameter a été créé par Rob van Oudheusden
Hello to you all,

I am new here and still learning Joomla development.

I have build a component with Component Creator, I added some custom functions and queries,  and it works fine so far.

Now my question
The component has default 2 menu options: one for the list view (all items) and one for the single view (single item by ID).
In the component frontend I have only one main view (single and list) active, There are several foreign keys for this main part to other tables. These work fine. The information on both single and list view are correctly displayed, the way I want.

But now I want to add menu item parameters as options to select one or more of the foreign key items as a list, like Joomla does with category views to select a category to display all the items that belong to that category. I have searched on the internet and learned a bit about it, but I do not get it working at all. I know you have to add a field in the default.xml file of the frontend list view and then I get lost. I see a menu option in the backend, without the options, just an empty select field. I know I have to do a lot more, but I do not know what or where to start.

Can someone guide me with a step by step instruction, or give me a link to a good tutorial or send me some examples. Please do not advise me to view the Hello World custom component tutorial of Joomla. This does not help me at all. I hope someone will give me some good directions.

Best wishes,
Rob van Oudheusden,
Joomla CSS.

Connexion ou Créer un compte pour participer à la conversation.

il y a 10 ans 4 mois #177 par Sigitas Petrauskas
Réponse de Sigitas Petrauskas sur le sujet Add menu parameter
Did you tryed to use JCATEGORY fields withing Jomla ComponentCreator (Joomla CC)? try that feature first.

Connexion ou Créer un compte pour participer à la conversation.

il y a 10 ans 4 mois - il y a 10 ans 4 mois #186 par Rob van Oudheusden
Réponse de Rob van Oudheusden sur le sujet Add menu parameter
Herllo Petrauskas,

Thank you for your response.

There are two reasons why I do not want to use the JCATEGORY option.
1. I am learning to develop Joomla components, and I want to know how to do this without using this option.
2. I have only asked help for the use of on one menu filter item, but in fact I want multi items to filter within the menu,

In the meantime I have found out how this is working.
There is realy no documentation about this subject, so I found it out myself.
Here I will give the solution I found for other developers, who also want to know this.
I am also open for other methods and suggestions.

I updated 3 files from the default output of component creator.
default.xml of the frontend list view.
a new file in models/fields in the backend
added query code in the model for the frontend listview.

I am developing a realestate component and the most important frontend view is the properties/objects list view, which I want the user to narrow the list of default all objects to objects of a specific market.
Now I have 2 markets in the database in seperate table for markets wich has a relation with the properties table with a foreign key field..But the users can add as much markets as they want.

property for sale
property to rent

Frontend properties List view
The tmpl/default.xml file is als follows

<?xml version="1.0" encoding="utf-8"?>
<metadata>
    <layout title="COM_REALESTATE_TITLE_LIST_VIEW_PROPERTIES" option="View">
        <message>
            <![CDATA[COM_REALESTATE_TITLE_LIST_VIEW_PROPERTIES_DESC]]>
        </message>
    </layout>
    <fields name="request">
        <fieldset
            name="request"
            addfieldpath="/administrator/components/com_realestate/models/fields"
        >
            <field
                name="id"
                type="marketlist"
                label="Select a Market"
                description="Select a Market"
            >
                <option value="0">All Markets</option>
            </field>
        </fieldset>
    </fields>
</metadata>

The fieldtype will be the name of a new file in the
backend models/fields/marketlist.php

<?php
// No direct access to this file
defined('_JEXEC') or die;
 
// import the list field type
jimport('joomla.form.helper');
JFormHelper::loadFieldClass('list');
 
/**
 * MarketList Form Field class for the Realestate component
 */
class JFormFieldMarketList extends JFormFieldList {
    /**
    * The field type.
    *
    * @var    string
    */
    protected $type = 'MarketList';
 
    /**
    * Method to get a list of options for a list input.
    *
    * @return      array           An array of JHtml options.
    */
    protected function getOptions() {
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);
        $query->select('p.id, p.market, m.market_name');
        $query->from('#__realestate_properties AS p');
        $query->join('LEFT', '#__realestate_markets AS m ON m.id = p.market');
        $db->setQuery((string)$query);
        $markets = $db->loadObjectList();

        $options = array();
        if ($markets) {
            foreach($markets as $market) {
                $options[] = JHtml::_('select.option', $market->market, $market->market_name);
            }
        }

        $options = array_merge(parent::getOptions(), $options);
        return $options;
    }
}

So far the menu selection is extended with the option to slelect a market, which is a value in the database.
In this example the user can choose to select all markets, and the markets which are in the database.

To filter the market I added some code on the query in the
List model properties of the frontend

protected function getListQuery() {
......
// Menu selection List
$app = JFactory::getApplication();
$menus = $app->getMenu();
$title = null;
$menu = $menus->getActive();
$selectmarket = $menu->query;
.......
$query    ->select('m.market_name, m.state as m_state, m.language as m_language')
    ->join('LEFT', '#__realestate_markets as m ON m.id = a.market')
    ->where('m.language in (' . $db->quote(JFactory::getLanguage()->getTag()) . ',' . $db->quote('*') . ')');
    if ($selectmarket != 0) {
        $query->where('m.id = ' .$selectmarket);
    }        
.....

Now you can select a menu option to All items or a specific market.
The listview in the frontend will only show the selected market.

That's it.
Dernière édition: il y a 10 ans 4 mois par Rob van Oudheusden.

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.051 secondes
Propulsé par Kunena

We use cookies so that you can place orders and we can provide a better service. You can control the use of cookies at the individual browser level. If you reject cookies, you may still use our website, but your ability to use some features or areas of our website may be limited.