Linkr API: Plugin Events
Linkr uses events to interact with other extensions.Related readings:
onLinkrGetLinks
This event is called when the view is first being displayed. It passes one argument (current installed version of Linkr) and expects an associative array of titles and javascript statements to be returned. Here's an example of a plugin that would create links for a fictitious extension "Foo":class plgContentFoo extends JPlugin { // Your code... // As of 2.2.2, $version is an interger function onLinkrGetLinks( $version ) { return array( // Link title => Javascript statement 'Foo' => 'FooJavaScriptFunc' ); } }NOTE: As of 2.2.2, the version is passed as an interger i.e. 222 instead of 2.2.2
onLinkrLoadJS
This is where the plugin would return the javascript needed for it to work. It is recommended to use objects and variables, but you're plugin can work just fine with a combination of functions (see Object-Oriented Programming). Here's an example from the "Foo" plugin:class plgContentFoo extends JPlugin { // Your code... // As of 2.2.2, $version is an interger function onLinkrLoadJS( $version ) { // URL for AJAX requests. Be sure to use full URLs $r = JURI::base() .'index.php?option=com_foo'. '&tmpl=component&view=foo'; // Security measure to prevent CSRF* $r .= '&'. JUtility::getToken() .'=1'; // Javascript return 'var LinkrFoo = { requestURL:"'. $r .'", /* etc, etc */ }'; } }*Cross Site Request Forgery attacks
Note that the plugin is not required to return anything. You may choose to include the javascript yourself:
class plgContentFoo extends JPlugin { // Your code... function onLinkrLoadJS( $version ) { if ( $version >= 222 ) { // External javascript $doc = & JFactory::getDocument(); $doc->addScript( 'path/to/script.js' ); return ''; } } }
Tips / Tricks
You can make a link available only in the back-end. This is how you would check to see if the article is being edited from the front-end or back-end:class plgContentFoo extends JPlugin { // Your code... function onLinkrLoadJS( $version ) { global $mainframe; if ( $mainframe->isAdmin() ) { // execute code } } }
Example
Here is what your plugin might look like:// Plugin Librairies jimport( 'joomla.plugin.plugin' ); // Foo Plugin class plgContentFoo extends JPlugin { // Constructor function plgContentFoo( &$subject, $config ) { parent::__construct( $subject, $config ); } // Send links to Linkr function onLinkrGetLinks( $version ) { return array( // Link title => Javascript statement 'Foo' => 'FooJavaScriptFunc' ); } // Scripts function onLinkrLoadJS( $version ) { $doc = & JFactory::getDocument(); $doc->addScript( 'plugins/content/foo/script.js' ); // URL for AJAX requests. Be sure to use full URLs $r = JURI::base() .'index.php?option=com_foo'. '&tmpl=component&view=foo&'. JUtility::getToken() .'=1'; return 'LinkrFoo.init("'. $r .'")'; } }
last updated aug 29 2008

