我正在尝试在joomla的前端新文章中添加新字段,如下所述:http://docs.joomla.org/Adding_custom_fields_to_the_article_component
但字段未显示在表单上。任何人都能解释一下原因吗?
以下是我在插件文件中编写的代码:
function onContentPrepareForm($form, $data)
{
if (!($form instanceof JForm))
{
$this->_subject->setError('JERROR_NOT_A_FORM');
return false;
}
// Add the extra fields to the form.
// need a seperate directory for the installer not to consider the XML a package when "discovering"
JForm::addFormPath(dirname(__FILE__) . '/rating');
$form->loadFile('rating', false);
return true;
}
我观察到的一件事是,在组件内部的com_content文件夹中,编写了固定代码,这就是为什么我的字段不可见的原因。如果我更改文件是否可以:\ components \ com_content \ views \ form \ tmpl
答案 0 :(得分:2)
我发现教程中还有另一个错误,至少我的Joomla版本是2.5.14。
在您的/rating/rating.xml(表单说明,而不是清单)中,您需要更改“
<fields name="rating">
到
<fields name="attribs">
使用“attribs”名称以使插件正常工作有一些特别之处。
答案 1 :(得分:0)
根据上面评论中的对话,我认为你的教程很差。那么文档永远不会说明这一点,你实际上必须将这个函数包装在一个类中,以便CMS正确地调用该函数。
即使没有类包装器,该文件也会被加载到CMS中,因此如果在函数外部放置die
或exit
语句,您应该看到它有效。如果你把它放在函数中,它就不会被调用,因为你的函数永远不会被调用。
您可以从教程底部或此处参考的教程中下载完整插件:http://joomlacode.org/gf/download/trackeritem/28771/75013/plg_content_rating-2.5.0.zip。如果你看一下这个,你会看到实际运作所需的额外部分。我将它包括在下面。
任何其他功能也必须在课堂上进行。此外,如果您更改了插件的名称,则还需要更新类名。这可能非常棘手,因为命名约定非常严格,并且要求插件名称(rating
)和插件组(content
)都是正确的。
代码:
class plgContentRating extends JPlugin
{
function onContentPrepareForm($form, $data)
{
if (!($form instanceof JForm))
{
$this->_subject->setError('JERROR_NOT_A_FORM');
return false;
}
// Add the extra fields to the form.
// need a seperate directory for the installer not to consider the XML a package when "discovering"
JForm::addFormPath(dirname(__FILE__) . '/rating');
if (!$form->loadFile('rating', false)) {
die('No Form');
}
return true;
}
}
*更新:好的,所以这不仅仅是缺少的课程,而是我要离开,以防它帮助其他人。
我更新了上面的代码来检查loadFile以查看它是否正常工作。