2
votes

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
Just curious, is there a reason you are not using a CCK for this? This is pretty much exactly the point of a CCK.Brent Friar
@BrentFriar Most of the CCK's for Joomla! that i've seen are terrible. For instance, Zoo stores all of the data in XML in a single database field. This is terrible for indexing. Also, the CCKs are usually poorly integrated with the rest of Joomla! so the search does not respond accordingly. Also, existing modules designed for com_content will not pull in contentment from CCK's so any modules that could have been used no longer can be. However, I do agree that Joomla! needs an integrated CCK of some sort. However, storing fields in separate tables is somewhat close.David Barratt
I am afraid that putting extra fields in a single database field is the way you will find it in most instances. It's not really a problem for indexing or search at least for the large CCKs because there are plugins that handle those tasks, as it should be. You will also find that most of the big developers also support some of the various CCKs. If you have a module that does not, it is trivial to change the SQL statements to work with a CCK. All of this is well worth not having to write a rather complex work around. you should seriously try some CCKs before committing to writing your own.Brent Friar
Oh yeah, I should have mentioned that there are a couple of CCKs that extend com_content. I don't know the status of them updating to 1.7 other than Seblod, but that one is supposed to be very powerful and compatible with all com_content compatible modules. seblod.comBrent Friar

1 Answers

0
votes

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!