Solved! K2 - Help! Default category on frontend add new item

This forum is for general questions about extensions for Joomla! 2.5.

Moderators: pe7er, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Locked
LBJerryH
Joomla! Intern
Joomla! Intern
Posts: 70
Joined: Tue Sep 13, 2005 2:42 pm

Solved! K2 - Help! Default category on frontend add new item

Post by LBJerryH » Wed Jan 16, 2013 4:11 pm

I posted this question a few weeks ago on the K2 forum, but have gotten nowhere. Maybe someone here can help me.

I need to make it where when a user submits a new item from the frontend, the category is set to a default category that they do not have to choose. I have searched and searched but all I can find is a fix for Joomla 1.5. I am running 2.5. Can anyone help me with this? Its a simple coding fix, but I am not good at coding. Thanks for the help in advance. Below is the link to the 1.5 solution.

http://www.byte-my-neck.com/j/index.php ... 2&Itemid=4

If anyone can help, I would really appreciate it. If only the files I need to edit can be identified, that would be huge. Thanks so much in advance!
Last edited by LBJerryH on Sun Jan 20, 2013 2:53 pm, edited 1 time in total.

BizCentric
Joomla! Apprentice
Joomla! Apprentice
Posts: 46
Joined: Wed Apr 25, 2012 6:53 pm

Re: K2 - Help! Default category on frontend add new item

Post by BizCentric » Fri Jan 18, 2013 9:45 pm

The guide to 'hacking' the code is pretty self-explanatory. You just need to get into the 'file system' of joomla and follow the directory tree to where that link specifies and change that code on that single line.

What the code does is that instead of allowing for a choice to be made between categories, the code forces just one category to be chosen, as specified by quoting the single id number.

LBJerryH
Joomla! Intern
Joomla! Intern
Posts: 70
Joined: Tue Sep 13, 2005 2:42 pm

Re: K2 - Help! Default category on frontend add new item

Post by LBJerryH » Fri Jan 18, 2013 11:15 pm

BizCentric wrote:The guide to 'hacking' the code is pretty self-explanatory. You just need to get into the 'file system' of joomla and follow the directory tree to where that link specifies and change that code on that single line.

What the code does is that instead of allowing for a choice to be made between categories, the code forces just one category to be chosen, as specified by quoting the single id number.
Thanks for the help. I actually just figured this out, but now I have another problem. The default category is selected, but the extra fields on the form won't display without the specific category being manually selected. Is there any way around this to just make them display without choosing the category?

BizCentric
Joomla! Apprentice
Joomla! Apprentice
Posts: 46
Joined: Wed Apr 25, 2012 6:53 pm

Re: K2 - Help! Default category on frontend add new item

Post by BizCentric » Fri Jan 18, 2013 11:29 pm

It gets quite tricky now. You will have to find where the code for the extra fields is situated and probably do a similar hack by setting the default category or something similar.

Due to K2 being so vast, it's sometimes very hard to track down where the code for everything lies.

But as you said, code isn't your strong point. Perhaps finding someone thats part of the K2 development and paying them a consultant fee to help you might work (maybe 15 dollars an hour?)

LBJerryH
Joomla! Intern
Joomla! Intern
Posts: 70
Joined: Tue Sep 13, 2005 2:42 pm

Re: K2 - Help! Default category on frontend add new item

Post by LBJerryH » Fri Jan 18, 2013 11:32 pm

BizCentric wrote:It gets quite tricky now. You will have to find where the code for the extra fields is situated and probably do a similar hack by setting the default category or something similar.

Due to K2 being so vast, it's sometimes very hard to track down where the code for everything lies.

But as you said, code isn't your strong point. Perhaps finding someone thats part of the K2 development and paying them a consultant fee to help you might work (maybe 15 dollars an hour?)
I think I found the code, I just don't know what to do with it. This is what I found just below the hack that I figured out about the default category:

JTable::addIncludePath(JPATH_COMPONENT.DS.'tables');
$category = JTable::getInstance('K2Category', 'Table');
$category->load($item->catid);

$extraFieldModel = K2Model::getInstance('ExtraField', 'K2Model');
if ($category->id)
{
$extraFields = $extraFieldModel->getExtraFieldsByGroup($category->extraFieldsGroup);
}
else
{
$extraFields = NULL;
}

