How to save Custom Fields in Core components

21

7

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>

GDP

Posted 2014-06-21T17:21:35.517

Reputation: 5 737

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? – Riccardo Zorn – 2014-06-22T06:43:01.840

My data is for content, not the user...only the tutorial is for contacts – GDP – 2014-06-22T12:18:35.723

Sorry I wasn't clear, just hook the onContentAfterSave event and handle the saving part – Riccardo Zorn – 2014-06-23T06:35:18.133

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? – None – 2014-07-11T08:50:31.670

I always put files in the same place that a Joomla extension would put them, i.e. the /languages folder in this case. – GDP – 2014-07-11T13:13:24.183

using attribs and a unique name for the fieldset does not work for me. in fact, i don't even get a separate tab for my options. My custom fields are located at the bottom of the "Options" tab. – None – 2014-08-03T01:12:51.927

@F.J. Feel free to create a new question if you are having problems and want some help. – David Fritsch – 2014-08-05T18:30:56.777

Answers

19

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"

GDP

Posted 2014-06-21T17:21:35.517

Reputation: 5 737

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 – 2014-11-03T02:45:43.413

You should create a new question about this, and add a link to this question/answer if related. – Bogowoe – 2014-11-11T20:39:36.330