本文介绍了您如何在 Joomla 中向 com_content 添加字段!使用插件并将数据存储在自己的表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行 Joomla 1.7,我知道它能够使用插件将自定义表单字段添加到组件中.

I'm running Joomla 1.7 and I know that it has the ability to add custom form fields to components with a plugin.

有一个示例插件位于:/plugins/user/profile

There is a sample plugin located at:/plugins/user/profile

此插件允许您在用户配置文件前端和后端放置自定义表单字段,这些字段存储在自定义表中.

This plugin allows you to put custom form fields on the user profile front end and back end and these fields are stored in a custom table.

我为用户配置文件创建了一个类似的插件,并且效果很好.

I created a similar plugin for user profiles and it worked perfectly.

然而,当我为 com_content 创建一个这样的插件时,我遇到了一个问题.

However, when I go to create a plugin like this for com_content, I am met with a problem.

这是我的 XML 文件的样子:

this is what my XML file looks like:

<?xml version="1.0" encoding="utf-8"?>
  <form>
   <fields name="additionalinfo">
    <fieldset name="additionalinfo_fieldset" label="PLG_CONTENT_ADDITIONALINFO_FIELDSET_LABEL">
        <field name="tagline" type="text" size="50" default="" label="PLG_CONTENT_ADDITIONALINFO_TAGLINE_LABEL" description="PLG_CONTENT_ADDITIONALINFO_TAGLINE_DESC" />
        <field name="pseudocategory" type="category" extension="com_content" label="PLG_CONTENT_ADDITIONALINFO_PSEUDOCATEGORY_FIELD_LABEL" description="PLG_CONTENT_ADDITIONALINFO_PSEUDOCATEGORY_FIELD_DESC" />
    </fieldset>
  </fields>
</form>

然而这不起作用,每当我做类似上面的事情时,表单字段永远不会显示在管理表单上(即使我正确设置了它,并且用户插件和内容插件之间唯一改变的是我希望表单出现的表单名称

This however does not work, whenever I do something like above, the form fields never show up on the admin form (even though I have it set correctly, and the only thing that changed between the user plugin and the content plugin is the name of the form i'd like the form to appear on

当我将 XML 更改为此时:

When I change my XML to this:

<?xml version="1.0" encoding="utf-8"?>
  <form>
   <fields name="attribs">
    <fieldset name="additionalinfo_fieldset" label="PLG_CONTENT_ADDITIONALINFO_FIELDSET_LABEL">
        <field name="tagline" type="text" size="50" default="" label="PLG_CONTENT_ADDITIONALINFO_TAGLINE_LABEL" description="PLG_CONTENT_ADDITIONALINFO_TAGLINE_DESC" />
        <field name="pseudocategory" type="category" extension="com_content" label="PLG_CONTENT_ADDITIONALINFO_PSEUDOCATEGORY_FIELD_LABEL" description="PLG_CONTENT_ADDITIONALINFO_PSEUDOCATEGORY_FIELD_DESC" />
    </fieldset>
  </fields>
</form>

当我进行这个简单的更改时,表单域出现了!但是,数据不会从自定义表中存储或检索,它只是进入 _content 表上的属性"列.这将内容存储在 JSON 中,这没问题,但我们希望能够通过自定义字段索引内容(并且不必遍历数据库中的每条记录来查找我们要查找的内容).

When I make this simple change, the form fields show up! BUT, the data isn't stored or retrieved from the custom table, it just goes into the 'attribs' column on the _content table. This stores the content in JSON, which is alright, but we'd like to be able to index the content by the custom fields (and not have to loop through each record in the database to find what we're looking for).

关于如何解决这个问题有什么想法吗?

Any ideas on how to fix this?

谢谢!

大卫·巴拉特

推荐答案

我猜您的插件文件(例如,yourplugin.php")将有一个名为onContentPrepareForm"的方法.如果你想给一篇文章添加数据,这个方法应该是这样开始的:

I guess your plugin file ( for example, "yourplugin.php" ) will have one method called "onContentPrepareForm". If you want to add data to an article, this method should start like this:

function onContentPrepareForm($form, $data)
{

    if (!($form instanceof JForm))
    {
        $this->_subject->setError('JERROR_NOT_A_FORM');
        return false;
    }

    // Check we're manipulating an
    if ( $form->getName() != "com_content.article" ) {
        return true;
    }
    //[...] The rest of your code here

此外,如果您想将这些字段存储在另一个表中以便更容易使用这些字段进行搜索,也许您应该创建一个新表并使用onContentAfterSave"方法保存数据:

Besides, if you want to store these fields in another table in order to make it easier to search using this fields, maybe you should create a new table and save the data using the "onContentAfterSave" method:

public function onContentAfterSave( $context, &$article, $isNew )

在这个方法上,你应该经常检查 $context 是com_content.article",否则你在保存类别时可能会遇到问题.

On this method, you should always check that $context is "com_content.article", otherwise you might face problems when saving categories.

希望能帮到你!

这篇关于您如何在 Joomla 中向 com_content 添加字段!使用插件并将数据存储在自己的表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 04:03