Create a Joomla! Article Programmatically

牧云@^-^@ 提交于 2020-01-09 07:06:10

问题


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

I found this on stack overflow Programmatically adding an article to Joomla which explains how to do it. The code makes sense, and looks like it will work. The problem is that once methods start being called that are contained in com_content, all the relative URLs in com_content break down and joomla throws an error.

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


回答1:


It's not possible to change the working directory because its a constant. To work around this issue you could choose not to use ContentModelArticle at all and instead use the table class only:

$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;
}

Note that the code above does not trigger the before/after save events. If that is needed however, it should not be a problem to trigger those events. Also worth noticing is that the field published_up will not be automatically set and the articles within the category will not be reordered.

To reorder the category:

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



回答2:


The error I get says:

File not found /var/www/administrator/com_mynewcomponent/helpers/content.php

I got around the problem by creating an empty file at this location to suppress the error message and manually including /var/www/administrator/com_content/helpers/content.php with a require_once statement.




回答3:


Support Joomla 2.5 and Joomla 3.0

JTableContent is not autoloaded prior to Joomla! version 3.0, so it needs to 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's 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;
}


来源:https://stackoverflow.com/questions/12643725/create-a-joomla-article-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!