Joomla 2.5 Load custom field in components layout

2019-08-15 04:56发布

Im trying to load custom field in my backend component default view (default.php):

JFormHelper::addFieldPath(JPATH_COMPONENT . '/models/fields');
$productType = JFormHelper::loadFieldType('ProductType',false);

$productTypeOptions = $productType->getOptions(); 

But I get a fatal error:

Fatal error: Call to a member function children() on a non-object in xxx\libraries\joomla\form\fields\list.php on line 89

When I load this custom field into the form, everything works perfectly.

Any ideas?

3条回答
Emotional °昔
2楼-- · 2019-08-15 05:03

Well, I tried to expand my fellow above idea but it seemed an inappropiate edit, I put it here then:

This worked for me. In your getOptions, if you have something like the getOptions found here (http://docs.joomla.org/How_to_add_custom_filters_to_component_admin), you´ll have this line:

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

This is the one that makes the error. Why? Well, I´m not sure. If you see the related file, you´ll find this:

foreach ($this->element->children() as $option)

So the problem is that you are calling children() on the model parent that it seems is not initialized. Why the array_merge is needed? Is discussed here (http://forum.joomla.org/viewtopic.php?f=626&t=782877)

My explanation is more like a dirty blind patch, but hope it helps to move forward.

查看更多
干净又极端
3楼-- · 2019-08-15 05:16

In your $productType->getOptions() function, Try removing:
$options = array_merge(parent::getOptions(), $options);

查看更多
萌系小妹纸
4楼-- · 2019-08-15 05:30

Make sure you are adding the correct path to the fields

查看更多
登录 后发表回答