简体   繁体   English

如何在joomla组件视图模板中使用自定义字段

[英]How to use a custom field in a joomla component view template

i have created a dropdown select list custom field as what is taught in joomla document, but i do not get the idea of calling this field in a component template. 我已经按照joomla文档中的说明创建了一个下拉选择列表自定义字段,但是我不知道在组件模板中调用此字段的想法。

the code suggested in the document is like this, 文档中建议的代码是这样的,

//Get custom field
JFormHelper::addFieldPath(JPATH_COMPONENT . '/models/fields');
$cities = JFormHelper::loadFieldType('City', false);
$cityOptions=$cities->getOptions(); // works only if you set your field getOptions on public!!

but how can it show like a select form? 但是如何像选择表单一样显示呢? like 喜欢

<select>
      <option></option>
      <option></option>
      <option></option>
</select>

The field definition code is like this: 字段定义代码如下:

<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

jimport('joomla.form.formfield');

// The class name must always be the same as the filename (in camel case)
class JFormFieldRank extends JFormField {

    //The field class must know its own type through the variable $type.
    protected $type = 'Rank';

    public function getLabel() {
            // code that returns HTML that will be shown as the label
    }

    public function getInput() {
            // code that returns HTML that will be shown as the form field
            return '<select id="'.$this->id.'" name="'.$this->name.'">'.
                   '<option value="0" >00 President</option>'.
                   '<option value="1" >01 Vice President</option>'.
                   '<option value="10" >10 General Secretary</option>'.
                   '<option value="20" >20 Financial Chair</option>'.
                   '<option value="21" >21 Financial Deputy Chair</option>'.
                   '<option value="30" >30 Academics Chair</option>'.
                   '<option value="31" >31 Academics Deputy Chair</option>'.
                   '<option value="40" >40 Public Relation Chair</option>'.
                   '<option value="41" >41 Public Relation Deputy Chair</option>'.
                   '<option value="50" >50 Publicity Chair</option>'.
                   '<option value="51" >51 Publicity Deputy Chair</option>'.
                   '<option value="60" >60 Social and Sports Chair</option>'.
                   '<option value="61" >61 Social and Sports Deputy Chair</option>'.
                   '<option value="70" >70 Logistics and Welfare Chair</option>'.
                   '<option value="71" >71 Logistics and Welfare Deputy Chair</option>'.
                   '<option value="80" >80 IPSSSCB Advisor</option>'.                       
                   '</select>';

    }
}

Second, the xml file in the forms folder, what should be its name? 其次,forms文件夹中的xml文件,其名称应为什么? the same with the custom field name? 与自定义字段名称相同吗? which is rank.xml ? 哪个是rank.xml

and in the xml file, like following: can i change the name, label, description tag? 并在xml文件中,如下所示:我可以更改名称,标签,描述标签吗?

<field name="title" type="Rank" label="JGLOBAL_TITLE"
    description="JFIELD_TITLE_DESC"
    required="true" />

<fieldset addfieldpath="/administrator/components/com_committee/models/fields">

Thank you! 谢谢!

This looks like a totally normal list field to me, why are you defining it as a custom field? 对我来说,这似乎是一个完全正常的列表字段,为什么将其定义为自定义字段? That is say type="list" and then put your xml in the xml file just as for any list field (there are dozens of examples in the core forms). 也就是说,键入type =“ list”,然后将您的xml放入xml文件中,就像处理任何列表字段一样(核心表单中有数十个示例)。

<field name="title" type="list" label="COM_MYCOMPONENT_FIELD_TITLE_LABEL"
        description="COM_MYCOMPONNET_TITLE_DESC"
        required="true" >
        <option value="0" >00 President</option>
        <option value="1" >01 Vice President</option>
        <option value="10" >10 General Secretary</option>
        <option value="20" >20 Financial Chair</option>
        <option value="21" >21 Financial Deputy Chair</option>
        <option value="30" >30 Academics Chair</option>
         ...
        <option value="80" >80 IPSSSCB Advisor</option>
</field>

Of course you can change the text, that's the whole point of having those as string keys, you make your own keys in your component language file. 当然,您可以更改文本,这就是将它们作为字符串键的全部要点,您可以在组件语言文件中创建自己的键。 Again look at any form field in a core form. 再次查看核心表单中的任何表单字段。 Best practice would be to use language keys for the values also. 最佳做法是也将语言键用于这些值。

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

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