24

I'm trying to add a single, simple field to each #__content item, and I've very carefully followed the tutorial at Adding Custom Fields to Core Components, substituting "content" for "contact". I now have an installable plugin that successfully includes the tab/field on the Admin Content Item editing form, and works wonderfully, except for that pesky "saving" feature.

The only reference to Saving is (again,content instead of contact) :

The additional fields are loaded from the file forms/contact.xml in the plugin directory. It's important that these fields are in a fields element with the name property set to "params". If you don't set this property name the fields will appear in the admin site but the values will not be saved.

The fact that this cautionary note exists, suggests to me that Joomla should know the fields are there, and the core code handles it, but since it's not saving, I'm wondering if there's another event I should be adding.

Here's the simple field I need - invisible to the user, but critical nonetheless, and nowhere near the need of a CCK to store it.
What is wrong/missing that is preventing the custom field from being saved?

Update: Hoping beyond hope that the custom field was stored in the database, I added a column to the #__content table with the same name and tried - no luck.

gdp.php

class plgContentGdp extends JPlugin {
    protected $autoloadLanguage = true;
    function onContentPrepareForm($form, $data) {
        $app = JFactory::getApplication();
        $option = $app->input->get('option');
        switch($option) {
            case 'com_content':
                if ($app->isAdmin()) {
                    JForm::addFormPath(__DIR__ . '/forms');
                    $form->loadFile('content', false);
                }
                return true;
        }
        return true;
    }
}

/forms/content.xml

<?xml version="1.0" encoding="UTF-8"?>
<form>
    <fields name="params" >
        <fieldset name="params" >
            <field
                name="content_myfield"
                type="text"
                label="PLG_CONTENT_GDP_MYFIELD"
                />
        </fieldset>
    </fields>
</form>

Rendered HTML (without the label)

<div class="tab-pane active" id="attrib-params">
    <div class="control-group ">
        <div class="controls">
            <input
                type="text"
                value=""
                id="jform_params_content_myfield"
                name="jform[params][content_myfield]"
                class=""
                aria-invalid="false"
                >
        </div>
    </div>
</div>
7
  • I remember seeing this in action with the field name NOT set to params and the extra field; but that's a bit ugly and will require some maintenance after each upgrade. Much as I like getting this sort of things to work, in order to get you result, have you considered using the user profile plugin approach instead? Jun 22, 2014 at 6:43
  • My data is for content, not the user...only the tutorial is for contacts
    – GDP
    Jun 22, 2014 at 12:18
  • Sorry I wasn't clear, just hook the onContentAfterSave event and handle the saving part Jun 23, 2014 at 6:35
  • Thank you so much for this. I have been trying to sort this out for the best part of a day. I am quite new to Joomla plugin development so I rely on the examples to get me going. Can I just confirm about the language string files - do these go with the plugin files or in the existing language folder? or does it not matter?
    – user1632
    Jul 11, 2014 at 8:50
  • I always put files in the same place that a Joomla extension would put them, i.e. the /languages folder in this case.
    – GDP
    Jul 11, 2014 at 13:13

1 Answer 1

20

The AHA! moment - #__content no longer has the params column, so the following adjustments have to be made for the plugin to work with components/tables with attribs instead of params (which appears only to be #__content as of v3.2):

  1. Change the fields name to "attribs"
  2. Change the fieldset name to something unique (I used the plugin name)
  3. Add a language string for the tab on the content editing page

/forms/content.xml

<form>
    <fields name="attribs" >
        <fieldset name="gdp" >
            <field
                name="myfield"
                type="text"
                label="PLG_CONTENT_GDP_MYFIELD"
                />
        </fieldset>
    </fields>
</form>

en-GB.plg_content_pass.ini

COM_CONTENT_GDP_FIELDSET_LABEL="My Custom Fields"
2
  • can i ask a question about this here? or should i create a new question? its related to this? Basically i need to know how to add more than one tab to the article edit view. It seems that the forms/extra.xml only allows you to create one tab. I tried several variations of splitting the xml file into fieldsets but no luck. Only one tab.
    – Jonnypixel
    Nov 3, 2014 at 2:45
  • You should create a new question about this, and add a link to this question/answer if related.
    – Bogowoe
    Nov 11, 2014 at 20:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.