I love K2, and I've played with a lot of other CCK solutions, some are great but often its more than you need when K2 is almost perfect and you dont want to start from scratch building a whole app through another CCK.
In this case, I needed to customize (read: simplify) the backend of K2 for a client and I realized it would be so great if I could control the backend template per category just like I can for the frontend. So I put together this hack. Its not perfect, but it works.
This way, I can have some categories whose admin pages show their extra fields in the first tab, or perhaps just the text editor and video settings, depending on what I need for that content, without confusing the client with lots of options they dont need.
In theory, this setup combined with extra fields and the ability to call specific extra field data in frontside K2 templates basically makes K2 as versatile as any CCK, without the need to build it all up from scratch. For most projects, K2 is a perfect base, and with these two additions I can customize it more than I ever might need. Of course, either of these might be useful to you on its own as well.
Part A: Adding Custom Admin Templates
I previously had a more roundabout way of doing this posted here, but this way is much easier!
Step 1: In the file administrator/components/com_k2/views/item/view.html.php paste the following code just after
$this->assignRef('form', $form); (around line 307)
//Look for specific admin template file
jimport('joomla.filesystem.folder');
$componentPath = JPATH_SITE.DS.'components'.DS.'com_k2'.DS.'templates';
$componentFolders = JFolder::folders($componentPath);
$db =& JFactory::getDBO();
$query = "SELECT template FROM #__templates_menu WHERE client_id = 0 AND menuid = 0";
$db->setQuery($query);
$defaultemplate = $db->loadResult();
if (JFolder::exists(JPATH_SITE.DS.'templates'.DS.$defaultemplate.DS.'html'.DS.'com_k2'.DS.'templates'.DS.'admin'.DS.$item->catid))
{
$this->_addPath('template',
JPATH_SITE.DS.'templates'.DS.$defaultemplate.DS.'html'.DS.'com_k2'.DS.'templates'.DS.'admin'.DS.$item->catid);
}
Step 2: To initiate the override, duplicate the folder and contents of /administrator/components/com_k2/views/item/tmpl, move it to templates/MY_TEMPLATE/html/com_k2/admin and rename the new folder with the category id you want to override. Then edit the enclosed file default.php (the template for the k2 item backend screen) as much as you want to change the admin appearance for that category.
Click the link below for an example of admin view for editing a specific category with the admin item view override. Notice the extrafield tab is now first, I removed the unnecessary tabs, and renamed the remaining ones.
Part B: Calling Specific Extra Field Data on the Frontend
Note: Ive actually only used this in the item view, but Id imagine it would work fine in the category_item view as well. UPDATE: it works fine there too.
Note 2: I had a different method posted here before but I found out a better way to do it. The old way put the extra field data in an array based on the ordering of the fields but that meant that if you wanted to reorder the fields - something that I couldnt figure out at first but is possible, just for some reason you have to click order at the top of the column before it lets you - anyway, this would change the array and therefore break the template you made based on the specific extra fields. This way, the extrafield values are based on their ids, so you can reorder all you want and they will still work fine.
Step 1: Make a new K2 template - copy the folder and contents from components/com_k2/templates/default to templates/[your joomla template]/html/com_k2/templates and rename it.
Step 2: In the item.php file in your new template insert the following code just after K2 Plugins: K2BeforeDisplayContent is called. Note: Basically this just puts all the extra field data for this item in an array so you can access it. I dont know if it matters where you place this, but it has be be sometime before you try to use your extra fields in the template.
<!-- Call to prepare extra fields -->
<?php
//convertArray to use ids as key
$extrafields = array();
foreach($this->item->extra_fields as $item)
{
$extrafields[$item->id] = $item->value;
}
?>
Step 3: Now you can use the following bit of code anywhere to call any extra field value by its id.
<?php echo $extrafields[13];?>
The array value in the brackets after the variable will correspond to the id of the extra field. you can see these in the backend extra fields view.
Just to get you started, an example of how I used this was to make a custom social media link section on a site thats easily updated by the client - just by putting in the links they want for each item in the extra fields of that category, rather than having to try and use the editor. It also checks if there is any value in each before displaying the icon:
<div class="artistSocialMedia">
<!-- Twitter --> <a href="/"
target="_blank"><img src="/images/smicons/twitter2.png" alt="Twitter" title="Twitter" /></a>
<!-- Facebook --> <a href="/"
target="_blank"><img src="/images/smicons/fb2.png" alt="Facebook" title="Facebook" /></a>
<!-- Vimeo --> <a href="/"
target="_blank"><img src="/images/smicons/vimeo2.png" alt="Vimeo" title="Vimeo" /></a>
<!-- Youtube --> <a href="/"
target="_blank"><img src="/images/smicons/youtube2.png" alt="Youtube" title="Youtube" /></a>
<!-- Flickr --> <a href="/"
target="_blank"><img src="/images/smicons/flickr2.png" alt="Flickr" title="Flickr" /></a>
<!-- Instagram --> <a href="/"
target="_blank"><img src="/images/smicons/instagram2.png" alt="Instagram" title="Instagram" /></a> <?php endif; ?>
</div>
The next step will be to figure out how to make all this into a plugin, but im far from that at the moment...
Also, I havent tried it yet, but combined with the fields for k2 extension this could be pretty sweet.
Thats it for now, hope it helps some of you... let me know in the comments what you think.
Also, check out other web/joomla/k2 related posts on my blog: http://jurawa.com/notes