如何在 Joomla! 中向 com_content 添加字段使用插件并将数据存储在自己的表中?

发布于 2024-12-15 21:53:41 字数 1896 浏览 7 评论 0原文

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

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

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

我为用户配置文件创建了一个类似的插件,它运行得很好。

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

这就是我的 XML 文件的样子:

<?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>

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

当我将 XML 更改为以下内容时:

<?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 表上的“attribs”列将内容存储在 JSON 中,这没问题,但我们希望能够通过自定义字段对内容进行索引(而不必循环遍历数据库中的每个记录来查找)我们正在寻找什么)。

关于如何解决这个问题的任何想法,

谢谢

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

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.

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

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

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>

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?

thanks!

david barratt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖伴 2024-12-22 21:53:41

我猜你的插件文件(例如“yourplugin.php”)将有一种名为“onContentPrepareForm”的方法。如果你想向文章添加数据,这个方法应该这样开始:

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”方法保存数据:

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

在此方法中,您应该始终检查 $context 是否为“com_content.article”,否则在保存类别时可能会遇到问题。

我希望它有帮助!

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

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 )

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

I hope it helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文