I have now managed to almost make this work. The code I run in the auto action is:
Code:
$newapp = Joomla\CMS\Factory::getApplication();
$mvcFactory = $newapp->bootComponent('com_content')->getMVCFactory();
$articleModel = $mvcFactory->createModel('Article', 'Administrator', array('ignore_request' => true));
$article = [
'catid' => 8,
'com_fields' => array('cvr' => '12345678', 'adresse' => 'Testadresse', 'postnr' => '8000', 'by' => 'Aarhus C', 'telefon' => '33333333', 'webshop-url' => 'https://xxxxxx.dk'),
'alias' => '',
'title' => '[username]',
'introtext' => 'ee',
'fulltext' => '[cb_description]',
'created_by' => $user->id,
'state' => 1,
'attribs' => '{"article_layout":"","show_title":"","link_titles":"","show_tags":"","show_intro":"","info_block_position":"","info_block_show_title":"","show_category":"","link_category":"","show_parent_category":"","link_parent_category":"","show_associations":"","show_author":"","link_author":"","show_create_date":"","show_modify_date":"","show_publish_date":"","show_item_navigation":"","show_icons":"","show_print_icon":"","show_email_icon":"","show_vote":"","show_hits":"","show_noauth":"","urls_position":"","alternative_readmore":"","article_page_title":"","show_publishing_options":"","show_article_options":"","show_urls_images_backend":"","show_urls_images_frontend":""}',
'metadata' => '{"robots":"","author":"","rights":"","xreference":""}',
'language' => '*'
];
// Make sure that the article gets a unique alias before saving
if ($article['alias'] == null || empty($article['alias']))
{
if (Joomla\CMS\Factory::getConfig()->get('unicodeslugs') == 1)
{
$article['alias'] = JFilterOutput::stringURLUnicodeSlug($article['title']);
}
else
{
$article['alias'] = JFilterOutput::stringURLSafe($article['title']);
}
$table = JTable::getInstance('Content', 'JTable');
while ($table->load(array('alias' => $article['alias'], 'catid' => $article['catid'])))
{
$article['alias'] = Joomla\String\StringHelper::increment($article['alias'], 'dash');
}
}
if (!$articleModel->save($article)){
throw new Exception($articleModel->getError());
}
The article is created just fine, but the custom field values are not entered - UNLESS I change the permissions "Edit custom value" on the custom field group to Public !
How can I make teh Auto action - or the batch run as the registered user (or the super user) ?