When writing 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.

Like this Article?
Netamity 2012-12-30 10: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?
JacobIMH Community Support technician 2012-12-31 2:40 pm
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
Netamity 2012-12-31 6: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.

Login to comment.

Your Opinion Matters

... but we need to know what you're thinking!

I'm Brad Markle, your friendly Community Support technician, and I wrote the article you're looking at now. I like to think it's perfect, but I'm sure you have some suggestions. Please, let me know what they are!

Feedback
Your Email Address
Because we'd like to talk with you!

Latest Questions

If you need some help, submit your question to our Community!
We guarantee a response within 60 minutes (8am - 9pm EST, Monday - Friday)
Ask a Question!
Recent Questions
  1. I'm having trouble publishing with iWeb
  2. How store set of pdf files that viewers may download from my website.
  3. Has anyone created Terms of Services for their resellers?

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!