在 T3 Framework中为菜单添加Joomla自定义字段(extra fields)

在过去的教程中,我们展示了如何创建额外的字段与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 文件中我们可以:

定义选项卡添加额外的字段

  1. <fieldset name="tab-attribute"label="Tab Label"description="Tab Label Description">

为表单定义扩展字段:

  1. <field name="extra-field-1"type="extra-field-type"default=""label="Extra field label"description="Extra field description"/>

诸如这样的代码在这个 com_menus.item.xml 文件中

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <form>
  3. <fields name="params">
  4. <fieldset name="extra-field" label="Extra field" description="Extra field description">
  5. <field name="masshead-title" type="text" default="" label="Masshead title" description="Masthead title" />
  6. <field name="masshead-slogan" type="textarea" default="" label="Masshead slogan" description="Masthead description" filter="RAW" />
  7. </fieldset>
  8. </fields>
  9. </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:

  1. $menuitem->params->get(extra-field-name);// $menuitem 是菜单项目对象
  1. // sample code for get extra field from active menu item
  2. $menu =JFactory::getApplication()->getMenu();
  3. $active = $menu->getActive()? $menu->getActive(): $menu->getDefault();
  4. $active->params->get('masthead-title');
  5. $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.

  1. <?php
  2. /**
  3. * @package T3 Blank
  4. * @copyright Copyright (C) 2005 - 2012. All rights reserved.
  5. * @license GNU General Public License version 2 or later; see LICENSE.txt
  6. */
  7. defined('_JEXEC')ordie;
  8. $input =JFactory::getApplication()->input;
  9. $menu =JFactory::getApplication()->getMenu();
  10. $activemenu = $menu->getActive()? $menu->getActive(): $menu->getDefault();
  11. $query = $activemenu->query;
  12. $mast_title = $mast_slogan ='';
  13. if((!isset ($query['option'])|| $query['option']== $input->get('option'))
  14. &&(!isset ($query['view'])|| $query['view']== $input->get('view'))
  15. &&(!isset ($query['id'])|| $query['id']== $input->get('id'))){
  16. $mast_title = $activemenu->params->get('masthead-title');
  17. $mast_slogan = $activemenu->params->get('masthead-slogan');
  18. }
  19. $masthead_position ='masthead';
  20. ?>
  21. <?php if($mast_title || $this->countModules($masthead_position)):?>
  22. <divclass="page-masthead">
  23. <?php if($mast_title):?>
  24. <divclass="jumbotron jumbotron-primary">
  25. <divclass="container">
  26. <h1><?php echo $mast_title ?></h1>
  27. <p><?php echo $mast_slogan ?></p>
  28. </div>
  29. </div>
  30. <?php endif ?>
  31. </div>
  32. <?php endif ?>

加载 masthead到布局中

打开布局文件(templates/t3_bs3_blank/tps/)如果你要增加这个masthead布局到模板中显示,则添加下面一行

  1. <?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模块框架。

 

 

chuangye

chuangye

chuangye

 

chuangye

Joomla的老巢