Tassos Marinos Developer of Joomla Extensions

How to Auto-Populate a Form Field with an Article Data

Published in Convert Forms
Updated 16 Nov, 2022
Heads up! This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.

You may need to populate your form fields with data from the article where the form appears. This means you can set value to your form fields from both the article data as well as the article's custom fields data. Below you can find a PHP snippet for each version.

Setup

Populate a form field with the article's title

To populate a form field with the article's title, copy the code shown below and place it into the PHP Scripts -> Form Prepare area of your form.

// Enter the name of the field to save the value
$fieldName = 'title';

// Optionally set the article's ID. Don't touch it to load current article.
$articleID = $app->input->get('id');

// Do not edit below
// Ensure article exists
$query = $db->getQuery(true);
$query->select('*')->from('#__content')->where('id=' . $db->q($articleID));
$db->setQuery($query);
if ($db->loadObject())
{
	$model = JModelLegacy::getInstance('Article', 'ContentModel');
	$model->setState('params', JFactory::getApplication()->getParams());
	$article = $model->getItem($articleID);

	$form['fields'][$fieldName]['value'] = $article->title;
}

Populate a form field with the article's author email

To populate a form field with the article's author email, copy the code shown below and place it into the PHP Scripts -> Form Prepare area of your form.

// Enter the name of the field to save the value
$fieldName = 'title';

// Optionally set the article's ID. Don't touch it to load current article.
$articleID = $app->input->get('id');

// Do not edit below
// Ensure article exists
$query = $db->getQuery(true);
$query->select('*')->from('#__content')->where('id=' . $db->q($articleID));
$db->setQuery($query);
if ($db->loadObject())
{
	$model = JModelLegacy::getInstance('Article', 'ContentModel');
	$model->setState('params', JFactory::getApplication()->getParams());
	$article = $model->getItem($articleID);

	// Load author's user data
	$author = JFactory::getUser($article->created_by);

	$form['fields'][$fieldName]['value'] = $author->email;
}

Populate a form field with an article's custom field value

To populate a form field with a custom field value of an article, copy the code shown below and place it into the PHP Scripts -> Form Prepare area of your form.

// Enter the Custom Field Name (not Label) to find its value
$customFieldName = 'name';

// Enter the Hidden Field Name here to save the value
$hiddenFieldName = 'myHiddenFieldName';

// Optionally set the article's ID. Don't touch it to load current article.
$articleID = $app->input->get('id');

// Do not edit below
// Ensure article exists
$query = $db->getQuery(true);
$query->select('*')->from('#__content')->where('id=' . $db->q($articleID));
$db->setQuery($query);
if ($db->loadObject())
{
	$model = JModelLegacy::getInstance('Article', 'ContentModel');
	$model->setState('params', JFactory::getApplication()->getParams());
	$article = $model->getItem($articleID);

	if (defined('nrJ4'))
	{
		$jcfields = Joomla\Component\Fields\Administrator\Helper\FieldsHelper::getFields('com_content.article', $article, true);
	}
	else
	{
		$jcfields = FieldsHelper::getFields('com_content.article', $article, true);
	}

	foreach($jcfields as $jcfield)
	{
		if ($jcfield->name != $customFieldName)
		{
			continue;
		}

		$form['fields'][$hiddenFieldName]['value'] = $jcfield->rawvalue;
		break;
	}
}