- Posts: 17
COMMUNITY FORUM
- Forum
- K2 Community Forum
- English K2 Community
- [SOLVED] Overriding frontend item edit form (itemform.php)
[SOLVED] Overriding frontend item edit form (itemform.php)
- Nikola
- Topic Author
- Offline
- New Member
Less
More
9 years 11 months ago #133958
by Nikola
Overriding frontend item edit form (itemform.php) was created by Nikola
Hi everyone,
I've tried searching for this as I'm sure the problem was addressed many times already - to no success, however :(
I have several different K2 templates I've created for my users, each is assigned to a different category and the output works perfectly. I've also included the frontend post/edit buttons, but regardless of the category, K2 always calls the "default" itemform.php. For example, I have:
/templates/my_joomla_template/html/com_k2/k2_template_1/
/templates/my_joomla_template/html/com_k2/k2_template_2/
Each of the folders contains category.php, category_item.php, item.php etc... all of the views work exactly as they should, however for some reason K2 disregards the itemform.php and calls the default one. I've tried making a default override:
/templates/my_joomla_template/html/com_k2/default/itemform.php
And voila - it works.
The problem is, I need really different forms for each category.
Is there anything I'm missing?
I am using Joomla 3.3.6 and K2 2.6.8 stable. I've used dev builds before and updated them regularly, however strange problems occur from time to time, so I've decided to stick with the stable one.
Thanks for any help in advance!
I've tried searching for this as I'm sure the problem was addressed many times already - to no success, however :(
I have several different K2 templates I've created for my users, each is assigned to a different category and the output works perfectly. I've also included the frontend post/edit buttons, but regardless of the category, K2 always calls the "default" itemform.php. For example, I have:
/templates/my_joomla_template/html/com_k2/k2_template_1/
/templates/my_joomla_template/html/com_k2/k2_template_2/
Each of the folders contains category.php, category_item.php, item.php etc... all of the views work exactly as they should, however for some reason K2 disregards the itemform.php and calls the default one. I've tried making a default override:
/templates/my_joomla_template/html/com_k2/default/itemform.php
And voila - it works.
The problem is, I need really different forms for each category.
Is there anything I'm missing?
I am using Joomla 3.3.6 and K2 2.6.8 stable. I've used dev builds before and updated them regularly, however strange problems occur from time to time, so I've decided to stick with the stable one.
Thanks for any help in advance!
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
- Offline
- Platinum Member
Less
More
- Posts: 15920
9 years 11 months ago #133994
by Krikor Boghossian
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by Krikor Boghossian on topic Overriding frontend item edit form (itemform.php)
This is quite tricky, since only the default itemform can be overriden without a menu item.
You need to create a menu item for these forms. One per different template you need.
You need to create a menu item for these forms. One per different template you need.
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Nikola
- Topic Author
- Offline
- New Member
Less
More
- Posts: 17
9 years 11 months ago #134033
by Nikola
Replied by Nikola on topic Overriding frontend item edit form (itemform.php)
Hi Krikor,
thank you very much for your reply!
Purely by accident, I discovered this exact solution this morning :) Since the "edit" or "add item" links from withing the category and/or item view pass the Itemid to the form, I am able to create several different forms and call different CSS files using PHP switch statement, depending on the category. Luckily, I only have 3 or 4, so it's not a big deal.
Hell of a component, very VERY customizable, I am able to do wonders with K2. Thank you very much for building it and making it free on top of all!
Cheers,
Nikola
thank you very much for your reply!
Purely by accident, I discovered this exact solution this morning :) Since the "edit" or "add item" links from withing the category and/or item view pass the Itemid to the form, I am able to create several different forms and call different CSS files using PHP switch statement, depending on the category. Luckily, I only have 3 or 4, so it's not a big deal.
Hell of a component, very VERY customizable, I am able to do wonders with K2. Thank you very much for building it and making it free on top of all!
Cheers,
Nikola
Please Log in or Create an account to join the conversation.
- william white
- Offline
- Platinum Member
Less
More
- Posts: 3722
9 years 11 months ago #134037
by william white
Replied by william white on topic Overriding frontend item edit form (itemform.php)
Hello Nikola,
If you will please share whatever part of your code you can here.
If you will please share whatever part of your code you can here.
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
- Offline
- Platinum Member
Less
More
- Posts: 15920
9 years 11 months ago #134042
by Krikor Boghossian
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by Krikor Boghossian on topic Overriding frontend item edit form (itemform.php)
Hello Nikola and thank you so much for your kind words :)
I would also like to see the end result if it is possible.
I would also like to see the end result if it is possible.
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Nikola
- Topic Author
- Offline
- New Member
Less
More
- Posts: 17
9 years 11 months ago - 9 years 11 months ago #134064
by Nikola
Replied by Nikola on topic Overriding frontend item edit form (itemform.php)
The least I can do for the community is share some of my ideas :)
Should anyone else try this, please note that there are probably way more elegant solutions. I am absolutely confident that none of my frontend users have ever heard of any debugging tools, so there is absolutely no way for them to screw anything up - therefore I only use CSS to hide what they don't need to see.
1) Create the "default" itemform.php override in templates/my_joomla_template/html/com_k2/default
2) Add this portion of code to the top:
The $current_url contains the non-SEF current URL. This is, of course, important if Joomla SEF optimization is on.
Next, for example, I have 3 different K2 categories and 3 K2 user groups, each group is given permissions to add/edit items in its own category. Let's call them Cat1, Cat2 and Cat3.
Now, as Krikor suggested, it is absolutely imperative to have a menu item which shows each of these categories. It does not have to be visible, but it must exist somewhere, in a helper menu of some kind. Suppose I create 3 menu items, and their respective Itemids are 101, 102 and 103.
The next step is to load custom CSS files.
I compare the $current_url against the known Itemids and load the corresponding CSS.
These CSS files hide various fields I don't want the inexperienced users to see and modify. For example, one category is such that I only need the extra fields, so I hide all the other tabs. The view override (item.php) is such that it only prints some basic HMTL along with extra field values, and so on.
The other thing I need is to modify the labels, so for example, the "Extra fields" label is changed to "General Data" for the Cat1 and to "Professor Personal Data" for the Cat2. I achieve this by creating new PHP variables and placing them inside the aforementioned condition (perhaps a clumsy solution for a multilanguage site, however I don't have this kind of problem):
Now, in the itemform.php, instead of:
I have:
And so on.
Here's an example form (sorry it's cyrilic, but you'll get the overall idea): i.imgur.com/Z56YoI0.png
This form only shows extra fields and attachments tabs. The output in this category is always the same, except for the extra fields: i.imgur.com/FsJAIt5.png (the extra fields and the attachment are marked red, the rest is always the same).
As I said, it's extremely easy to tweak when you need a really customized solution backed by an awesome framework :) I needed my users to create uniformed articles using extra fields as a kind of form, and thanks to the awesomness of K2, I've managed to do it in 2-3 hours of work.
Thank you once again!
Should anyone else try this, please note that there are probably way more elegant solutions. I am absolutely confident that none of my frontend users have ever heard of any debugging tools, so there is absolutely no way for them to screw anything up - therefore I only use CSS to hide what they don't need to see.
1) Create the "default" itemform.php override in templates/my_joomla_template/html/com_k2/default
2) Add this portion of code to the top:
<?php
JURI::current();
$router =& JSite::getRouter();
$query = $router->parse(JURI::getInstance());
$current_url = JURI::getInstance()->buildQuery($query);
?>
The $current_url contains the non-SEF current URL. This is, of course, important if Joomla SEF optimization is on.
Next, for example, I have 3 different K2 categories and 3 K2 user groups, each group is given permissions to add/edit items in its own category. Let's call them Cat1, Cat2 and Cat3.
Now, as Krikor suggested, it is absolutely imperative to have a menu item which shows each of these categories. It does not have to be visible, but it must exist somewhere, in a helper menu of some kind. Suppose I create 3 menu items, and their respective Itemids are 101, 102 and 103.
The next step is to load custom CSS files.
I compare the $current_url against the known Itemids and load the corresponding CSS.
<?php if (strpos($current_url,'Itemid=101') == true) : ?>
<link href="<?php echo $this->baseurl ?>/templates/my_joomla_template/css/itemid101.css" rel="stylesheet" type="text/css"/>
<?php endif; ?>
These CSS files hide various fields I don't want the inexperienced users to see and modify. For example, one category is such that I only need the extra fields, so I hide all the other tabs. The view override (item.php) is such that it only prints some basic HMTL along with extra field values, and so on.
The other thing I need is to modify the labels, so for example, the "Extra fields" label is changed to "General Data" for the Cat1 and to "Professor Personal Data" for the Cat2. I achieve this by creating new PHP variables and placing them inside the aforementioned condition (perhaps a clumsy solution for a multilanguage site, however I don't have this kind of problem):
<?php if (strpos($current_url,'Itemid=101') == true) : ?>
<link href="<?php echo $this->baseurl ?>/templates/my_joomla_template/css/itemid101.css" rel="stylesheet" type="text/css"/>
<?php $label_extrafields = "General Data";
endif;
if (strpos($current_url,'Itemid=102') == true) : ?>
<link href="<?php echo $this->baseurl ?>/templates/my_joomla_template/css/itemid102.css" rel="stylesheet" type="text/css"/>
<?php $label_extrafields = "Professor Personal Data";
endif; ?>
Now, in the itemform.php, instead of:
...
<li id="tabAttachments"><a href="#k2Tab6"><?php echo JText::_('K2_ATTACHMENTS'); ?></a></li>
...
I have:
...
<li id="tabExtraFields"><a href="#k2Tab5"><?php echo $label_extrafields; ?></a></li>
...
And so on.
Here's an example form (sorry it's cyrilic, but you'll get the overall idea): i.imgur.com/Z56YoI0.png
This form only shows extra fields and attachments tabs. The output in this category is always the same, except for the extra fields: i.imgur.com/FsJAIt5.png (the extra fields and the attachment are marked red, the rest is always the same).
As I said, it's extremely easy to tweak when you need a really customized solution backed by an awesome framework :) I needed my users to create uniformed articles using extra fields as a kind of form, and thanks to the awesomness of K2, I've managed to do it in 2-3 hours of work.
Thank you once again!
Last edit: 9 years 11 months ago by Nikola.
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
- Offline
- Platinum Member
Less
More
- Posts: 15920
9 years 11 months ago #134078
by Krikor Boghossian
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by Krikor Boghossian on topic Overriding frontend item edit form (itemform.php)
NIkola thanks mate, you 're awesome :))
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- aluneoff
- Offline
- New Member
Less
More
- Posts: 1
9 years 1 month ago - 9 years 1 month ago #147286
by aluneoff
Replied by aluneoff on topic Overriding frontend item edit form (itemform.php)
Hi!
more elegant solution
category.php from template/html/your_k2_templ/
insteadchange
add .'&catid='.$this->category->id
category_item.php from template/html/your_k2_templ/
not at hand default template so post work template link
the next in itemform.php from template/html/default/
editaddspecial for category which id is XX
for more than 1 cat or i don't know...
as for me, i rename default/itemform.php to def_itemform.php and create new itemform.php in which simple past this code in the begining of default/itemform.php
and put in default dir itemform.php from template/html/default/ files like 1.php 2.php 3.php 17.php 22.php 98.php
i can do anything else but don't remember... it's works
sorry for my english - it's not my first lang.
more elegant solution
category.php from template/html/your_k2_templ/
instead
<a class="modal" rel="{handler:'iframe',size:{x:990,y:650}}" href="<?php echo $this->addLink;?>"><?php echo JText::_('K2_ADD_A_NEW_ITEM_IN_THIS_CATEGORY'); ?></a>
<a class="modal" rel="{handler:'iframe',size:{x:990,y:650}}" href="<?php echo $this->addLink.'&catid='.$this->category->id;?>"><?php echo JText::_('K2_ADD_A_NEW_ITEM_IN_THIS_CATEGORY'); ?></a>
add .'&catid='.$this->category->id
category_item.php from template/html/your_k2_templ/
not at hand default template so post work template link
<?php if(isset($this->item->editLink)): ?><a class="modal" rel="{handler:'iframe',size:{x:990,y:550}}" href="<?php echo $this->item->editLink; ?>"><?php echo JText::_('K2_EDIT_ITEM'); ?></a><?php endif; ?></p><?php } ?>
the next in itemform.php from template/html/default/
edit
<?php if (JRequest::getInt('cid') !== 0) {} ?>
<?php if (JRequest::getInt('cid') == 0) {} ?>
<?php if((JRequest::getInt('cid')) == 'XX' ) {} ?>
for more than 1 cat or i don't know...
<?php $cur_cat_id = (JRequest::getInt('cid'));
$regex = "/\b".$cur_cat_id."\b/";
// for lite example
$cat_id_spec = "1 2 3 11 98 22 17";
if (preg_match($regex, $cat_id_spec)) {} ?>
as for me, i rename default/itemform.php to def_itemform.php and create new itemform.php in which simple past this code in the begining of default/itemform.php
if (file_exists(__DIR__ . '/'. $cur_cat_id . '.php'))
{ include_once __DIR__ . '/'. $cur_cat_id . '.php';} else { include_once __DIR__ . '/def_itemform.php';}
and put in default dir itemform.php from template/html/default/ files like 1.php 2.php 3.php 17.php 22.php 98.php
i can do anything else but don't remember... it's works
sorry for my english - it's not my first lang.
Last edit: 9 years 1 month ago by aluneoff. Reason: something update
Please Log in or Create an account to join the conversation.
- Forum
- K2 Community Forum
- English K2 Community
- [SOLVED] Overriding frontend item edit form (itemform.php)