繁体   English   中英

如何在 Joomla 的模块内使用自定义字段?

[英]How to use a custom field inside a module in Joomla?

好的,这不是一个问题,而是一个答案,因为在我调试它之前我实际上没有找到具体的响应!

TLDR:总是像他们应该的那样命名您的 class !

所以我是 Joomla 的新手,我个人讨厌它,但那是我的问题,我更喜欢在构建我的网站时不使用 cms。 无论如何,我正在创建一个模块,因为我实际上需要逻辑并且我厌倦了在 JS 中填充所有内容,然后我遇到了一个奇怪的情况,我实际上需要确保下一个网站管理员的输入不是垃圾。 那时我说该死,我怎么能在我的模块中创建一个自定义字段而不创建更多? 答案很简单,只需遵循本指南: https://docs.joomla.org/Creating_a_custom_form_field_type对吗?

嗯,它很简单,不清楚的一件事是如何实际使用您制作的自定义字段,当然快速修复是将其移动到libraries\joomla\form\fields但我只是忘记了一些东西,joomla 没有如果你的名字不正确,不知道如何注册东西。 举个例子:

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

JFormHelper::loadFieldClass('list');

class JFormFieldyear extends JFormFieldList
{
    protected $type = 'Year';

    public function getOptions() {
        $years= array();
        for ($i= date("Y");$i>1800;$i--){
            array_push($years,array('value' => $i, 'text' => $i));
        }
        // Merge any additional options in the XML definition.
        $options = array_merge(parent::getOptions(), $years);
        // preselect last year
        $this->value = array( date("Y"));

        return $options;
    }
}

你注意到有什么不对吗? 好的,我会给你一个提示,class 名称没有正确的 CamelCase。 但是,是的,为什么这很重要? 很好地介绍 Joomla 注册表,默认情况下它将使用文件名和大小写并为其添加前缀以确保其加载的 class 正确。

好吧,也许我有点太快了! 您需要将您的自定义字段(或表单)放在此处的文件名中year.php然后使用驼峰式大小写和正确的前缀正确命名 class 并指定一个可以是任何类型的类型,因为 Z57AC91865E5064F2531CF6209888

无论如何如何在本地集成它? 很简单,把你的 new year.php 放在你的模块中的任何地方,然后在你的主 xml 中你放置你的配置就这样做:

<config>
        <fields name="params">
            <fieldset name="basic">
                <field addfieldpath="/modules/<your_module_name>/<the_folder_chain_where_you_put_year.php>" name="<an_unique_id>" type="year" description="<whatever_maybe_use_an_ini>" label="<whatever_maybe_use_an_ini>" (showon="<other_id>:<other_value>" multiple="multiple")/>
           </fieldset>
       </fields>
</config>

您注意到我使用了注册位置的 addfieldpath(但如果您没有在 class 或 php 文件的文件名上正确命名,那么该类型将不执行任何操作或默认为文本)然后我输入一个名称(ID)和公平的类型可以是 Year YeAr YeaR (任何大小写)。 对我来说这看起来很有趣,因为 Joomla 上的注册非常糟糕,我肯定更喜欢通过自定义字段中的类型变量进行注册,这是区分大小写的,但我想这就是为什么使用记录不充分的 cms 不是你的 go 的原因...

TLDR: Custom fields: https://docs.joomla.org/Creating_a_custom_form_field_type Put the php file in any folder in your module, name it with what type you want in lowercase and name the class inside with JFormField<Name_of_your_file> (upper case on第一个字母)然后在您的 xml 中包含带有 addfieldpath 的路径,并将类型指定为 php 文件名称的任何大小写。

干杯,

尼尔

TLDR: Custom fields: https://docs.joomla.org/Creating_a_custom_form_field_type Put the php file in any folder in your module, name it with what type you want in lowercase and name the class inside with JFormField<Name_of_your_file> (upper case on第一个字母)然后在您的 xml 中包含带有 addfieldpath 的路径,并将类型指定为 php 文件名称的任何大小写。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM