When creating a Joomla 2.5 content plugin, there are several events at which you can run your code. For example, the hook we've been using in our testing thus far has been onContentAfterTitle, which allows us to run code immediately after the title of the article has been displayed. In this tutorial, we'll look at the various events you can trigger your code at within a content plugin.

How are the plugin events triggered?

Content plugins are triggered within the components/com_content/views/article/view.html.php file. You can see the code below that triggers each event below:

//
// Process the content plugins.
//
JPluginHelper::importPlugin('content');
$results = $dispatcher->trigger('onContentPrepare', array ('com_content.article', &$item, &$this->params, $offset));

$item->event = new stdClass();
$results = $dispatcher->trigger('onContentAfterTitle', array('com_content.article', &$item, &$this->params, $offset));
$item->event->afterDisplayTitle = trim(implode("\n", $results));

$results = $dispatcher->trigger('onContentBeforeDisplay', array('com_content.article', &$item, &$this->params, $offset));
$item->event->beforeDisplayContent = trim(implode("\n", $results));

$results = $dispatcher->trigger('onContentAfterDisplay', array('com_content.article', &$item, &$this->params, $offset));
$item->event->afterDisplayContent = trim(implode("\n", $results));

Testing each plugin event

In our plugin's PHP file, helloworld.php, we added the following functions to test each of the above triggers:

public function onContentPrepare($context, &$row, &$params, $limitstart)
{
    echo "<div>OnContentPrepare</div>";
}

public function onContentAfterTitle($context, &$row, &$params, $limitstart)
{
    return "<div>OnContentAfterTitle</div>";
}

public function onContentBeforeDisplay($context, &$row, &$params, $limitstart)
{
    return "<div>OnContentBeforeDisplay</div>";
}

public function onContentAfterDisplay($context, &$row, &$params, $limitstart)
{
    return "<div>OnContentAfterDisplay</div>";
}

Timing Plugin Events

triggers-and-eventsAs you can see in the screenshot to the right, each plugin event is triggered at different points of the article setup. For example, some events are triggered before the article is displayed, some triggered during, and some ran after.

OnContentPrepare

The OnContentPrepare event is triggered before the article is printed to the screen. If you wanted to modify the content in some way, this would be the hook to use. While this plugin modifies the article, the other events add content to the article.

OnContentAfterTitle

The OnContentAfterTitle event allows you to add content to the article after the title is printed but before the introtext is displayed.

OnContentBeforeDisplay

The onContentBeforeDisplay event is very similar to OnContentAfter Title, but it runs after it.

OnContentAfterDisplay

After the content of an article has been displayed, you can run OnContentAfterDisplay to add information to the end of the article.

Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address
Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Like this Article?
2012-12-30 6:57 pm
Thanks for this info.
I'm trying to get content output before all titles and content and just once (even if it's a category blog) but by using the onContentPrepare, it still runs the function for each item, so the echo in my function outputs for the number of articles I am displaying (which is fine for single articles, but no good for blogs). Is there any way I can get my plug-in content output just once above everything else in the content view?
Staff
9,521 Points
2012-12-31 10:40 am
Hello Netamity,

I think we know what you're trying to accomplish, but unfortunately we don't have a clear answer for you right at this moment. If you could please provide us with an example URL of where this is happening for you, or at the very least a screen shot showing what's going on this could help us troubleshoot this faster for you.

I believe you might need to modify the actual onContentPrepare function, so that it can keep track of how many times it has run on a page, and limit itself to only once. Once we get back an example from you we should be able to test this out a bit and let you know what we come up with.

- Jacob
2012-12-31 2:21 pm
Thank you. I managed to set a session variable at the start of my plugin and then change it after first run of the onContentPrepare function. It's done the job- not very elegant, but it works. Thanks for your time all the same.

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

3 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!