11

My component needs to add articles from the front-end in a non-conventional way. I realize I can just insert them with MySQL, but I'd like to use the core functions to do so (if it's possible).

Having studied the code in components\com_content, I'm a little overwhelmed by everything that needs to happen, and was hoping that I'm just overcomplicating it.

Are there any examples of this in Joomla, or a convention of steps to be followed to accomplish it?

6

Open the model file and add these lines inside the model class:

public function getContentTable($type = 'Content', $prefix = 'JTable', $config = array())
{
    return JTable::getInstance($type, $prefix, $config);
}

Now you can define a method inside the model class to add an article. Something like this:

public function addArticle()
{
    $table = $this->getContentTable();
    $table->title = "Foo";
    $table->alias = "foo";
    // or
    // $table->alias = JApplication::stringURLSafe($table->title);
    $table->catid = 2;
    $table->state = 1;
    // and so on!
    // then save it
    $table->save();
}
1

I also had to load articles in an unconventional way. I was able to leverage a lot of the Joomla code for this. You made need to adjust this based on your needs.

This function will return an artlice given an id (numeric) or an alias.

    function loadArticle($id){

            $app = JFactory::getApplication();
            $db = JFactory::getDBO();
            $query = $db->getQuery(true);
            $selects = array('a.introtext','a.publish_up','a.publish_down');
            $query->select($selects);
            $query->from('#__content as a');

            // select the alias or id
            $where = 'a.title = ' . $db->q(NNText::html_entity_decoder($id));
            $where .= ' OR a.alias = ' . $db->q(NNText::html_entity_decoder($id));
            if (is_numeric($id)) {
                    $where .= ' OR a.id = ' . $id;
            }

            $query->where('(' . $where . ')');

            // check the publish and unpublish dates
            $now = JFactory::getDate('now','UTC');
            $nullDate = $db->getNullDate();

            $query->where('a.state = 1');

            $query->where('( a.publish_up = ' . $db->q($nullDate) . ' OR a.publish_up <= ' . $db->q($now) . ' )');
            $query->where('( a.publish_down = ' . $db->q($nullDate) . ' OR a.publish_down >= ' . $db->q($now) . ' )');

            $db->setQuery($query);
            $article = $db->loadObject();
            return $article;
    }
  • This looks like a good clean starting point to GET an article, but I need to ADD an article... – Al Knight Jul 29 '14 at 16:07
  • Sorry, I misunderstood – ContextSwitch Jul 29 '14 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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