K2 Templating: Create a K2 Plugin
Well, this is kind of a deviation, but is related to K2 templates. In this tutorial we show you how to create a k2 plugin. This is more of an overview than a full plugin tutorial; just a taste. K2 specific plugins allow you to add input fields that are globally available for all K2 items and categories.
Writing a plugin for K2 is super easy. They even give you a nice example to start from:
http://getk2.org/extend/extensions/90-example-k2-plugin-for-developers
The best way to know how plugins are placed within the template is to look at the template that you’re using.
Below is a convenient placement that we use often for blog subtitles:
<!-- K2 Plugins: K2AfterDisplayTitle -->
<?php echo $this->item->event->K2AfterDisplayTitle; ?>
To make plugins only requires two files: pluginname.xml and pluginname.php
The XML file defines the plugin’s inputs options for the administrator. Within this file, you’ll have to be careful as there exists two sections; one for Joomla 1.5 and Joomla 2.5. In addition to defining the output types and the plugin parameters, the XML file has parameters settings that will be made available in the administrator as inputs. Below is from the example provided
<params group="item-video">
<param name="videoURL_item" type="text" size="80" default="" label="YouTube URL (for item forms)" description="" />
</params>
The above defines the administrator input within the video section of the item. Simiarly, if you wanted to define a text input for the content section, you might do something like this:
<params group="item-content">
<param name="subtitle" type="text" size="80" default="" label="Subtitle" description="" />
</params>
The PHP file defines the output locations. To correspond to the earlier “K2AfterDisplayTitle”, make sure to modify the “onK2AfterDisplayTitle” function. You have access to the $params that you defined in the XML file as well as the normal sets of functions available from Joomla (like JFactory::getApplication() and JFactory::getDocument() ). Below is something we might use to display just the “subtitle” as defined by the above XML example.
function onK2AfterDisplayTitle( &$item, &$params, $limitstart) {
$mainframe = &JFactory::getApplication();
$plugins = new K2Parameter($item->plugins, '', $this->pluginName);
return $plugins->get('subtitle');
}
If you want to know what options are available and how the plugin would interact with the administrator, checkout the link below and you can figure out what is available to you:
http://getk2.org/community/New-to-K2-Ask-here-first/164657-Solved-Plugin-development–select-admin-tab
Also, you can look at the source for the admin panel and see what’s really going on:
“/administrator/components/com_k2/views/item/tmpl/default.php” (for Item entry)
If all of your items/categories are the same, this is another way to add extra input that you might require.
Remember that if you need any more examples for plugins, you can always get a few and open the zip file.