在 Joomla 3 中向组件添加类别
Adding category to component in Joomla 3
我使用 Joomla Component Builder 来快速创建一些小组件。现在我创建了简单的目录组件,是时候添加类别了,因为所有其他想法似乎工作正常,但有一个问题。
类别的所有代码都创建得很好,我可以添加新类别并将其保存在数据库中,但是当我编辑目录项时没有看到任何这只猫。
我试图找出问题所在,并通过将 catid 添加到列表模式下的某些项目和类别显示来简单地对数据库进行更改,但在编辑模式下组合框仍然只有根元素。
我检查 \models\forms\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
table 以确保您使用的是正确的上下文。
您尝试过 categoryedit 吗?
<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"
/>
现在一切正常。
嗯,还有很多需要改进的地方,例如,在类别中添加像填充一样的树等
我使用 Joomla Component Builder 来快速创建一些小组件。现在我创建了简单的目录组件,是时候添加类别了,因为所有其他想法似乎工作正常,但有一个问题。
类别的所有代码都创建得很好,我可以添加新类别并将其保存在数据库中,但是当我编辑目录项时没有看到任何这只猫。 我试图找出问题所在,并通过将 catid 添加到列表模式下的某些项目和类别显示来简单地对数据库进行更改,但在编辑模式下组合框仍然只有根元素。
我检查 \models\forms\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
table 以确保您使用的是正确的上下文。
您尝试过 categoryedit 吗?
<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"
/>
现在一切正常。 嗯,还有很多需要改进的地方,例如,在类别中添加像填充一样的树等