I work on a custom Joomla 3 module that creates articles programmatically based on user input. I can create the articles, but I could not find a way to set the article options and associations when the article is created. I found this article on where the options are stored in the Joomla DB, but I would prefer not to write the DB directly, if possible. I would also need a solution to create associations for the newly created articles. For your reference, you can see my current source code below.
Thanks, W.
static function createArticle(GNGLocation $loc, GNGLang $lang, $title, $alias, $intro, $summary, $included, $excluded, $imageStr) {
$table = JTable::getInstance('Content', 'JTable', array());
$jcat = $loc->get('jcat_id_tour');
$jlang = $lang->get('joomlaName');
$intro .= "{loadposition gngscrollarticle}<BR>{loadposition gngpricingtable}<BR>{loadposition gngcontact}";
$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());
}
//write custom field values
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');
$included_value = 'haliho';
$model_field = JModelLegacy::getInstance('Field', 'FieldsModel', ['ignore_request' => true]);
$model_field->setFieldValue($custom_fields_by_name['excluded']->id, $item->id, $excluded);
$model_field->setFieldValue($custom_fields_by_name['included']->id, $item->id, $included);
return $table->id;
}