我使用Joomla Component Builder来快速创建一些小组件。现在我创建简单的目录组件,是时候添加类别了,因为所有其他想法似乎都工作得很好,但有一个问题。
类别的所有代码都创建得很好,我可以添加新类别并将其保存在数据库中,但当我编辑目录项时没有看到任何此类猫。 我尝试找出问题所在,并通过将 catid 添加到列表模式下的某些项目和类别显示来简单地对数据库进行更改,但在编辑模式下组合框仍然只有根元素。
我检查 \models orms\item.xml 文件并找到字段描述:
<!-- Catid Field. Type: Category. (joomla) -->
<field
type="category"
name="catid"
label="COM_SKYCATALOG_ITEM_CATID_LABEL"
extension="com_skycatalog.list"
required="true"
show_root="true"
description="COM_SKYCATALOG_ITEM_CATID_DESCRIPTION"
published="true"
/>
看起来一切都好。
你确定
com_skycatalog.list
是正确的吗?检查 #__categories
表以确保您使用正确的上下文。
您尝试过类别编辑吗?
<field name="catid"
type="categoryedit"
extension="__EXTENSION__"
label="JCATEGORY"
required="true"
default=""
/>
奇怪的是,标准方法不起作用,但我以不同的方式管理它。我只是添加自定义字段:
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
JFormHelper::loadFieldClass('list');
/**
* skycatalog Form Field class for the skycatalog component
*
* @since 0.0.1
*/
class JFormFieldSkyCatalog extends JFormFieldList
{
/**
* The field type.
*
* @var string
*/
protected $type = 'skycatalog';
/**
* Method to get a list of options for a list input.
*
* @return array An array of JHtml options.
*/
protected function getOptions()
{
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('id, title');
$query->from('#__categories');
// Retrieve only published items
$query->where('#__categories.published = 1','and');
$query->where("#__categories.extension like 'com_skycatalog.list'",'and');
$db->setQuery((string) $query);
$messages = $db->loadObjectList();
$options = array();
if ($messages)
{
foreach ($messages as $message)
{
$options[] = JHtml::_('select.option', $message->id, $message->title);
}
}
$options = array_merge(parent::getOptions(), $options);
return $options;
}
}
并更改字段类型:
<field
type="Skycatalog"
name="catid"
class="inputbox"
label="COM_SKYCATALOG_ITEM_CATID_LABEL"
extension="com_skycatalog"
required="true"
description="COM_SKYCATALOG_ITEM_CATID_DESCRIPTION"
published="true"
/>
现在效果很好。 嗯,还有很多需要改进的地方,例如,为类别添加树形填充等。