I added some custom fields to the articles of my Joomla website: I work on a custom Joomla module that creates new articles programmatically using the below code:
static function createArticle(GNGLocation $loc, GNGLang $lang, $title, $alias, $intro, $summary, $imageStr) {
$table = JTable::getInstance('Content', 'JTable', array());
$jcat = $loc->get('jcat_id_tour');
$jlang = $lang->get('joomlaName');
$data = array(
'catid' => $jcat,
'title' => $title,
'alias' => $alias,
'language' => $jlang,
'introtext' => $summary,
'fulltext' => $intro,
'state' => 0,
);
// Bind data
if (!$table->bind($data))
{
throw new Exception("Failed to bind article data. Error: " . $table->getError());
}
// Check the data.
if (!$table->check())
{
throw new Exception("Failed to check article data. Error: " . $table->getError());
}
// Store the data.
if (!$table->store())
{
throw new Exception("Failed to store article data. Error: " . $table->getError());
}
}
This works fine, creates the new article, but I could not find a way to also write the values of the custom fields that belong to the article. This is what I tried so far, based on this post, but it does not seem to work, meaning that when I go to the article in the backend and check the custom fields they are still empty. No error message or whatsoever:
JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_fields/models');
$model =& JModelLegacy::getInstance('Article', 'ContentModel', array('ignore_request'=>true));
$appParams = JFactory::getApplication()->getParams();
$model->setState('params', $appParams);
$item =& $model->getItem($table->id);
$custom_fields = FieldsHelper::getFields('com_content.article', $item, True);
$custom_fields_by_name = \Joomla\Utilities\ArrayHelper::pivot($custom_fields, 'name');
$your_custom_field_value = 'haliho';
$model_field = JModelLegacy::getInstance('Field', 'FieldsModel', ['ignore_request' => true]);
$model_field->setFieldValue(
$custom_fields_by_name['excluded']->id,
$item->id,
$your_custom_field_value
);
UPDATE: running the following line, gives the below diagnostics:
JFactory::getApplication()->enqueueMessage("<pre>" . json_encode($custom_fields_by_name, JSON_PRETTY_PRINT). "</pre><br>" . $custom_fields_by_name['excluded']->id . "<br>" . $item->id , 'info');
Output:
info
{
"included": {
"id": "2",
"title": "Included",
"name": "included",
"checked_out": "0",
"checked_out_time": "0000-00-00 00:00:00",
"note": "",
"state": "1",
"access": "1",
"created_time": "2019-11-24 16:10:18",
"created_user_id": "919",
"ordering": "0",
"language": "*",
"fieldparams": {
"filter": "",
"maxlength": ""
},
"params": {
"hint": "",
"class": "",
"label_class": "",
"show_on": "",
"render_class": "",
"showlabel": "1",
"label_render_class": "",
"display": "0",
"layout": "",
"display_readonly": "2"
},
"type": "text",
"default_value": "",
"context": "com_content.article",
"group_id": "1",
"label": "Included",
"description": "",
"required": "0",
"language_title": null,
"language_image": null,
"editor": null,
"access_level": "Public",
"author_name": "Administrator",
"group_title": "Tour Extension",
"group_access": "1",
"group_state": "1",
"group_note": "",
"value": "",
"rawvalue": ""
},
"excluded": {
"id": "3",
"title": "Excluded",
"name": "excluded",
"checked_out": "0",
"checked_out_time": "0000-00-00 00:00:00",
"note": "",
"state": "1",
"access": "1",
"created_time": "2019-12-01 14:40:45",
"created_user_id": "919",
"ordering": "0",
"language": "*",
"fieldparams": {
"filter": "",
"maxlength": ""
},
"params": {
"hint": "",
"class": "",
"label_class": "",
"show_on": "",
"render_class": "",
"showlabel": "1",
"label_render_class": "",
"display": "0",
"layout": "",
"display_readonly": "2"
},
"type": "text",
"default_value": "",
"context": "com_content.article",
"group_id": "1",
"label": "Excluded",
"description": "",
"required": "0",
"language_title": null,
"language_image": null,
"editor": null,
"access_level": "Public",
"author_name": "Administrator",
"group_title": "Tour Extension",
"group_access": "1",
"group_state": "1",
"group_note": "",
"value": "",
"rawvalue": ""
}
}
3
332
What am I doing wrong, how should I do this?