Contao Open Source CMS > Understand > Developer's guide > Customizing Contao > How to add custom fields to a Data Container Array

Adding custom fields

Let us assume that you want to add a customer number to the members table. Adding a custom field to a Contao table requires to change more than just one file, therefore it is recommended to create a custom module in the system/modules directory. Remember that modules are loaded in alphabetical order, so do not name your extension custom if you want to override settings of the news extension.

Extending the database

The database configuration is stored in the config/database.sql files of the various Contao modules. The SQL files are not sent to the database but used to calculate the difference between the Contao specifications and the actual database tables. Therefore, you can also alter fields defined by another module in the database.sql file. Add the following code to create the new field:

CREATE TABLE `tl_member`(
  `customer_number` varchar(8) NOT NULL default ''
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Use the Contao install tool to update your database tables.

Extending the DCA

Create the file dca/tl_member.php in your module folder and add the meta data for the new field so Contao knows how to handle it.

// Modify the palette
$GLOBALS['TL_DCA']['tl_member']['palettes']['default'] = str_replace
(
    'company',
    'customer_number,company',
    $GLOBALS['TL_DCA']['tl_member']['palettes']['default']
);

// Add the field meta data
$GLOBALS['TL_DCA']['tl_member']['fields']['customer_number'] = array
(
    'label'     => &$GLOBALS['TL_LANG']['tl_member']['customer_number'],
    'exclude'   => true,
    'inputType' => 'text',
    'eval'      => array('mandatory'=>true, 'rgxp'=>'digit', 'maxlength'=>8)
);

If you do not understand the code above, you might want to read the chapter on Data Container Arrays.

Adding a translation

Create the file languages/en/tl_member.php in your module folder and add the English labels for the new field:

$GLOBALS['TL_LANG']['tl_member']['customer_number'] = array
(
    'Customer number',
    'Please enter the 8-digit customer number.'
);

Now you can enter a customer number in the members module which can contain up to 8 digits. If the field is left blank or contains any non-digit characters, Contao will not save the value and show an error message instead.

Add a comment

Comment by Custom Theses Writing |

Many institutions limit access to their online information. Making this information available will be an asset to all.

Comment by zag |

The code in Extending the DCA needs to have the <?php and ?> parts added. It may be obvious to php-people but it took a while for me to work out what was missing.

<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');

// Modify the palette
$GLOBALS['TL_DCA']['tl_member']['palettes']['default'] = str_replace
(
    'company',
    'customer_number,company',
    $GLOBALS['TL_DCA']['tl_member']['palettes']['default']
);

// Add the field meta data
$GLOBALS['TL_DCA']['tl_member']['fields']['customer_number'] = array
(
    'label'     => &$GLOBALS['TL_LANG']['tl_member']['customer_number'],
    'exclude'   => true,
    'inputType' => 'text',
    'eval'      => array('mandatory'=>true, 'rgxp'=>'digit', 'maxlength'=>8)
);

?>