How to create new com_content article via PHP?

179 views
Skip to first unread message

Ruud van Lent

unread,
Sep 27, 2018, 11:17:29 AM9/27/18
to Joomla! General Development
Hi,
been searching on what is the best practice for creating programmatically new articles in com_content.
Found solutions ranging from directly saving to the database to creating a $data array manually and saving that via the article model.

But what is the preferred and 'supported' way of doing this?

thanks in advance!

regards,
Ruud.

Allon Moritz

unread,
Sep 28, 2018, 7:34:00 AM9/28/18
to joomla-de...@googlegroups.com
I would always go through the model.

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send email to joomla-de...@googlegroups.com.
Visit this group at https://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/d/optout.

Ruud van Lent

unread,
Sep 28, 2018, 8:46:25 AM9/28/18
to Joomla! General Development
Hi, Thanks for the follow up. I am trying to follow the model route, not sure if this can be improved?

        $contentModel = BaseDatabaseModel::getInstance('Article', 'ContentModel', array('ignore_request' => true));

       
// Create $article array from object and remove '_errors' from array
        $article
= get_object_vars($contentModel->getItem());
        unset
($article['_errors']);


This results in an $article array

        $article['title'] = $title;
        $article
['articletext'] = $content;
        $article
['catid'] = (int) $categoryID;
        $article
['created_by'] = $author;
        $article
['state'] = (int) $publish;

       
// Set Article default values
        $article
['language'] = '*';
        $article
['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":""}';
        $article
['metadata'] = '{"robots":"","author":"","rights":"","xreference":""}';

       
// Make sure that the article gets a unique alias before saving
       
if ($article['alias'] == null || empty($article['alias']))
       
{
           
if (JFactory::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'] = StringHelper::increment($article['alias'], 'dash');
           
}
       
}

Adding the minimum required data to it

        $result = $contentModel->save($article);

Saving the $article data via the model.

Reply all
Reply to author
Forward
0 new messages