3

Dear Joomla Community,

I'm working on a little Joomla (3.4.1) system plugin that injects some meta tags into an article. It's working fine so far. Now I'd like to extend the plugin to copy the content of an other article into the article that's processed by the plugin. Is this somehow possible?

To be more precise, what I'm looking for is:

  • How can I get the content/body from an other article from inside my plugin?
  • How can the content/body then be injected into the current article (i.e. the article the plugin is currently processing)?
  • Currently the plugin does all it's work in onBeforeRender(). Is that the right place to read and inject an other article?

Any advice is welcome. Thank you very much!

1 Answer 1

1

You can load/print any article by articleId using this code:

<?php
    $articleId = 22; // Your article id here
    $table_plan         =  JTable::getInstance('Content', 'JTable');
    $table_plan_return  = $table_plan->load(array('id'=>$articleId));
    echo $table_plan->introtext;
?>

You can also output the full text using echo $table_plan->fulltext; if your articles contain a Read More separation. However, keep in mind that fulltext is empty if no Read More separator has been added to the text.


Update:

In order to replace any article with the given content, you can use onContentPrepare:

public function onContentPrepare($context, &$row, &$params, $page = 0)
    {
        if ($context == "com_content.article") { // run only in article views
            $articleId = 22; // Your article id here
            $table_plan         =  JTable::getInstance('Content', 'JTable');
            $table_plan_return  = $table_plan->load(array('id'=>$articleId));
            $row->text = $table_plan->introtext;
        }
    }
}

You can use the parameters ($context, $row, $params, $page = 0) and if/else-statements to control when and how the articles will be replaced.

3
  • Thank you very much, that helped a lot! One question though: Putting the code into onContentPrepare() causes the echo statement to print the content of the injected article above the destination articles headline/title. Is there a way to completely replace the content of the destination article? Commented May 30, 2015 at 23:38
  • @user324820, please see updated answer.
    – johanpw
    Commented May 31, 2015 at 3:08
  • Thank you very much for the detailed answer - especially for including the if ($context == "com_content.article") part, I would have missed that. Totally awesome! Commented May 31, 2015 at 9:40

Your Answer

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

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