Take the 2-minute tour ×
Joomla Stack Exchange is a question and answer site for Joomla! administrators, users, developers and designers. It's 100% free, no registration required.

I'm developing a small plugin for Joomla 3.x that adds a "Trash article" button when you're in Joomlas Frontend. See screenshot below.

http://imgur.com/NYLGRdY

Now I have this AJAX call that happens when you click on the list item.

Here's the code:

request = {
          "option" : "com_ajax",
          "plugin" : "deletearticle"
          "data"   : "test",
          "format" : "raw"  
};

$.ajax({
       type : "POST",
       data : request,
       success: function (response) {
           $("p:first").html("Data: " + response)
      } 
});

And the helper PHP file.

<?php 
 jimport('joomla.plugin.plugin');
 class plgAjaxDeletearticle extends JPlugin
 {
    function onAjaxDeletearticle()
    {
        $controller = JControllerLegacy::getInstance('Content');
        $controller->execute(JFactory::getApplication()->input->get('task'));
    }
 }

I'm right now doing work experience and I asked my mentor how I should change the article state to trashed. And he gave me the code inside the function and told me that I should be able to modify it in some way, so that the controller will execute a save function on the article.

I've tried and read up on this stuff but it doesn't seem to be all that well documented. Any help would be greatly appreciated as I'm not quite sure how I should proceed.

Thanks.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

First of all you need to pass article ID with your request. Then you can use JTable class to update the state:

public function onAjaxDeletearticle()
{
    // Get id from the request
    $id = JFactory::getApplication()->input->getInt('data');

    // Get the new instance of #__content table
    $table = JTable::getInstance('content');

    // Load the article data by id
    $table->load($id);

    // Set the state to 'trashed'
    $table->state = -2;

    // Store the article
    $table->store();
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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