for ($i = 0; $i < sizeof($extraFields); $i++)
{
$extraFields[$i]->element = $extraFieldModel->renderExtraField($extraFields[$i], $item->id);
}

Can you tell what I should do?

BizCentric
Joomla! Apprentice
Joomla! Apprentice
Posts: 46
Joined: Wed Apr 25, 2012 6:53 pm

Re: K2 - Help! Default category on frontend add new item

Post by BizCentric » Sat Jan 19, 2013 2:50 am

I notice that these 2 lines seem the most important regarding the category.

If my memory serves me right about objects and classes, then at this function:

Code: Select all

$category->load($item->catid);
All of the categories that do exist are being loaded, and at this function:

Code: Select all

if ($category->id)
{
$extraFields = $extraFieldModel->getExtraFieldsByGroup($category->extraFieldsGroup);
}
It's using a conditional statement to see if a category has been chosen. I'm not so sure because my PHP knowledge isn't that great, but if you fix the ($category->id)...
to something similar to what was done in the link you provided above, it may work as it will always check for the specific id (which has been auto-chosen through the other edit).

LBJerryH
Joomla! Intern
Joomla! Intern
Posts: 70
Joined: Tue Sep 13, 2005 2:42 pm

Re: K2 - Help! Default category on frontend add new item

Post by LBJerryH » Sat Jan 19, 2013 3:47 am

BizCentric wrote:I notice that these 2 lines seem the most important regarding the category.

If my memory serves me right about objects and classes, then at this function:

Code: Select all

$category->load($item->catid);
All of the categories that do exist are being loaded, and at this function:

Code: Select all

if ($category->id)
{
$extraFields = $extraFieldModel->getExtraFieldsByGroup($category->extraFieldsGroup);
}
It's using a conditional statement to see if a category has been chosen. I'm not so sure because my PHP knowledge isn't that great, but if you fix the ($category->id)...
to something similar to what was done in the link you provided above, it may work as it will always check for the specific id (which has been auto-chosen through the other edit).
I will give it a try. Thanks so much for your help. I really appreciate it.

LBJerryH
Joomla! Intern
Joomla! Intern
Posts: 70
Joined: Tue Sep 13, 2005 2:42 pm

Re: K2 - Help! Default category on frontend add new item

Post by LBJerryH » Sun Jan 20, 2013 2:53 pm

It worked!

I entered the extra fields group id here: (where it says 12)

Code: Select all

 JTable::addIncludePath(JPATH_COMPONENT.DS.'tables'); 
        $category = JTable::getInstance('K2Category', 'Table'); 
        $category->load(12); 
 
        $extraFieldModel = K2Model::getInstance('ExtraField', 'K2Model'); 
        if ($category->id)
Thanks for the help, BizCentric! I really appreciate it!

BizCentric
Joomla! Apprentice
Joomla! Apprentice
Posts: 46
Joined: Wed Apr 25, 2012 6:53 pm

Re: Solved! K2 - Help! Default category on frontend add new

Post by BizCentric » Sun Jan 20, 2013 4:58 pm

My pleasure.

You can actually learn a lot about programming from the hacks. Answering your issue made me understand PHP better, which seems very similar to C++.

framontb
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Tue Mar 23, 2010 4:41 pm

Re: Solved! K2 - Help! Default category on frontend add new item

Post by framontb » Tue Aug 09, 2016 6:02 pm

The problem with hacking code is: updates. An update on your K2 component and your code is gone.
Perhaps safer way is to override the mod_k2_user layout. In this way, if you updates your K2 component, you don't loss your code.
For example, if you are using the protostar template:
- create the folder: templates/protostar/html/mod_k2_user
- copy the userblock.php file (from modules/mod_k2_user/tmpl/userblock.php to templates/protostar/html/mod_k2_user)
- found "$user->profile->addLink" in userblock.php and replace with "$user->profile->addLink.'&catid=666'" (supose 666 is your default category)
Done !
Now if you access mod_k2_user > add new item in front end it will be with your default category.


Locked

Return to “Extensions for Joomla! 2.5”