2

I was going through this part of the tutorial:
https://docs.joomla.org/J3.x:Developing_an_MVC_Component/Using_the_database
Except I tried using my own names instead of 'helloworld'.


Everything else works fine, even the site-page loads the text from the database. The problem is:

On the backend 'Menu Item' options, where there's supposed to be a list, it's just showing a blank textbox.

enter image description here


The codes I've used are:

site/views/uploadwizard/tmpl/default.xml:

<metadata>
    <layout title="COM_OMITTED_UPLOADWIZARD_VIEW_DEFAULT_TITLE">
        <message>COM_OMITTED_UPLOADWIZARD_VIEW_DEFAULT_DESC</message>
    </layout>

    <fields
        name="request"
        addfieldpath="/administrator/components/com_omitted/models/fields"
        >
        <fieldset name="request">
            <field
                name="id"
                type="ulwiz_wmode"
                label="COM_OMITTED_UPLOADWIZARD_FIELD_WMODE_LABEL"
                description="COM_OMITTED_UPLOADWIZARD_FIELD_WMODE_DESC"
                />
        </fieldset>
    </fields>
</metadata>

admin/models/fields/ulwiz_wmode.php:

<?php
defined('_JEXEC') or die('Restricted access');

JFormHelper::loadFieldClass('list');

class JFormFieldUlWiz_WMode extends JFormFieldList
{
    protected function getOptions()
    {
        $db    = JFactory::getDBO();
        $query = $db->getQuery(true);
        $query->select('id,wmode');
        $query->from('#__omitted_ulwiz_wmode');
        $db->setQuery((string) $query);
        $messages = $db->loadObjectList();
        $options  = array();

        if ($messages)
        {
            foreach ($messages as $message)
            {
                $options[] = JHtml::_('select.option', $message->id, $message->wmode);
            }
        }

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

        return $options;
    }
}

And the Database description:

DROP TABLE IF EXISTS `#__omitted_ulwiz_wmode`;

CREATE TABLE `#__omitted_ulwiz_wmode` (
    `id`        INT(11)     NOT NULL AUTO_INCREMENT,
    `wmode`     VARCHAR(25) NOT NULL,
    `published` tinyint(4)  NOT NULL DEFAULT '1',
    PRIMARY KEY (`id`)
)

    ENGINE =MyISAM
    AUTO_INCREMENT =0
    DEFAULT CHARSET =utf8;

INSERT INTO `#__omitted_ulwiz_wmode` (`wmode`) VALUES
('Option1Text'),
('Option2Text');

How do I fix this? I've been at it for hours and am unable to move over to the next part of the tutorial hence.

1 Answer 1

3

The problem is that it is not able to get the field. I see filename you have defined as ulwiz_wmode.php and in class name you are using this JFormFieldUlWiz_WMode. Make sure the naming convention is very proper and should be same as class name and filename. It should be JFormFieldUlwiz_wmode.

<?php
defined('_JEXEC') or die('Restricted access');

JFormHelper::loadFieldClass('list');

class JFormFieldUlwiz_wmode extends JFormFieldList
{
   protected $type = 'ulwiz_wmode'; //also add this line

   protected function getOptions()
    {
        //your code
    }
}

You are creating your own custom field. For more details on custom fields refer this documentation:

https://docs.joomla.org/Creating_a_custom_form_field_type

3
  • Apologies. $type='ulwiz_wmode' is there in the actual code, forgot to copy it to here. I thought the $type automatically hooked the class to the right field type. So, I read up on the documentation in the link and wow, I didn't know the naming conventions were this strict. Also googling 'Camel Casing' gave a few hits where devs were asked to choose either CamelCasing or under_score naming conventions.
    – Glitch
    Commented Aug 31, 2017 at 14:09
  • 2
    Finally tried out the coding: Ulwiz_wmode didn't work. But UlwizWmode and even UlWizWMode worked. I guess underscores aren't allowed here. I'm going to mark this as Answer since naming was indeed the issue. Thanks for the help! Funny though, I used OmittedTableUlWiz_WMode as the JTable class and that works, somehow.
    – Glitch
    Commented Aug 31, 2017 at 14:09
  • 1
    I am glad it helped. :)
    – Liz.
    Commented Aug 31, 2017 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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