将自定义表单字段添加到Joomla插件管理员

时间:2016-01-06 16:17:23

标签: php joomla joomla3.0

我创建了一个Joomla插件,允许您选择在头部异步加载的脚本。这是通过在管理端的简单文本字段中输入关键字来完成的。我认为允许用户通过单选按钮或复选框选择脚本会更有帮助。

我需要在我的插件中添加一个自定义表单字段,将这些脚本作为选项收集,并以适当的格式显示给管理员端。目前在查看admin中的插件选项时出现500错误。我认为我所要做的就是更改JForm列表字段的getOptions()函数,但它不起作用。希望得到关于所有功能预期会采取什么以及它产生什么的一些指导。

/forms/scripts.php:

<?php
defined('_JEXEC') or die;
jimport('joomla.form.formfield');
jimport('joomla.form.helper');
JFormHelper::loadFieldClass('list');

class JFormFieldScripts extends JFormFieldList {

public function getOptions()
{
    //$fieldname = preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname);
    $options = array();
    $doc = JFactory::getDocument();
    foreach ($doc->_scripts as $url => $value) {

        $value = $url;
        $text = trim($url);
        $disabled = 0;
        $checked = 0;   
        $selected = 0;
        $tmp = array(
            'value' => $value,
            'text' => $text,
            'disable' => $disabled,
            'class' => '',
            'selected' => ($checked || $selected),
            'checked' => ($checked || $selected)
        );

// Set some event handler attributes. But really, should be using unobtrusive js.
        $tmp['onclick'] = 0;
        $tmp['onchange'] = 0;

// Add the option object to the result set.
        $options[] = (object)$tmp;
        echo $tmp;
    }

    reset($options);

    return $options;
}
}

插件xml:

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.0" type="plugin" group="system">
    <name>System - JS Async</name>
    <license>GNU General Public License version 2 or later; see LICENSE.txt</license>
    <version>1.0</version>
    <description>Choose scripts to load asynchronously in the head.</description>
    <files>
        <filename plugin="js_async">js_async.php</filename>
        <filename>index.html</filename>
        <folder>forms</folder>
    </files>
    <config>
        <fields name="params">
            <fieldset name="basic">
                <field name="file_names" type="text"
                       description="List of file names to load asynchronously."
                       label="File Name(s)"
                       size="50"
                />
            </fieldset>
            <fieldset addfieldpath="/plugins/system/js_async/forms">
                <field name="title" type="Scripts"
                       description="blah blah blah."
                       label="File Name(s)"
                />
            </fieldset>
        </fields>
    </config>
</extension>

0 个答案:

没有答案