fields.php

File structure

This file contains custom fields to be used in your plugin. It does not contain field names (those are stored in language files).

$fields = array(
	0 => array( // section ID
		'title' => array( // unique field keyword
			'type' => 'text', // text field type
			'required' => 1, // make it required
			'system' => 1, // system field indicates that user cannot change its type or keyword
			'multilang' => 0, // multilingual option should be enable if you want to fill in values for each language on your site
			'config' => array( // configuration data for this field
				'in_view' => 1, // display when viewing article
				'in_account' => 1, // display when editing article
				'in_list' => 1, // display when browsing articles
				'min_length' => 0,
				'max_length' => 255,
				'in_search' => 0 // do not display in the search form
			)
		),
		'category' => array(
			'type' => 'select', // drop down box
			'required' => 1,
			'system' => 0,
			'multilang' => 0,
			'items' => 3, // number of items in this drop down field, actual items are included in the language file instead
			'config' => array(
				'in_view' => 1,
				'in_account' => 1,
				'in_list' => 1,
				'min_length' => 0,
				'max_length' => 255,
				'in_search' => 0
			)
		),
		'category2' => array(
			'type' => 'checkbox',
			'required' => 0,
			'system' => 0,
			'multilang' => 0,
			'items' => 3, // number of items in this check box field, actual items are included in the language file instead
			'config' => array(
				'in_view' => 1,
				'in_account' => 1,
				'in_list' => 1,
				'in_search' => 0,
				'columns_number' => 2 // display check boxes in 2 columns
			)
		),
		'body' => array(
			'type' => 'textarea',
			'required' => 1,
			'system' => 1,
			'multilang' => 0,
			'config' => array(
				'in_view' => 1,
				'in_account' => 1,
				'in_list' => 1,
				'min_length' => 10,
				'max_length' => 65535,
				'in_search' => 0
			)
		)
	)
);

Plugin may contain more than one set of custom fields, in which case you would create a new section with the ID of 1, 2, etc. The above example users only 1 section with the default ID of 0.

Custom fields are saved into the "core_fields" and "core_fields_items" (for drop down or check box items) tables in the database.

Custom field types

Fields can be of different types and include the following:

  • number - basic number value
  • dimensions - width and height values used for images
  • country - drop down box with countries
  • location - geo location including country, state and city
  • birthday - birthday field
  • date - date field
  • date_time - date and time field
  • text - any text value
  • textarea - any text value in a text area box
  • boolean - yes/no value
  • select - drop down box
  • radio - radio check boxes
  • checkbox - check boxes

Setting types including "select", "radio" and "checkbox" must include an additional parameter called "items" where you must list items for user to select from (see "rating" setting above).

Accessing custom fields

You may fetch custom fields from the database like this:

// 'articles' is the name of the plugin
// 0 is the section ID
// 'view' indicates that we're going to view custom fields
// 'in_list' indicates that we need only custom fields that are enable when browsing articles
$fields = $this->fields_model->getFields('articles', 0, 'view', 'in_list');

// 'articles' is the name of the plugin
// 0 is the section ID
// 'edit' indicates that we're going to edit custom fields
// 'in_account' indicates that we need only custom fields that are enable when editing article
$fields = $this->fields_model->getFields('articles', 0, 'edit', 'in_account');

Language file

Language file "languages/english.php" will contain code like this:

$fields = array(
	0 => array( // section ID
		'title' => array( // field keyword
			'name' => "Title", // field name
			'vname' => "", // optional field name (when viewing article)
			'sname' => "", // optional field name (on the search form)
			'validate_error' => "" // optional regular expression error message
		),
		'category' => array(
			'name' => "Category",
			'vname' => "",
			'sname' => "",
			'validate_error' => "",
			'items' => array( // since we have 3 items indicated in the fields.php file, we must include them here
				array(
					'name' => "Arts and culture",
					'sname' => "" // item name (on the search form)
				),
				array(
					'name' => "Business",
					'sname' => "" // item name (on the search form)
				),
				array(
					'name' => "Cars",
					'sname' => "" // item name (on the search form)
				)
			)
		),
		'category2' => array(
			'name' => "Another category",
			'vname' => "",
			'sname' => "",
			'validate_error' => "",
			'items' => array( // since we have 3 items indicated in the fields.php file, we must include them here
				array(
					'name' => "Business",
					'sname' => ""
				),
				array(
					'name' => "Leisure",
					'sname' => ""
				),
				array(
					'name' => "Sports",
					'sname' => ""
				)
			)
		),
		'body' => array(
			'name' => "Article",
			'vname' => "",
			'sname' => "",
			'validate_error' => ""
		)
	)
);