1

我正在为我们的产品编写一个集成模块以进入任何 Joomla 1.6 页面。我需要一些帮助来获取自定义数据(来自 API)到基本设置部分,但似乎无法找到获取它的方法。

如果您查看有关创建模块的文档,您会以 XML 格式设置模块的设置。这使您可以在没有任何动态选项的情况下对任何值或选择进行硬编码。基本上我想做的是设置一个非常基本的模块,它具有三个基本属性: URL(用于定义 API 的路径) API 密钥(API 密钥) 列表选择(连接到 API 并从您的帐户获取列表名称.)

对于每个用户的 API 密钥,列表选择自然会发生变化,但是因为您使用 XML 文件设置模块,所以我认为无法绕过列表选择选项的硬编码。

<select>请告诉我您是否可以在 Joomla 1.6 模块中使用选项构建动态。

注意:我说 1.6 是因为在 Joomla 中 1.5 和 1.6 开发之间存在重大差异。

4

2 回答 2

4

经过一场地狱般的斗争并且没有任何帮助,我终于可以说我已经做到了。以下是未来任何谷歌员工的流程:

要构建动态下拉列表,您必须做很多正确的事情才能最终被 Joomla 拉到一起。

还记得仔细阅读文档,这个答案的方法不包含在其中,但也许有一天有人会醒来并把它放在那里。

因此,在检查了 1.6 架构如何使用 XML 构建文件将模块变量放在一起之后,我们开始了。

XML 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<extension type="module" version="1.6.0" client="site">
    <name>...</name>
    <author>...</author>
    <creationDate>April 2011</creationDate>
    <copyright>Copyright (C) 2011 ... All rights reserved.</copyright>
    <license>GNU General Public License version 2 or later; see LICENSE.txt</license>
    <authorEmail>...</authorEmail>
    <authorUrl>...</authorUrl>
    <version>1.6.0</version>
    <description>
        ...
    </description>
    <files>
        <filename module="mod_your_mod">mod_your_mod.php</filename>
        <filename>helper.php</filename>
        <filename>index.html</filename>
        <folder>tmpl</folder>
        <folder>elements</folder>
        <folder>lib</folder>
    </files>
    <languages />
    <help />
    <config>
        <fields name="params">
            <fieldset name="basic">
                <!-- Custom field, list selection from API -->
                <!-- Path to module external parameters -->
                <field addfieldpath="/modules/mod_your_mod/elements" 
                    name="mod_your_mod_id_selection" <!-- Name of variable -->
                    type="lists" <!-- New variable type -->
                    default="" 
                    label="Select lists" 
                    description="This is the list selection, where you select the list a contact can subscribe to." />
            </fieldset>
            <fieldset
                name="advanced">
                <field
                    name="layout"
                    type="modulelayout"
                    label="JFIELD_ALT_LAYOUT_LABEL"
                    description="JFIELD_ALT_MODULE_LAYOUT_DESC" />
                <field
                    name="moduleclass_sfx"
                    type="text"
                    label="COM_MODULES_FIELD_MODULECLASS_SFX_LABEL"
                    description="COM_MODULES_FIELD_MODULECLASS_SFX_DESC" />
                <field
                    name="cache"
                    type="list"
                    default="1"
                    label="COM_MODULES_FIELD_CACHING_LABEL"
                    description="COM_MODULES_FIELD_CACHING_DESC">
                    <option
                        value="1">JGLOBAL_USE_GLOBAL</option>
                    <option
                        value="0">COM_MODULES_FIELD_VALUE_NOCACHING</option>
                </field>
                <field
                    name="cache_time"
                    type="text"
                    default="900"
                    label="COM_MODULES_FIELD_CACHE_TIME_LABEL"
                    description="COM_MODULES_FIELD_CACHE_TIME_DESC" />
                <field
                    name="cachemode"
                    type="hidden"
                    default="itemid">
                    <option value="itemid"></option>
                </field>
            </fieldset>
        </fields>
    </config>
</extension>

所以在按照Joomla的herehere实现模块的方式之后,我将新的参数变量类型添加到elements文件夹中lists.php,看到名称与您在XML文件中声明的类型相同。

该文件中的类如下所示:

<?php
// No direct access
defined('_JEXEC') or die('Direct Access to this location is not allowed.');
jimport('joomla.html.html');
//import the necessary class definition for formfield
jimport('joomla.form.formfield');

