簡體   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