How to Include Custom Fields in Joomla

You can have all the custom fields corresponding to the current item or current user. It can be accessible through a new property in $item variable called jcfields. The $item->jcfields property is an array that holds the data per field.

Step 1: Load the FieldsHelper

The first step is to load the fields helper function file.

// Load the FieldsHelper
<?php JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php'); ?>

Step 2: Render the Fields

To render the field you can use FieldsHelper::render() by passing the needed values. As $item->jcfields property is an array that stores all the fields for the current item, you can use foreach loop.

foreach ($this->item->jcfields as $field)
{
// Render the field using the fields render method
echo FieldsHelper::render($field->context, 'field.render', array('field' => $field));
}

Code for a raw override

foreach ($this->item->jcfields as $field)
{
// Render the field using the fields render method
echo $field->label . ':' . $field->value;
}

How to Display Custom Fields from Article in Module

You can use FieldsHelper::getFields() which gets the array of custom fields.

JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
JModelLegacy::addIncludePath(JPATH_SITE. '/components/com_content/models', 'ContentModel');

$id = JFactory::getApplication()->input->get('id');

$model = JModelLegacy::getInstance('Article', 'ContentModel', array('ignore_request'=>true));
$appParams = JFactory::getApplication()->getParams();
$model->setState('params', $appParams);
$item = $model->getItem($id);
$jcFields = FieldsHelper::getFields('com_content.article', $item, true);

foreach($jcFields as $jcField)
{
$jcField->label = $jcField->value;
}

How to Display User Custom Field

For Users Component, you can use:

JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');

$context = 'com_users.user';
$user = JFactory::getUser();
$fields = FieldsHelper::getFields($context, $user, true);

The $fields is an indexed object. To get array with field name:

$custom = array();

foreach ($fields as $field)
{
$custom[$field->name] = array('id' => $field->id, 'name' => $field->name, 'label' => $field->label, 'value' => $field->value);
}

Access Custom Field Directly

If you know the field ID and item ID (or user ID), you can directly access the field value or set the field value using the model.

1. To Get Field Value

JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_fields/models', 'FieldsModel');
$fieldModel = JModelLegacy::getInstance('Field', 'FieldsModel', array('ignore_request' => true));
$value = $fieldModel->getFieldValue($field_id, $item_id);

2. To Set Field Value

JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_fields/models', 'FieldsModel');
$fieldModel = JModelLegacy::getInstance('Field', 'FieldsModel', array('ignore_request' => true));
$fieldModel->setFieldValue($field_id, $item_id, $value);

Item ID can be the ID of the article or user, as the case be.