How do you add fields to com_content in Joomla! with a plugin and store the data in its own table?

↘锁芯ラ 提交于 2019-12-10 10:33:38

问题


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


回答1:


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!



来源:https://stackoverflow.com/questions/8168932/how-do-you-add-fields-to-com-content-in-joomla-with-a-plugin-and-store-the-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!