简体   繁体   English

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

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

Ok this is not a question, more an answer since i actually didn't find the specific response until i debugged it!好的,这不是一个问题,而是一个答案,因为在我调试它之前我实际上没有找到具体的响应!

TLDR: always name your class like they should be ! TLDR:总是像他们应该的那样命名您的 class !

So i am new to Joomla and i personnally hate it but that's on me, i prefer to not use cms when building my website.所以我是 Joomla 的新手,我个人讨厌它,但那是我的问题,我更喜欢在构建我的网站时不使用 cms。 Anyway I am creating a module since i actually needs logic and i am fed up to stuff everything in JS, then I encountered the weird case where i actually need to make sure the input of the next webmaster is not some garbage.无论如何,我正在创建一个模块,因为我实际上需要逻辑并且我厌倦了在 JS 中填充所有内容,然后我遇到了一个奇怪的情况,我实际上需要确保下一个网站管理员的输入不是垃圾。 And at that point i said darn it, how could i make a custom field inside my module without creating more?那时我说该死,我怎么能在我的模块中创建一个自定义字段而不创建更多? Well the answer is simple, just follow this guide: https://docs.joomla.org/Creating_a_custom_form_field_type right?答案很简单,只需遵循本指南: https://docs.joomla.org/Creating_a_custom_form_field_type对吗?

Well duh its simple enough, the one thing that is not clear is how to actually use the custom field you made, for sure a quick fix is to move it to libraries\joomla\form\fields but I just forgot something, joomla doesn't know how to register stuff if you name then incorrectly.嗯,它很简单,不清楚的一件事是如何实际使用您制作的自定义字段,当然快速修复是将其移动到libraries\joomla\form\fields但我只是忘记了一些东西,joomla 没有如果你的名字不正确,不知道如何注册东西。 Take this example:举个例子:

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

Do you notice anything wrong?你注意到有什么不对吗? Ok i will give you a hint, the class name is without correct CamelCase.好的,我会给你一个提示,class 名称没有正确的 CamelCase。 But yeah why does that matter?但是,是的,为什么这很重要? well introduce Joomla registry by default it will use the filename and Case and prefix it to make sure its the correct class that is loaded.很好地介绍 Joomla 注册表,默认情况下它将使用文件名和大小写并为其添加前缀以确保其加载的 class 正确。

Ok maybe i was a bit too fast!好吧,也许我有点太快了! You need to put your custom field (or forms) in the file name here year.php then you name the class correctly with camel case and the correct prefix and you specify a type which can be whatever because Joomla i guess.您需要将您的自定义字段(或表单)放在此处的文件名中year.php然后使用驼峰式大小写和正确的前缀正确命名 class 并指定一个可以是任何类型的类型,因为 Z57AC91865E5064F2531CF6209888

Anyway how to integrate it locally?无论如何如何在本地集成它? Well simple enough put your new year.php anywhere in your module, then in your main xml where you are putting your config just do so:很简单,把你的 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>

You notice i use the addfieldpath which register the location (but if you didnt do the naming correctly on the class or the filename of the php file then the type will do nothing or default to text) then i put a name (the id) and the type which to be fair could be Year YeAr YeaR (any capitalization).您注意到我使用了注册位置的 addfieldpath(但如果您没有在 class 或 php 文件的文件名上正确命名,那么该类型将不执行任何操作或默认为文本)然后我输入一个名称(ID)和公平的类型可以是 Year YeAr YeaR (任何大小写)。 To me this look funny that the registering is so bad on Joomla, i would definetly prefer to register via the type variable inside the custom field and this was case sensitive but yeah i guess this is why using a poorly documented cms is not your go to...对我来说这看起来很有趣,因为 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 first letter) then in your xml include the path with addfieldpath and specify the type as any casing of the name of your php file. 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 文件名称的任何大小写。

Cheers,干杯,

Neil尼尔

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 first letter) then in your xml include the path with addfieldpath and specify the type as any casing of the name of your php file. 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