Plugin or module list type parameter with dynamic option values

992 views
Skip to first unread message

nant

unread,
May 23, 2013, 4:26:31 PM5/23/13
to joomla-de...@googlegroups.com
Hi gang!

I am trying to implement a Joomla 3.1 user plugin and I have the following issue:

I want to have a drop-down list type plugin parameter where the option values are not statically provided in the plugin xml file, but they are populated dynamically from php code that grabs them from another extension via some api calls.

I  did find an old reference here:
http://docs.joomla.org/SQLMultiSelectX

but that is obviously outdated.

Is there something more current or some example that can be shared.

Thanks for reading!
Nick

Mark Dexter

unread,
May 23, 2013, 5:05:29 PM5/23/13
to joomla-de...@googlegroups.com
You can use a plugin to modify the values of a form. Use the onContentPrepareForm event. The basic idea is to create a skeleton form using a normal XML file. Then use the onContentPrepareForm event to trigger your plugin. There you can add, remove, or modify form elements and attributes. So you can dynamically change the form using the API. See the core file plugins/user/profile/profile.php for an example. And look at the JForm set methods to see the methods available to you. Good luck. Mark


--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Gary Mort

unread,
Jun 26, 2013, 5:01:23 PM6/26/13
to joomla-de...@googlegroups.com


On Thursday, May 23, 2013 5:05:29 PM UTC-4, Mark Dexter wrote:
You can use a plugin to modify the values of a form. Use the onContentPrepareForm event. The basic idea is to create a skeleton form using a normal XML file. Then use the onContentPrepareForm event to trigger your plugin. There you can add, remove, or modify form elements and attributes.

JForm has some merge functions as well, so you can use them to merge changes from 2 different definitions.

For example, rather than keep cut/pasting the same field definitions into each form field[where I am using the same fields in multiple forms] - I setup skeleton form without labels, descriptions, and default values for each form - then merged the common form xml file with all that data.

The main purpose of the skeleton file was to provide layout information[ie I used a foreach loop to create each fields output, and the order was defined by the order in the file].

One oddity here is that you can't always change the type of a field with merge, ie if I defined all the fields as text fields and then tried to merge in another definition where the type changed to select with some options, the form would display as a select field but the options would not be merged - I'm assuming it is some sort of level issue.

Franz

unread,
Jun 27, 2013, 8:43:04 AM6/27/13
to joomla-de...@googlegroups.com
I think that what you're looking at is a custom form field.

Have a look at Watchful "Apps" that you can download for free. Some of those use exactly those kind of dynamic parameters, implemented with custom fields.

Please note that these apps support both custom fields for Joomla 1.5 and Joomla 2.5/3.x, you'll have to look only at the "fields" subfolder.

Ove

unread,
Jun 28, 2013, 8:09:00 AM6/28/13
to joomla-de...@googlegroups.com, Gary Mort

> One oddity here is that you can't always change the type of a field
> with merge, ie if I defined all the fields as text fields and then
> tried to merge in another definition where the type changed to select
> with some options, the form would display as a select field but the
> options would not be merged - I'm assuming it is some sort of level issue.

IMO it's a bug since after V2.4. Reported with
http://joomlacode.org/gf/project/joomla/tracker/?action=TrackerItemEdit&tracker_id=8103&tracker_item_id=28832


I still do not know the reason for the code change but with V2.4
everything worked for me especially replacing fields with options.

WP4J

unread,
Jul 6, 2013, 3:53:38 AM7/6/13
to joomla-de...@googlegroups.com
I have found that implementing a custom field (extending JFormFieldList) is a good way to achieve this. Consider the following custom field code:

// No direct access to this file
defined('_JEXEC') or die;

// import the list field type
jimport('joomla.form.helper');
JFormHelper::loadFieldClass('list');

class JFormFieldMylist extends JFormFieldList
{
        /**
         * The field type.
         *
         * @var         string
         */
        protected $type = 'mylist';

        /**
         * Method to get a list of options for a list input.
         *
         * @return      array           An array of JHtml options.
         */
        protected function getOptions()
        {
JLoader::register('myhelper', JPATH_ROOT . '/path/to/myhelper/helper.php');

            $helper = new myhelper();

            $results = $helper->get_list();

            $options = array();

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

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

            return $options;

Fedir

unread,
Jul 6, 2013, 4:52:16 AM7/6/13
to joomla-de...@googlegroups.com
or you can use JavaScript like this mooOptionTree
and then field code
<?php
class JFormFieldMytrickyfield extends JFormField 
{
   protected $type = 'mytrickyfield';

   protected function getInput()
   {
     //add script
     //get options tree
     //add script options

     return '<div id="wrapper-for-script"></div>';
   }
}



Субота, 6 липня 2013 р. 10:53:38 UTC+3 користувач WP4J написав:
Reply all
Reply to author
Forward
0 new messages