4

Sometimes, we need to customize the structure of Joomla! articles with custom fields in order to propose to users different textarea / input where he can enter datas such as date, location, text 1, text 2, etc.

Then, it is easier to deal with design in front end. With CSS rules, each content is correctly designed.

I know i can do that with CCK component such as K2, Seblod, etc.

Is there an other way without component, with php and adding some modifications to Joomla ?

3 Answers 3

10

I believe I have your solution, but do NOT hack the core. We felt forced to do that over the years, and we're still paying the price even today, missing updates, revisiting hacks everytime we do update, and general incompatibility with other components.

What we now do is this:

  1. New table #__content_ext with a key field that is identical to #__content and a column for each custom field you want.
  2. New table #__categories_ext with a key field that is identical to #__categories and a column for each custom field you may want.
  3. Add custom fields to com_content (we use and love Aixeena CCK which can add custom fields to content, categories, and menus).

At this point, you're leveraging all the code/logic in com_content without a single core hack. However, your custom fields are all being stored in the params and attribs columns of the Joomla tables, which is not ideal for typical database use in other custom code you may need. This is where the "magic" comes in:

Write a simple content plugin using the below sample code to create/update the table extensions described above. This puts all your custom fields into custom tables which are available for whatever purpose you need them for in SQL.

public function onContentAfterSave($context=null, $article=null, $isNew=0) {
    if($context == 'com_categories.category') {
        $params = new JRegistry;
        $params->loadString($article->params);
        $myObj = $params->toObject();
        // Use properties from $article and $myObj to create/update #__categories_ext
        // Use $article->id as the primary key if #__categories_ext
        // Your SQL goes here....

    }elseif($context=='com_content.article'){
        $attribs = new JRegistry;
        $attribs->loadString($article->attribs);
        $myObj = $attribs->toObject();
        // Use properties from $article and $myObj to create/update #__content_ext
        // Use $article->id as the primary key if #__content_ext
        // Your SQL goes here....

    }
}

Admittedly, the data is being stored twice - once as custom content, and again in your custom table, but bending that normalization rule MORE than pays for the work and grief that a core hack costs if you intend on keeping the site current to Joomla versions. You've also left things in such a state you can use all the com_content and other extensions that use com_content without any consequence of your hacks.

2
  • Really nice answers. I need to keep Joomla! up to date for security reasons. I try your solution and i tell you. Aug 13, 2014 at 13:42
  • Aixeena CCK is a really nice and simple solution that suits to my needs. Thank you for the explanation on how to store the data in custom tables. Aug 17, 2014 at 10:03
3

I needed to do this recently as well and I followed a few online examples.

There are a few good examples including the Joomla docs here

Another nice tutorial can be found here - Add custom content fields to Joomla

I realise you don't want to use a CCK however you should have a look at Fields Attach which is very good, and simple

Hope these help.

0
2

Apart of extending the core, there are a few extensions that allows extending joomla's content with custom fields.

Otherwise, the suggestions and tutorials provided in the other answers will "keep you going".

1
  • Thank you for the links. I’ve tried the 3 components / plugins. They are all very good. I like Aixeena CCK for its simplicity. Aug 17, 2014 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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