Question

I'm going to use Joomla K2 component as a directory system only, So just show Extra-Fields in front-end contents.

Therefore in the K2 item submission form which is available to registered users, I tend to remove all further inputs (including content tab and its huge textarea for writing post, attachment tab, title input, publish radio buttons and so on) and keep extra-fields' inputs only. I tried to do this through k2 template overriding by hiding those inputs using CSS or HTML however it's not a good idea because users are still able to show and see hidden stuff by set display to normal via browsers' developer tools.

Also it isn't possible to achieve this purpose by put those fields in an always false PHP condition since some inputs need to have at least one value, It seems.

Below is the default layout of K2 submission form in user section, Red areas are those I want to make them removed and inactive:

enter image description here

And here's the source of default layout:

com_k2/templates/default/itemform.php

By hiding category input from users, I also need to set a predefined category which all contents that users submit would apply to it and Extra-fields related to that category are shown to users in submission form. Is there any way to define a value in template overriding files and hide its related field completely? (It's better to be by variable but constant would work too). I would need it to Auto title assignment for item submitted by users too (However it's not as necessary as other things)

All these change would be in K2 adding item form in front-end site and not Admin section.

What's the workaround to this all?

Regards

Was it helpful?

Solution

I know this question is old but this is my idea, I did something similar, not exactly the same.

Because you can't submit the form if you remove the required fields like Category or Title, you can put a hidden INPUT field with a random value, as long as it is not empty value. In your override file templates/YOUR TEMPLATE/html/com_k2/default/itemform.php instead of showing category selection:

<tr>
    <td class="adminK2LeftCol">
        <label><?php echo JText::_('K2_CATEGORY'); ?></label>
    </td>
    <td class="adminK2RightCol">
        <?php echo $this->lists['categories']; ?>
    </td>
</tr>

You use something like this

<input type="hidden" id="catid" name="catid" value="-1">

Yes naughty users can use Firebug to put their values in that INPUT, but you still can build a plugin and listen to onBeforeK2Save event then set your default value of category to your own value (0, 1 , 2 anything) before saving the content to database. By using this way, you can generate title for your K2 item too.

<?php
defined('_JEXEC') or die ;

JLoader::register('K2Plugin', JPATH_ADMINISTRATOR.'/components/com_k2/lib/k2plugin.php');

class plgK2MyExample extends K2Plugin
{
    var $pluginName = 'myexample';
    var $pluginNameHumanReadable = 'My Example K2 Plugin';

    function onBeforeK2Save(&$item, $isNew)
    {
        $item->catid = 10000;
        $item->title = 'my own title';
    }
}

Check the example plugin here: https://github.com/joomlaworks/example-k2-plugin (onBeforeK2Save is missing in the example plugin).

onBeforeK2Save is called in administrator/components/com_k2/models/item.php ("save" function).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top