"Overriding" protected function getOptions() of JFormFieldSql - how?

For Joomla! 3.x Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Locked
gba
Joomla! Intern
Joomla! Intern
Posts: 71
Joined: Tue Jun 03, 2014 3:37 pm

"Overriding" protected function getOptions() of JFormFieldSql - how?

Post by gba » Tue Oct 24, 2017 4:26 pm

Hi all!

I created a Joomla plugin.
In the backend of the plugin I want to use a field of type 'sql'.
But I want to load language files of a third party extension (VirtueMart) when creating the options list.
In my plugin folder I created a subdir 'fields' containing a file 'sql.php':

Code: Select all

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

JFormHelper::loadFieldClass('sql');

class MyFormFieldSql extends JFormFieldSql
{
	protected function getOptions()
	{
		$options = $this->getOptions();

		// Do something ...

		return $options;
	}
}
But I guess I have forgotten something ... nothing happens.
Function getOptions() is even not executed.

All I want is to have the values not only translated considering Joomla language variables, but also the ones of VirtueMart.

Can anyone, please, help me finding out, what is missing?
Thank you very much in advance!

Kind regards,
Gerald

 
electrocity
Joomla! Intern
Joomla! Intern
Posts: 53
Joined: Sat Mar 06, 2010 8:21 pm
Contact:

Re: "Overriding" protected function getOptions() of JFormFieldSql - how?

Post by electrocity » Tue Oct 24, 2017 9:11 pm

You need to declare the path to your field directory in the XML config.

Code: Select all

    <config>
	<fields name = "params" addfieldpath="/plugins/system/YOURPLUGIN/fields">
		<field name="fieldname" type="myfield"  
				default="0" label="MYFIELD_LABEL" >
				<option value="">JOPTION_SELECT_CATEGORY</option>
			</field>
	   ......
	</fields>
    </config>
And add the file inside this folder

Plugin class need to be JFormFieldMyfield in case of type is Myfield and file name myfield.php
and file path /plugins/system/YOURPLUGIN/fields/myfield.php

Code: Select all

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

JFormHelper::loadFieldClass('sql');

class JFormFieldMyfield extends JFormFieldSql
{
   protected function getOptions()
   {
      $options = $this->getOptions();

      // Do something ...

      return $options;
   }
}
Free and paid Joomla & virtuemart extensions https://shop.st42.fr/en

gba
Joomla! Intern
Joomla! Intern
Posts: 71
Joined: Tue Jun 03, 2014 3:37 pm

Re: "Overriding" protected function getOptions() of JFormFieldSql - how?

Post by gba » Wed Oct 25, 2017 6:43 am

Hi!

Thank you very much for your time!

Can I also use the same class name:

Code: Select all

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

JFormHelper::loadFieldClass('sql');

class JFormFieldSql extends JFormFieldSql
{
   protected function getOptions()
   {
      $options = $this->getOptions();

      // Do something ...

      return $options;
   }
}
I guess no, right?
Because the class name needs to be unique...

gba
Joomla! Intern
Joomla! Intern
Posts: 71
Joined: Tue Jun 03, 2014 3:37 pm

Re: "Overriding" protected function getOptions() of JFormFieldSql - how?

Post by gba » Wed Oct 25, 2017 8:00 am

Code: Select all

$options = $this->getOptions();
... causes the server to crash - why?

gba
Joomla! Intern
Joomla! Intern
Posts: 71
Joined: Tue Jun 03, 2014 3:37 pm

Re: "Overriding" protected function getOptions() of JFormFieldSql - how?

Post by gba » Wed Oct 25, 2017 8:09 am

It needs to be:

Code: Select all

$options = parent::getOptions();

electrocity
Joomla! Intern
Joomla! Intern
Posts: 53
Joined: Sat Mar 06, 2010 8:21 pm
Contact:

Re: "Overriding" protected function getOptions() of JFormFieldSql - how?

Post by electrocity » Wed Oct 25, 2017 7:26 pm

I don't checked your code, only changed the class naming needed to do it work.
I hope it work now ?
Free and paid Joomla & virtuemart extensions https://shop.st42.fr/en

gba
Joomla! Intern
Joomla! Intern
Posts: 71
Joined: Tue Jun 03, 2014 3:37 pm

Re: "Overriding" protected function getOptions() of JFormFieldSql - how?

Post by gba » Thu Oct 26, 2017 5:27 am

Works - thank you!

 

Locked

Return to “Joomla! 3.x Coding”