I have a problem and I'm new to Joomla and K2, so any help will be highly appreciated.
I'm developing a simple joomla plugin based on Example K2 Plugin (http://getk2.org/extend/extensions/90-example-k2-plugin-for-developers).
What i'm trying to create is a K2 extension for Categories, so i'll be able to add extra content to categories. I already did a search and there is not plugin or extension to cover my needs for this project with Joomal 3.x.
For each category I set information in the backend as currency, language, country , etc. I added this fields to the backend via the xml file.
I have tried several ways, but I haven't been able to access this information on my k2 template. When i dump parameters I get the default value from the xml, but not the one already saved for the category.
So i'm being able to easily display the content with a plugin template but by defect the only function available for categories will be onK2CategoryDisplay, but what i'm trying to achieve here is to call the fields values from the K2 template, next to the title for example, or below a gallery.
I found this lines, but it only displays the default text saved on the xml file, and not the new content. If i'm not being clear, please let me know and i'll update this post. Thanks in advance.
$plugin = JPluginHelper::getPlugin('k2', 'categories');
$pluginParams = new JRegistry();
$pluginParams->loadString($plugin->params);
$param = $pluginParams->get('localCountry_cat');
var_dump($param);
Here is the categories.xml:
<?xml version="1.0" encoding="utf-8"?>
<extension version="3.1" type="plugin" group="k2" method="upgrade">
<name>Categories K2 Plugin</name>
<files>
<filename plugin="categories">categories.php</filename>
<folder>categories</folder>
</files>
<config>
<fields name="params">
<fieldset name="basic">
<field name="localCountry_cat" type="text" size="80" default="test" label="Country local name" description="" />
<field name="capital_cat" type="text" size="80" default="test" label="Capital" description="" />
<field name="languages_cat" type="text" size="80" default="test" label="Official Languages" description="" />
</fieldset>
</fields>
</config>
<!-- K2 backend field parameters -->
<fields group="category">
<field name="localCountry_cat" type="text" size="80" default="" label="Country local name" description="" />
<field name="capital_cat" type="text" size="80" default="" label="Capital" description="" />
<field name="languages_cat" type="text" size="80" default="" label="Official Languages" description="" />
</fields>
Here is the categories.php
// no direct access
defined('_JEXEC') or die('Restricted access');
// Load the K2 plugin API
JLoader::register('K2Plugin', JPATH_ADMINISTRATOR.DS.'components'.DS.'com_k2'.DS.'lib'.DS.'k2plugin.php');
class plgK2categories extends K2Plugin {
// Required global reference parameters
var $pluginName = 'categories';
var $pluginNameHumanReadable = 'Categories K2 Plugin';
var $plgCopyrightsStart = " "
var $plgCopyrightsEnd = " ";
function plgK2categories( & $subject, $params) {
parent::__construct($subject, $params);
}
function onK2CategoryDisplay( & $category, & $params, $limitstart) {
// API
$mainframe = JFactory::getApplication();
$document = JFactory::getDocument();
// ---------- Get plugin parameters ---------------
// Global plugin params
$plugin = JPluginHelper::getPlugin('k2', $this->pluginName);
$pluginGlobalParams = new JRegistry( $plugin->params );
// K2 Category plugin specific params
$pluginParams = new K2Parameter($category->plugins, '', $this->pluginName);
$local = $pluginParams->get('localCountry_cat');
$capital = $pluginParams->get('capital_cat');
$languages = $pluginParams->get('languages_cat');
$currency = $pluginParams->get('currency_cat');
// --------- Requirements -------
require_once(dirname(__FILE__).DS.$this->pluginName.DS.'includes'.DS.'helper.php');
// ---------- Fetch the template -------
ob_start();
$getTemplatePath = categoriesHelper::getTemplatePath($this->pluginName,'default.php');
$getTemplatePath = $getTemplatePath->file;
include($getTemplatePath);
$getTemplate = $this->plgCopyrightsStart.ob_get_contents().$this->plgCopyrightsEnd;
ob_end_clean();
// ----- Output -----
return $getTemplate;
}
} // END CLASS
I already solved it. It was easier than I thought, but took me a while to discover it. The lack of documentation for K2 and it's developers is a huge pain.
Retrieved from the database the information with $this->params->get('itemK2Plugins'), but also with: $this->category->plugins
If there is a different and correct way to do it, please let me know!
Thanks :)