在過去的教程中,我們展示了如何創建額外的字段與T3文章框架。今天我們將介紹另一個額外的字段類型——額外的表單字段。
您可以添加任何形式的額外字段比如橫幅、聯系人、搜索或菜單…… 一般來說,它包括以下步驟: 閑話少說,讓我們去得到它。
定義額外的字段組和額外的字段
得到額外的字段的形式
如何使用額外的字段
額外的字段的樣式
在本教程中,我們將關注如何為菜單添加額外的字段,對于其他形式,它幾乎是相同的。

#1: 為 com-menu 定義擴展字段
在 templates\模板名稱\etc\extrafields.文件夾下,為每個表單創建一個 .xml 文件,在創建的 .xml 文件中,我們將為這個表單定義所有擴展字段。這個 .xml 文件名最好能表明這個表單的使用形式,所以命名時可以進行規范,例如使用這種格式: com_COM-NAME.VIEW.xml
如com-menu 表單,名字可以命名為: com_menus.item.xml
提示:如何規范的命名你的擴展字段文件,給你的建議是到網站管理后臺,打開你要添加擴展字段的位置,查看頂部的url鏈接中的包含內容,例如, 文件名為 = com_option.view.xml

在每個.xml 文件中我們可以:
定義選項卡添加額外的字段
- <fieldset name="tab-attribute"label="Tab Label"description="Tab Label Description">
為表單定義擴展字段:
- <field name="extra-field-1"type="extra-field-type"default=""label="Extra field label"description="Extra field description"/>
諸如這樣的代碼在這個 com_menus.item.xml 文件中

- <?xml version="1.0" encoding="utf-8"?>
- <form>
- <fields name="params">
- <fieldset name="extra-field" label="Extra field" description="Extra field description">
- <field name="masshead-title" type="text" default="" label="Masshead title" description="Masthead title" />
- <field name="masshead-slogan" type="textarea" default="" label="Masshead slogan" description="Masthead description" filter="RAW" />
- </fieldset>
- </fields>
- </form>
這個com_menus.item.xml 定義了:
- 添加擴展字段到 “Extra field” tab
- Extra field 1: Masthead title
- Extra field 2: Masthead description
#2: 在菜單項目中獲取擴展字段
Here is the Code format to get extra field for menu items:
- $menuitem->params->get(extra-field-name);// $menuitem 是菜單項目對象
- // sample code for get extra field from active menu item
- $menu =JFactory::getApplication()->getMenu();
- $active = $menu->getActive()? $menu->getActive(): $menu->getDefault();
- $active->params->get('masthead-title');
- $active->params->get('masthead-slogan');
In Joomlart.com, we get the extra fields in masthead block and load the masthead block in all layouts. The advantage of getting the extra fields in the masthead block is that we can add style for the extra fields in the masthead.php.
masthead.php file (templates/t3_bs3_blank/tps/blocks)
If you don’t see the file, just create new file then add the following code to the file.
- <?php
- /**
- * @package T3 Blank
- * @copyright Copyright (C) 2005 - 2012. All rights reserved.
- * @license GNU General Public License version 2 or later; see LICENSE.txt
- */
- defined('_JEXEC')ordie;
- $input =JFactory::getApplication()->input;
- $menu =JFactory::getApplication()->getMenu();
- $activemenu = $menu->getActive()? $menu->getActive(): $menu->getDefault();
- $query = $activemenu->query;
- $mast_title = $mast_slogan ='';
- if((!isset ($query['option'])|| $query['option']== $input->get('option'))
- &&(!isset ($query['view'])|| $query['view']== $input->get('view'))
- &&(!isset ($query['id'])|| $query['id']== $input->get('id'))){
- $mast_title = $activemenu->params->get('masthead-title');
- $mast_slogan = $activemenu->params->get('masthead-slogan');
- }
- $masthead_position ='masthead';
- ?>
- <?php if($mast_title || $this->countModules($masthead_position)):?>
- <divclass="page-masthead">
- <?php if($mast_title):?>
- <divclass="jumbotron jumbotron-primary">
- <divclass="container">
- <h1><?php echo $mast_title ?></h1>
- <p><?php echo $mast_slogan ?></p>
- </div>
- </div>
- <?php endif ?>
- </div>
- <?php endif ?>
加載 masthead到布局中
打開布局文件(templates/t3_bs3_blank/tps/)如果你要增加這個masthead布局到模板中顯示,則添加下面一行
- <?php $this->loadBlock(masthead)?>

#3: 為擴展字段添加樣式
你可以為添加的擴展字段添加樣式,可以使用為擴展字段使用Class標簽來定義樣式,在 masthead.php 文件中 .less file in your template LESS folder at templates/t3_bs3_blank/less.

請編譯LESS 成為CSS 這樣新增加的less樣式就會生到css文件中了.

#4: 如何使用擴展字段
打開一個菜單項目, 你可以看到一個新的選項卡 (Extra field tab)你可以添加內容到這個選項卡內.

現在到網站前臺進行檢查確認是否生效。

這些都是你可以遵循一些簡單的步驟來添加額外的字段com-menu和為您的網站創建一個獨特的菜單項。
希望你喜歡今天的教程,請繼續關注我們即將到來的教程添加Joomla定制字段與T3模塊框架。





