BzZzZ - ustvarjamo podobe logo
slovenščina
english
How to make onContentAfterSave compatible with Joomla 2.5 AND 3.x

Joomla 3 introduced a very annoying change, $article parameter in onContentAfterSave event is now passed by value, while in Joomla 1.6.-2.5 it was passed by reference and having the same method definition for both will cause an error in one of them. Depends on how you approach it, e.g. if you have parameter defined by reference, call_user_func_array that works behind the scenes to fire plugin events will return false and since your plugin won't work (without any php error).

So, a coding challenge is to make a plugin that is compatible with both popular joomla series. Take a bit of brain, add a large portion of poetry and here is a way to do it:
In the plugin file create an intermediate class that contains you actual code with one exception - rename onContentAfterSave to something else

class PlgContentSomethingIntermed extends JPlugin {
public function onContentAfterSaveIntermed ($context, $article, $isNew) {
somecode...
}
}


Now conditionally define a class with your actual plugin name using code like this:

if (version_compare ( JVERSION, '3.0', '<' )) {
  class PlgContentSomething extends PlgContentSomethingIntermed {
   public function onContentAfterSave($context, &$article, $isNew) {
   $this->onContentAfterSaveIntermed ( $context, $article, $isNew );
  }
}
} else {
  class PlgContentSomething extends PlgContentSomethingIntermed {
   public function onContentAfterSave($context, $article, $isNew) {
   $this->onContentAfterSaveIntermed ( $context, $article, $isNew );
  }
}
}

Yes, you can do that in PHP.

And again - no ninjas were harmed while performing this magic.

 
  • strateško usmerjeno
  • integrirano
  • s kreativnostjo pospešeno
  • učinkovito komuniciranje
© BzZzZ 2008
stik z nami    zaposlitev    kazalo strani