Hacking the K2 User Profile…again!

In my last post concerning K2 and its user profile, I detailed how to add fields to the Joomla!® User profile, and then have K2 show the added fields. While doing that, you will actually be changing the Joomla!® profile and you will not have actually changed the K2 user profile, apart from adding fields that will just be parsed on to the user component of Joomla!® to handle.

This time round though, I will detail how to actually add fields to the K2 user profile, without affecting the existing Joomla!® profile. The beauty of this hack is that it is now independent of the version of Joomla!® you may be running and only affects the K2 extension. So, here goes…

To add fields to the K2 user profile, please edit the following files as instructed below

1. administrator/components/com_k2/tables/k2user.php – declare your new variables in the TableK2User class which extends the core Joomla!® JTable class. You may also include checking code here by appending your checking code to the check() function of the class.

2. components/com_k2/templates/register.php – edit the front end registration form accordingly, adding the fields you require

3. components/com_k2/templates/profile.php – edit the front end profile edit forms accordingly to allow editing of the user profile including custom fields added.

4. plugins/system/k2/k2.php – edit this file if you need to generate and parse any HTML objects that will go into the register.php or profile.php The function to edit here is onAfterDispatch – edit the control section for registration [if (($option == 'com_user' && $view == 'register') || ($option == 'com_users' && $view == 'registration'))] to add elements to the registration form and the control section for profile editing[if (($option == 'com_user' && $view == 'user' && ($task == 'edit' || $layout == 'form')) || ($option == 'com_users' && $view == 'profile' && $layout == 'edit'))] to add elements to the profile edit form.

Please note that you are not required to edit this file if you do not need to. The hack will work well enough with all your HTML, JS or even php code in the register.php and profile.php

Now that we have edited the relevant files, please add required fields into the k2users table of your Joomla!® installation or the user system will crash. Ensure the field names match the variable names you added in the class of the first file.

Should any of the steps above be too summarized of should you need more details in getting an extended K2 User profile, please do not hesitate to ask in the comments. Also, if K2 changes that way it does things and for some reason I simply do not notice, holla and I will try to update the hack to ensure it always works.

Update

It was brought to my attention that I had not included the code for altering the form on the back end of K2 as well, so here goes.

To add fields to the K2 user profile backed, assuming that you have followed all the above instruction, please edit the following files as instructed

1. administrator/components/com_k2/views/user/tmpl/default.php – edit the back end profile edit form accordingly to allow editing of  a user profile including custom fields added, much like you edited the components/com_k2/templates/profile.php above

2. administrator/components/com_k2/views/user/view.html.php – edit this file if you need to generate and parse any HTML objects that will go into the default.php ,Edit this in much the same way as you edited the file plugins/system/k2/k2.php

To alter the list of users being viewed in the backed of K2, so that you can list a newly added custom field, please edit the files below.

1. administrator/components/com_k2/views/users/tmpl/default.php – edit this file to alter the table that shows the list of users in the K2 backend. This editing could involve adding/changing a table header, and adding/changing the relevant variable called in the foreach loop found in the <tbody> section.

2. administrator/components/com_k2/models/users.php – edit this file to alter the MySQL query run to get all the variables for available users. Edit the $query variable around line 36 – change the k2user.group to k2user.* to allow you all added fields in the generated variables that are parsed on to the list.

Share

53 comments

  1. I modified the function check ()

    if (jstring :: trim ($ this-> soundcloud) = ” && substr ($ this-> soundcloud, 0, 8) =! ‘https: //’)
                 $ this-> soundcloud = ‘https: //’.$this-> soundcloud;

    return true;

    but not resolved

  2. Hi Allan Jagwe! First of all thanks for share this hack, I used many times on my proyects. I have a question, is there a simply way to add multiple Authors to an item? I surf the web but i didn´t find anything…Thanks!

Leave a Reply