Create Joomla! Article programmatically

I created my own component. When I add a new entry to my component, I also want it to create a new article in joomla (i.e. using com_content).

I found this when stack overflow Programmatically adding an article to Joomla that explains how to do this. The code makes sense and looks as if it will work. The problem is that as soon as the methods that are called in com_content start to be called, all relative URLs in com_content break and joomla throws an error.

Does anyone know how to overcome this? The comment from the link above suggests that changing the current working directory to com_content will work before turning it on, but I'm not 100% sure how to do this.

+2
source share
4 answers

It is not possible to change the working directory because its constant. To work around this problem, you may not use the ContentModelArticle at all and use only the table class instead:

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

Please note that the above code does not fire events before and after saving. However, if necessary, it should not be a problem to trigger these events. It is also worth noting that the published_up field will not be automatically set, and articles in this category will not be reordered.

To reorder categories:

  $table->reorder('catid = '.(int) $table->catid.' AND state >= 0'); 
+14
source

The error I get says:

File not found / var / www / administrator / com _mynewcomponent / helpers / content.php

I had a problem creating an empty file in this place to suppress the error message and manually including /var/www/administrator/com_content/helpers/content.php in the require_once statement.

+1
source
 $table = JTable::getInstance('Content', 'JTable', array()); 

didn't work for me in joomla 2.5 but

 $table = JTable::getInstance('content', 'JTable'); 

have worked.

(This is probably a comment on the accepted answer, but I can not leave comments)

+1
source

Joomla 2.5 and Joomla 3.0 support

JTableContent does not load automatically until Joomla! version 3.0, so it should be included:

 if (version_compare(JVERSION, '3.0', 'lt')) { JTable::addIncludePath(JPATH_PLATFORM . 'joomla/database/table'); } $article = JTable::getInstance('content'); $article->title = 'This is my super cool title!'; $article->alias = JFilterOutput::stringURLSafe('This is my super cool title!'); $article->introtext = '<p>This is my super cool article!</p>'; $article->catid = 9; $article->created = JFactory::getDate()->toSQL(); $article->created_by_alias = 'Super User'; $article->state = 1; $article->access = 1; $article->metadata = '{"page_title":"","author":"","robots":""}'; $article->language = '*'; // Check to make sure our data is valid, raise notice if it not. if (!$article->check()) { JError::raiseNotice(500, $article->getError()); return FALSE; } // Now store the article, raise notice if it doesn't get stored. if (!$article->store(TRUE)) { JError::raiseNotice(500, $article->getError()); return FALSE; } 
+1
source

All Articles