1

I'm working on custom Joomla MVC component for Joomla 2.x. I have a .xml file for custom menu item type. I need to get the value_field from it to tell the view what to show from database. I tried and researched everything and it seems nothing works for me.

Here's the codes:

default.xml

    <?xml version="1.0" encoding="utf-8"?>
    <metadata>
    <layout title="League select">
        <message>Selector for league view on site</message>
    </layout>
    <fields name="request">
    <fieldset name="request">
        <field name="selLeag" 
               type="sql"  
               label="Select league" 
               query="SELECT leagueName, id FROM footleague_leagues"
               key_field="id" 
               value_field="leagueName" 
               required="true"
        >

        <option value="">Select league</option>

        </field>

        <field name="selDiv" 
               type="sql"  
               label="Select division" 
               query="SELECT divisionName, id FROM footleague_divisions" 
               key_field="id" 
               value_field="divisionName" 
               required="true"
        >
            <option value="">Select division</option>

        </field>
    </fieldset>
</fields>

leagueinfo.php model

<?php

    defined('_JEXEC') or die('Restricted access');

    jimport('joomla.application.component.modelitem');

    class FutLigaModelLeagueInfo extends JModelItem{

    protected $msg;

    public function getMsg(){

        if(!isset($this->msg)){
            //this shows me only the ids from database but I need value_field
            $leagueid = JRequest::getVar('selLeag');
            $divisionid = JRequest::getVar('selDiv');

            //this shows me an empty array
            $data = JFactory::getApplication()->input->get('jform', array(), 'array');

        echo $leagueid;
        echo $divisionid;

        print_r($data);


    }

    return $this->msg;

    }

}

?>

Thank you for help! :)

  • I have updated the answer with few changes in code, kindly check – Liz. Jul 15 '16 at 6:27
  • @user8693 this page seems abandoned. Did the posted answer solve your problem? – mickmackusa Feb 3 at 1:41
1

You can add a parameter in the menu item and to obtain the value_field. You cannot directly achieve that but yes you can concat the 2 columns and pass the value in key_field in xml and then explode in model file and as follows:

  1. default.xml:

    <layout title="League select">
       <message>Selector for league view on site</message>
    </layout>
    
    <fields name="params">
        <fieldset name="advanced">
            <field name="selLeag" type="sql" label="Select league" 
         query="SELECT leagueName, id, concat( leagueName, ':-:',id) AS leagueNameid FROM footleague_leagues"key_field="leagueNameid" value_field="leagueName" required="true">
               <option value="">Select league</option>
            </field
       </fieldset>
    </fields>
    
  2. Html Output of the xml field will give you select options as below:Suppose here, NFL league is the leagueName and 1 is it's id

    <select id="jform_params_selLeag" name="jform[params][selLeag]" class="chzn-done">
        <option value="" selected="selected">Select League</option>
        <option value="NFL:-:1">NFL</option>
    </select>
    
  3. models/leagueinfo.php:

    $menuParam = JFactory::getApplication()->getParams();
    $selLeag = $menuParam->get('selLeag');// output: NFL:-:1
    $explode = explode(':-:',$selLeag);
    

    In this way, you have both id and leagueName and now you can use this value as per you want. Hope this helped!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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