5

I've read that $context parameter is:

"The context of the content being passed to the plugin - this is the component name and view - or name of module (e.g. com_content.article)"

https://docs.joomla.org/Plugin/Events/Content

What does it mean? Can we manipulate module(s) and other component(s) in addition to com_content from the content plugin, and if yes then how? Can you provide an example of code?

3

The $context parameter can be used to define if your plugin should run or not under certain circumstances. For example, you might want your plugin to run when displaying category blogs, but not when viewing the full article:

if ($context == "com_content.category") {
    // Your code for category blog here
}

Some of the available contexts are:

  • com_content.category
  • com_content.article
  • com_content.featured
  • com_contact.category.title
  • com_tags.tag

(List contains only contexts I could confirm with a quick test, feel free to add more)

You can create a small plugin to see the different contexts:

<?php
// no direct access
defined('_JEXEC') or die;

class plgContentContext extends JPlugin
{
    public function onContentPrepare($context, &$article, &$params)
        {
            echo '<h3 style="color:white;background:red">' . $context . '</h3>';
        }
}
?>
2

You can modify any component that uses the onContentPrepare event, i've done a quick search on Joomla 3.4.4 and it is used by com_config, com_content, com_tags and com_users.

Any extension may also choose to use this plugin trigger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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