// Include API utility file
require_once(dirname(__FILE__) . '/../lib/your_api.php');

class JFormFieldLists extends JFormField
{
    /**
     * The form field type.
     *
     * @var  string
     * @since 1.6
     */
    protected $type = 'lists'; //the form field type see the name is the same

    /**
     * Method to retrieve the lists that resides in your application using the API.
     *
     * @return array The field option objects.
     * @since 1.6
     */
    protected function getInput()
    {
        $options = array();
        $attr = '';

        $attr .= ' multiple="multiple"';
        $attr .= ' style="width:220px;height:220px;"';

        // Get the database instance
        $db = JFactory::getDbo();
        // Build the select query
        $query = 'SELECT params FROM jos_modules'
            . ' WHERE module="mod_your_mod"';
        $db->setQuery($query);
        $params = $db->loadObjectList();

        // Decode the options to get thje api key and url
        $options = json_decode($params[0]->params, true);

        // Gracefully catch empty fields
        if ( empty($options['api_key']) === true )
        {
            $tmp = JHtml::_(
                'select.option',
                0,
                'No lists available, please add an API key'
            );

            $lists[] = $tmp;

            // The dropdown output, return empty list if no API key specified
            return JHTML::_(
                'select.genericlist',
                $lists,
                $this->name,
                trim($attr),
                'value',
                'text',
                $this->value,
                $this->id
            );
        }
        if ( empty($options['url']) === true )
        {
            $tmp = JHtml::_(
                'select.option',
                0,
                'No lists available, please add the enterprise URL'
            );

            $lists[] = $tmp;

            // The dropdown output, return empty list if no API key specified
            return JHTML::_(
                'select.genericlist',
                $lists,
                $this->name,
                trim($attr),
                'value',
                'text',
                $this->value,
                $this->id
            );

        }

        // Create a new API utility class
        $api = new APIClass(
            $options['url'],
            $options['api_key']
        );

        try
        {
            // Get the lists needed for subscription
            $response = $api->getLists();
        }
        catch ( Exception $e )
        {
            $tmp = JHtml::_(
                'select.option',
                0,
                'Could not connect to the API'
            );

            $lists[] = $tmp;

            // The dropdown output, return empty list if no API key specified
            return JHTML::_(
                'select.genericlist',
                $lists,
                $this->name,
                trim($attr),
                'value',
                'text',
                $this->value,
                $this->id
            );
        }

        $lists = array();

        // Builds the options for the dropdown
        foreach ( $response['data'] as $list )
        {
            // Build options object here
            $tmp = JHtml::_(
                'select.option',
                $list['list_id'],
                $list['list_name']
            );

            $lists[] = $tmp;
        }

        // The dropdown output

        /* The name of the select box MUST be the same as in the XML file otherwise 
         * saving your selection using Joomla will NOT work. Also if you want to make it 
         * multiple selects don't forget the [].
         */
        return JHTML::_(
            'select.genericlist',
            $lists,
            'jform[params][mod_your_mod_id_selection][]',
            trim($attr),
            'value',
            'text',
            $this->value,
            $this->id
        );

    }

}

?>

因此,您将知道何时一切正常,因为您选择的由 API 构建的下拉列表(因此是完全动态的)将使用选择框的名称保存到模块数据库条目中,您可以通过调用轻松检索:

$api_key = $params->get('api_key', '');

在你的模块文件中。在这种情况下,它被称为mod_your_mod.php.

我真的希望这对您在 Joomla 1.6 模块的后端定义自定义参数时有所帮助。这允许极端自定义并与您喜欢使用的任何应用程序的 API 紧密集成。

唯一的缺点是它可能很慢,但是当 API 关闭或无法正确提取数据时,使用一堆检查它会优雅地失败。总而言之,这是一个非常令人不快的 CMS,但这只是我的看法。

于 2011-06-06T10:49:52.600 回答
3

如果您的需求是基本的,还有一个更简单的解决方案:http: //docs.joomla.org/SQL_form_field_type

有一个“sql”表单字段类型:

<field name="title" type="sql" default="10" label="Select an article" query="SELECT id AS value, title FROM #__content" />

(我对您的挫败感表示同情 - 文档很糟糕,分散且难以找到)

于 2011-09-30T15:45:34.190 回答