简体   繁体   English

在模板中显示Joomla插件字段数据| 第一个K2扩展

[英]Display Joomla plugin fields data in template | first K2 extension

I have a problem and I'm new to Joomla and K2, so any help will be highly appreciated. 我遇到了问题,而且我是Joomla和K2的新手,所以我们非常感谢您的帮助。

I'm developing a simple joomla plugin based on Example K2 Plugin ( http://getk2.org/extend/extensions/90-example-k2-plugin-for-developers ). 我正在基于示例K2插件( http://getk2.org/extend/extensions/90-example-k2-plugin-for-developers )开发一个简单的joomla插件。

What i'm trying to create is a K2 extension for Categories, so i'll be able to add extra content to categories. 我要创建的是类别的K2扩展,因此我将能够向类别添加额外的内容。 I already did a search and there is not plugin or extension to cover my needs for this project with Joomal 3.x. 我已经进行了搜索,但是还没有插件或扩展程序可以满足我使用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. 对于每个类别,我在后端将信息设置为货币,语言,国家等。我通过xml文件将此字段添加到后端。

I have tried several ways, but I haven't been able to access this information on my k2 template. 我尝试了几种方法,但无法在k2模板上访问此信息。 When i dump parameters I get the default value from the xml, but not the one already saved for the category. 当我转储参数时,我从xml获取默认值,但没有为该类别保存一个默认值。

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. 因此,我能够使用插件模板轻松显示内容,但是由于缺陷,只能用于类别的功能是onK2CategoryDisplay,但是我想在这里实现的是从K2模板中调用字段值,例如标题或在画廊下面。

I found this lines, but it only displays the default text saved on the xml file, and not the new content. 我找到了这些行,但是它仅显示保存在xml文件中的默认文本,而不显示新内容。 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: 这是category.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 这是category.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. 缺乏K2及其开发人员的文档,这是一个巨大的痛苦。

Retrieved from the database the information with $this->params->get('itemK2Plugins'), but also with: $this->category->plugins 从数据库中检索信息,该信息包括$ this-> params-> get('itemK2Plugins'),还包括:$ this-> category-> plugins

    <?php if($this->params->get('itemK2Plugins')): ?>

    <?php 
$array = json_decode($this->category->plugins, true);
foreach($array as $key => $value) { 
    $keys[$key] = $value;   
}

$countryL = $keys['categorieslocalCountry_cat'];    
$MottoL = $keys['categorieslocalMotto_cat'];

    ?>

    <?php endif; ?>

If there is a different and correct way to do it, please let me know! 如果有其他正确的方法,请告诉我!

Thanks :) 谢谢 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM