0

I created a custom component and in the form one custom field categoryList to list all categories dynamically. Following is the code:

  1. formname.xml

    <fieldset addfieldpath="/administrator/components/com_componentname/models/fields">
     <field name="id" type="text" readonly="true" class="readonly" default="0" description="JGLOBAL_FIELD_ID_DESC"/>
    
     <field name="cat_id_01" type="categoryList" onchange="loadSubcategory(1)" label="Category 1" description="Category">
        <option value="">Please select</option>
     </field>
    </fieldset>
    
  2. edit.php

    <?php echo $this->form->renderField('cat_id_01'); ?>
    
  3. How it appears on localhost: it displays as select-box with category options. This is how I want it to be displayed. php version: 7.0.0

  4. models/fields/categoryList.php

        JFormHelper::loadFieldClass('list');
    
                /**
                 * Supports an HTML select list of categories
                 *
                 * @since  1.6
                 */
                class JFormFieldCategoryList extends JFormFieldList
                {
                    /**
                     * The form field type.
                     *
                     * @var        string
                     * @since    1.6
                     */
                    protected $type = 'categoryList';
    
                    /**
                     * Method to get the field input markup.
                     *
                     * @return    string    The field input markup.
                     *
                     * @since    1.6
                     */
                    public function getOptions()
                    {
                        $categories[NULL] = 'Please Select';
                        $jinput = JFactory::getApplication()->input;
                        $db = JFactory::getDbo();
                        $query = 'SELECT id, title FROM `#__categories` WHERE parent_id IN (SELECT id from `#__categories` where title="'.$jinput->get('section').'")';
                        $rows = $db->setQuery($query)->loadObjectlist();
                        foreach ($rows as $row) {
                            $categories[$row->id] = $row->title;
                        }
    
                        return $categories;
                    }
    
                }
    
  5. How it appears on server: it displays as textbox and somehow the custom field is not working on server. php version: 7.0.17

    <div class="controls">
    <input type="text" name="jform[cat_id_01]" id="jform_cat_id_01" value="110" list="jform_cat_id_01_datalist" onchange="loadSubcategory(1)"><datalist id="jform_cat_id_01_datalist"></datalist>
    </div>
    
  6. Joomla version: 3.6.5

I want to display the field as selectbox on server as well.

  • 2
    On some reason it doesn't see the either the XML file at the server or categoryList.php at the server. You get a regular textbox instead of your field if Joomla doesn't see the field php file. Maybe you forgot to upload somethig or there is a path problem (windows/linux). There is a chance there is a PHP error. Set error level to Developemnt in Global configuration to see if there is some usefull information. If not done yet. – Gruz Apr 27 '17 at 18:51
  • @Gruz that's right. It was not reading the categoryList.php file. The issue was with the camelcase naming convention. When I changed it to categorylist.php it worked on server. Maybe some sort of setting was there – Liz. Apr 28 '17 at 13:09
0

[This page appears abandoned, so I'll transfer the commented solution as an answer.]

As described in the comments, the issue was with the camelcasing in the filename. For Joomla to access the file, the filename must be in lowercase:

models/fields/categoryList.php should be models/fields/categorylist.php


Additionally, I'll offer a revised getOptions() function declaration with Joomla query building syntax to replace the raw sql string and do my best to implement Joomla's php coding standards.

public function getOptions()
{
    $section = JFactory::getApplication()->input->get->get("section");
    $db = JFactory::getDbo();

    $subquery = $db->getQuery(true)
        ->select($db->quoteName("id"))
        ->from($db->quoteName("#__categories"))
        ->where($db->quoteName("title") . " = " . $db->quote($section));

    $query = $db->getQuery(true)
        ->select($db->quoteName(["id", "title"]))
        ->from($db->quoteName("#__categories"))
        ->where($db->quoteName("parent_id") . " IN (" . $subquery . ")");

    $rows = $db->setQuery($query)->loadObjectlist();
    $categories[null] = 'Please Select';
    foreach ($rows as $row)
    {
        $categories[$row->id] = $row->title;
    }
    return $categories;
}

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.