需要 joomla 1.6 模块自定义基本设置方面的帮助吗?

发布于 2024-11-02 13:58:28 字数 458 浏览 1 评论 0原文

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

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

列表选择自然会根据每个用户的 API 密钥而改变,但因为您使用 XML 文件设置模块,所以我看不出有什么办法可以绕过列表选择选项的硬编码。

请告诉我您是否可以在 Joomla 1.6 模块中构建带有选项的动态

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

I am writing an integration module for our product to go into any Joomla 1.6 page. I need some help with getting custom data (from the API) into the basic settings part but can't seem to find a way to get it.

If you look at the documentation for the creation of a module, the settings for your module you set up in an XML format. That leaves you to hard code any values or selections without any dynamic options whatsoever. Basically what I want to do is setup a very basic module that takes three basic properties:
URL (used to define the path to the API)
API key (The API key)
List selection (Connects to the API and gets list names from your account.)

The list selection would change for every user's API key naturally but because you setup the module with an XML file I see no way around the hard coding of list selection options.

Please tell me if you can build a dynamic <select> with options in a Joomla 1.6 module.

NOTE: I say 1.6 because there is a major difference between 1.5 and 1.6 development in Joomla.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁人与我共长歌 2024-11-09 13:58:28

经过一番挣扎并且没有任何帮助之后,我终于可以说我已经做到了。以下是未来任何 Google 用户的流程:

要构建动态下拉列表,您必须做一些正确的事情,以便最终由 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 实现模块的方式之后 此处这里,我将新的参数变量类型添加到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,但这只是的意见。

Well after a hell of a struggle and no help here whatsoever I can finally say that I have done it. Here is the process for any googlers in the future:

To build a dynamic dropdown you have to do a bunch of thing right for it to be pulled together by Joomla finally.

Also remember to read the documentation carefully, This answer's methods are not contained in it but maybe someone will wake up and put it there someday.

So after inspection of how the 1.6 architecture puts module variables together using the XML build file here we go.

The XML will look something like this :

<?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>

So after following the Joomla way of implementing a module here and here, I added the new parameter variable type into the elements folder as lists.php, see the name is the same as the type you declared in the XML file.

The class inside of this file looks like this :

<?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
        );

    }

}

?>

So you will know when everything is working because your selection of the drop down list, built by the API(hence completely dynamic), will save into the module database entry with the name of the select box which you can easily retrieve by calling:

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

in your module file. In this case its called mod_your_mod.php.

I really hope that this helps you when defining customized parameters in the back end of your Joomla 1.6 modules. This allows for extreme customizations and integrates tightly with whatever application you like to use's API.

The only downside is that it might be slow, but using a bunch of checks it fails gracefully when the API is down or not pulling data through correctly. All in all a very unpleasant CMS to work with but that is only my opinion.

偏爱你一生 2024-11-09 13:58:28

如果您的需求是基本的,还有一个更简单的解决方案: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" />

我对你的挫败感表示同情 - 文档很糟糕、分散且很难找到)

There is also a much simpler solution if your needs are basic: http://docs.joomla.org/SQL_form_field_type

There is an "sql" form field type:

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

(I sympathize with your frustration - the documentation is terrible, scattered and hard to find)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文