9

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.

https://i.stack.imgur.com/nlycx.jpg

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.

1 Answer 1

9

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();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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