3

I have tried a few ways to create articles programmatically in Joomla 4, but none of my attempts are working.

First is to insert data by mysql using $db->getQuery(true) -- the article shows in the database, but not in the admin dashboard.

$insert = $db->getQuery(true);
$columns = array('asset_id', 'title', 'alias', 'introtext', 'fulltext', 'state', 'catid', 'created', 'created_by', 'created_by_alias',
'modified', 'modified_by', 'images', 'urls', 'attribs', 'metadata', 'metadesc', 'language');
$values = array(0, $db->quote($title), $db->quote($alias), $db->quote($introtext), $db->quote($fulltext), $db->quote($state), $db->quote($catid), $db->quote(date("Y-m-d H:i:s")), $db->quote($created_by), $db->quote($created_by_alias),
$db->quote($modified), $db->quote($modified_by), $db->quote($images), $db->quote($urls), $db->quote($attribs), $db->quote($metadata), $db->quote(""), $db->quote("*"));

$insert
    ->insert($db->quoteName('#__content'))
    ->columns($db->quoteName($columns))
    ->values(implode(',', $values));
$db->setQuery($insert);
$db->execute();

Second is using JTable, but it says error failed to start application.

$table = JTable::getInstance('Content', 'JTable', array());
$data = array(
    'catid' => 1,
    'title' => 'SOME TITLE',
    'introtext' => 'SOME TEXT',
    'fulltext' => 'SOME TEXT',
    'state' => 1,
);
if (!$table->bind($data)){
    $this->setError($table->getError());
    return false;
}
if (!$table->check()){
    $this->setError($table->getError());
    return false;
}
if (!$table->store()){
    $this->setError($table->getError());
    return false;
}

How should I create articles programmatically in Joomla 4?

1
  • If you use this method, the historic of changes of the article is updated ? Thanks
    – Seb
    2 days ago

2 Answers 2

6

I rewrote the "Content Uploader" for Joomla 4 because I couldn't reach the developer. I had exactly the same problem as you described in the first example.

The reason is that the Workflow component does not display the article if there is no entry for this article. So you have to make an entry in the table #__workflow_associations, otherwise it won't work.

So I added the following code in which I make such an entry:

// define values
$ID = $results[0]->id;
$stageID = 1;
$extenstion = "'com_content.article'";

// makes an entry so that the article is visible in the backend
$query = $this->db->getQuery(true);
            
$colums = array('item_id', 'stage_id', 'extension');
$values = array($ID, $stageID, $extenstion);

$query  
    ->insert('#__workflow_associations')
    ->columns($colums)
    ->values(implode(',', $values));

$this->db->setQuery($query);
$this->db->execute();

Here is the link to my original file on GitLab. And here to the ticket that I made on the GitHub account of Joomla.

However, it is better to use the "article model" class. Note the example: https://joomla.stackexchange.com/a/32345/16394

1
  • Ahh yes, I tried to find in many tables to look any relationship, but didn't expect it to be the __workflow_associations table :) thank you very much!! Aug 22, 2022 at 7:28
6

This will work.

$newapp = JFactory::getApplication();
$mvcFactory = $newapp->bootComponent('com_content')->getMVCFactory();
$articleModel = $mvcFactory->createModel('Article', 'Administrator', ['ignore_request' => true]);

$article = [
    'catid' => 2,
    'alias' => 'tttttttttttttttt44',
    'title' => '123My Article Title 44',
    'introtext' => 'My Article Intro Text',
    'fulltext' => 'My Article Full Text',
    'state' => 1,
    'language' => '*',
];

if (!$articleModel->save($article)){
    throw new Exception($articleModel->getError());
}
2
  • Please always try to include some explanation or supporting references with your advised snippets. "Use this" answers miss a great opportunity to educate/empower the asker and future researchers. Aug 22, 2022 at 22:28
  • How do we get the id of the newly created article? Apr 11, 2023 at 11:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.