Friday, October 31, 2014
   
Text Size
Login

Creating a Profile Plugin for Joomla 1.6

So there is other documentation out there. I'm basing most of this article on the official Joomla 1.6 documentation: http://docs.joomla.org/Creating_a_profile_plugin and I want to use an example that worked for me. Important: This is for Joomla version 1.6.x+.

Aim / Objective
  1. Add a field with a dropdown list.
  2. Add a button to upload an avatar (and sync with 3rd party avatar).
Not riveting stuff but something that all my clients want. Fortunately for me, I just created one plugin which I can install on any number of Joomla 1.6 sites and it does all this for me... Neat huh?

Folder
I'm going to shove all these files into a folder which I will then compress and install using the Joomla! CMS Extension Manager.

I'm going to call the folder "profile5" (without the double-quotes) and create a subfolder called "profiles".

So I should end up with the following file/directory structure:

  1.  ./profile5 
  2.       ./profile5/profiles 
Also copy one of those handy blank "index.html" files into each of these folders in case "directory listing" gets enabled on your server.I'm going to refer to the files to create as being in this folder.

./profile5/profile5.php

  1.  <?php 
  2.   /** 
  3.    * @version 
  4.    * @copyright  Copyright (C) 2011 JoelLipman.Com, Inc. All rights reserved. 
  5.    * @license            GNU General Public License version 2 or later; see LICENSE.txt 
  6.    */ 
  7.   
  8.   defined('JPATH_BASE') or die; 
  9.   
  10.    /** 
  11.     * An example custom profile plugin. 
  12.     * 
  13.     * @package           Joomla.Plugins 
  14.     * @subpackage        user.profile 
  15.     * @version           1.6 
  16.     */ 
  17.    class plgUserProfile5 extends JPlugin 
  18.    { 
  19.          /** 
  20.           * @param       string  The context for the data 
  21.           * @param       int             The user id 
  22.           * @param       object 
  23.           * @return      boolean 
  24.           * @since       1.6 
  25.           */ 
  26.          function onContentPrepareData($context$data
  27.          { 
  28.                  // Check we are manipulating a valid form. 
  29.                  if (!in_array($contextarray('com_users.profile','com_users.registration','com_users.user','com_admin.profile'))){ 
  30.                          return true; 
  31.                  } 
  32.                  $userId = isset($data->id) ? $data->id : 0; 
  33.                  // Load the profile data from the database. 
  34.                  $db = &JFactory::getDbo(); 
  35.                  $db->setQuery( 
  36.                          'SELECT profile_key, profile_value FROM #__user_profiles' . 
  37.                          ' WHERE user_id = '.(int) $userId . 
  38.                          ' AND profile_key LIKE \'profile5.%\'' . 
  39.                          ' ORDER BY ordering' 
  40.                  ); 
  41.                  $results = $db->loadRowList(); 
  42.                  // Check for a database error. 
  43.                  if ($db->getErrorNum()) { 
  44.                          $this->_subject->setError($db->getErrorMsg()); 
  45.                          return false; 
  46.                  } 
  47.                  // Merge the profile data. 
  48.                  $data->profile5 = array(); 
  49.                  foreach ($results as $v) { 
  50.                          $k = str_replace('profile5.', '', $v[0]); 
  51.                          $data->profile5[$k] = $v[1]; 
  52.                  } 
  53.                  return true; 
  54.          } 
  55.   
  56.          /** 
  57.           * @param       JForm   The form to be altered. 
  58.           * @param       array   The associated data for the form. 
  59.           * @return      boolean 
  60.           * @since       1.6 
  61.           */ 
  62.          function onContentPrepareForm($form$data
  63.          { 
  64.                  // Load user_profile plugin language 
  65.                  $lang = JFactory::getLanguage(); 
  66.                  $lang->load('plg_user_profile5', JPATH_ADMINISTRATOR); 
  67.   
  68.                  if (!($form instanceof JForm)) { 
  69.                          $this->_subject->setError('JERROR_NOT_A_FORM'); 
  70.                          return false; 
  71.                  } 
  72.                  // Check we are manipulating a valid form. 
  73.                  if (!in_array($form->getName(), array('com_users.profile''com_users.registration','com_users.user','com_admin.profile'))) { 
  74.                          return true; 
  75.                  } 
  76.                  if ($form->getName()=='com_users.profile'
  77.                  { 
  78.                          // Add the profile fields to the form. 
  79.                          JForm::addFormPath(dirname(__FILE__).'/profiles'); 
  80.                          $form->loadFile('profile', false); 
  81.                          // Toggle whether the allergytime field is required. 
  82.                          if ($this->params->get('profile-require_allergytime', 1) > 0) { 
  83.                                  $form->setFieldAttribute('allergytime''required'$this->params->get('profile-require_allergytime') == 2, 'profile'); 
  84.                          } else { 
  85.                                  $form->removeField('allergytime''profile'); 
  86.                          } 
  87.                  } 
  88.   
  89.                  //In this example, we treat the frontend registration and the back end user create or edit as the same. 
  90.                  elseif ($form->getName()=='com_users.registration' || $form->getName()=='com_users.user' ) 
  91.                  { 
  92.                          // Add the registration fields to the form. 
  93.                          JForm::addFormPath(dirname(__FILE__).'/profiles'); 
  94.                          $form->loadFile('profile', false); 
  95.                          // Toggle whether the allergytime field is required. 
  96.                          if ($this->params->get('register-require_allergytime', 1) > 0) { 
  97.                                  $form->setFieldAttribute('allergytime''required'$this->params->get('register-require_allergytime') == 2, 'profile'); 
  98.                          } else { 
  99.                                  $form->removeField('allergytime''profile'); 
  100.                          } 
  101.                  } 
  102.          } 
  103.   
  104.          function onUserAfterSave($data$isNew$result$error
  105.          { 
  106.                  $userId        = JArrayHelper::getValue($data'id', 0, 'int'); 
  107.                  if ($userId && $result && isset($data['profile5']) && (count($data['profile5']))) 
  108.                  { 
  109.                          try 
  110.                          { 
  111.                                  $db = &JFactory::getDbo(); 
  112.                                  $db->setQuery('DELETE FROM #__user_profiles WHERE user_id = '.$userId.' AND profile_key LIKE \'profile5.%\''); 
  113.                                  if (!$db->query()) { 
  114.                                          throw new Exception($db->getErrorMsg()); 
  115.                                  } 
  116.                                  $tuples = array(); 
  117.                                  $order = 1; 
  118.                                  foreach ($data['profile5'] as $k => $v) { 
  119.                                          $tuples[] = '('.$userId.', '.$db->quote('profile5.'.$k).', '.$db->quote($v).', '.$order++.')'
  120.                                  } 
  121.                                  $db->setQuery('INSERT INTO #__user_profiles VALUES '.implode(', '$tuples)); 
  122.                                  if (!$db->query()) { 
  123.                                          throw new Exception($db->getErrorMsg()); 
  124.                                  } 
  125.                          } 
  126.                          catch (JException $e) { 
  127.                                  $this->_subject->setError($e->getMessage()); 
  128.                                  return false; 
  129.                          } 
  130.                  } 
  131.   
  132.                  return true; 
  133.          } 
  134.   
  135.          /** 
  136.           * Remove all user profile information for the given user ID 
  137.           * 
  138.           * Method is called after user data is deleted from the database 
  139.           * 
  140.           * @param       array           $user           Holds the user data 
  141.           * @param       boolean         $success        True if user was succesfully stored in the database 
  142.           * @param       string          $msg            Message 
  143.           */ 
  144.          function onUserAfterDelete($user$success$msg
  145.          { 
  146.                  if (!$success) { 
  147.                          return false; 
  148.                  } 
  149.                  $userId        = JArrayHelper::getValue($user'id', 0, 'int'); 
  150.                  if ($userId
  151.                  { 
  152.                          try 
  153.                          { 
  154.                                  $db = JFactory::getDbo(); 
  155.                                  $db->setQuery( 
  156.                                          'DELETE FROM #__user_profiles WHERE user_id = '.$userId . 
  157.                                          " AND profile_key LIKE 'profile5.%'
  158.                                  ); 
  159.   
  160.                                  if (!$db->query()) { 
  161.                                          throw new Exception($db->getErrorMsg()); 
  162.                                  } 
  163.                          } 
  164.                          catch (JException $e
  165.                          { 
  166.                                  $this->_subject->setError($e->getMessage()); 
  167.                                  return false; 
  168.                          } 
  169.                  } 
  170.                  return true; 
  171.          } 
  172.   } 
  173.   ?> 
./profile5/profile5.xml

  1.  <?xml version="1.0" encoding="utf-8"?> 
  2.          <!-- $Id:  --> 
  3.   <extension version="1.6" type="plugin" group="user"> 
  4.          <name>User - Profile - JoelLipman.Com</name> 
  5.          <author>Joel Lipman</author> 
  6.          <creationDate>April 2011</creationDate> 
  7.          <copyright>(C) 2011 JoelLipman.Com. All rights reserved.</copyright> 
  8.          <license>GNU General Public License version 2 or later; see LICENSE.txt</license> 
  9.          <authorEmail>info@joellipman.com</authorEmail> 
  10.          <authorUrl>www.joellipman.com</authorUrl> 
  11.          <version>1.6.0</version> 
  12.          <description>PLG_USER_PROFILE5_XML_DESCRIPTION</description> 
  13.   
  14.          <files> 
  15.                  <filename plugin="profile5">profile5.php</filename> 
  16.                  <filename>index.html</filename> 
  17.                  <folder>profiles</folder> 
  18.          </files> 
  19.   
  20.          <languages> 
  21.                  <language tag="en-GB">en-GB.plg_user_profile5.ini</language> 
  22.                  <language tag="en-GB">en-GB.plg_user_profile5.sys.ini</language> 
  23.          </languages> 
  24.   
  25.          <config> 
  26.                  <fields name="params"> 
  27.   
  28.                          <fieldset name="basic"> 
  29.                                  <field name="register-require-user" type="spacer" 
  30.                                          label="PLG_USER_PROFILE5_FIELD_NAME_REGISTER_REQUIRE_USER" 
  31.                                  /> 
  32.   
  33.                                  <field name="register-require_allergytime" type="list" 
  34.                                          description="PLG_USER_PROFILE5_FIELD_ALLERGYTIME_DESC" 
  35.                                          label="PLG_USER_PROFILE5_FIELD_ALLERGYTIME_LABEL" 
  36.                                  > 
  37.                                          <option value="2">JOPTION_REQUIRED</option> 
  38.                                          <option value="1">JOPTION_OPTIONAL</option> 
  39.                                          <option value="0">JDISABLED</option> 
  40.                                  </field> 
  41.   
  42.                                  <field name="profile-require-user" type="spacer" 
  43.                                          label="PLG_USER_PROFILE5_FIELD_NAME_PROFILE_REQUIRE_USER" 
  44.                                  /> 
  45.   
  46.                                  <field name="profile-require_allergytime" type="list" 
  47.                                          description="PLG_USER_PROFILE5_FIELD_ALLERGYTIME_DESC" 
  48.                                          label="PLG_USER_PROFILE5_FIELD_ALLERGYTIME_LABEL" 
  49.                                  > 
  50.                                          <option value="2">JOPTION_REQUIRED</option> 
  51.                                          <option value="1">JOPTION_OPTIONAL</option> 
  52.                                          <option value="0">JDISABLED</option> 
  53.                                  </field> 
  54.   
  55.                          </fieldset> 
  56.   
  57.                  </fields> 
  58.          </config> 
  59.   </extension> 

./profile5/en-GB.plg_user_profile5.ini

  1.  ; Joomla! Project 
  2.  ; Copyright (C) 2011 JoelLipman.com. All rights reserved. 
  3.  ; License http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL, see LICENSE.php 
  4.  ; Note : All ini files need to be saved as UTF-8 
  5.   
  6.  PLG_USER_PROFILE5_XML_DESCRIPTION="User Profile Plug-in by JoelLipman.Com to add extra fields to the user forms." 
  7.  PLG_USER_PROFILE5="User - Profile - JoelLipman.Com" 
  8.  PLG_USER_PROFILE5_SLIDER_LABEL="User Profile - JoelLipman.Com" 
  9.  PLG_USER_PROFILE5_FIELD_NAME_REGISTER_REQUIRE_USER="User profile fields for registration and administrator user profile - JoelLipman.Com forms" 
  10.  PLG_USER_PROFILE5_FIELD_NAME_PROFILE_REQUIRE_USER="User profile fields for profile - JoelLipman.Com edit form" 
  11.  PLG_USER_PROFILE5_FIELD_ALLERGYTIME_DESC="How long have you been dealing with this allergy?" 
  12.  PLG_USER_PROFILE5_FIELD_ALLERGYTIME_LABEL="Time With Allergy?" 
  13.  PLG_USER_PROFILE_FIELD_ALLERGYTIME_MESSAGE="Select one of the options" 
  14.  PLG_USER_PROFILE5_XML_DESCRIPTION="User Profile - JoelLipman.Com Plugin" 
  15.  JOELLIPMAN_BOOLEANYES="Yes" 
  16.  JOELLIPMAN_BOOLEANNO="No" 
  17.  JOELLIPMAN_ALLERGYTIMEOPTION0="Less than 6 months" 
  18.  JOELLIPMAN_ALLERGYTIMEOPTION1="Less than a year" 
  19.  JOELLIPMAN_ALLERGYTIMEOPTION2="1 year" 
  20.  JOELLIPMAN_ALLERGYTIMEOPTION3="5 years" 
  21.  JOELLIPMAN_ALLERGYTIMEOPTION4="10 years+" 
  22.  PLG_USER_PROFILE5_FIELD_ALLERGYTIMEDISP_DESC="Choose to hide from other users?" 
  23.  PLG_USER_PROFILE5_FIELD_ALLERGYTIMEDISP_LABEL="Hide from others?" 

./profile5/en-GB.plg_user_profile5.sys.ini

  1.  ; Joomla! Project 
  2.  ; Copyright (C) 2011 JoelLipman.com. All rights reserved. 
  3.  ; License http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL, see LICENSE.php 
  4.  ; Note : All ini files need to be saved as UTF-8 
  5.   
  6.  PLG_USER_PROFILE5="User - Profile - JoelLipman.com" 
  7.  PLG_USER_PROFILE5_XML_DESCRIPTION="User Profile Plug-in - JoelLipman.Com" 

./profile5/profiles/profile.xml

  1.  <?xml version="1.0" encoding="utf-8"?> 
  2.       <!-- $Id: profile.xml 20156 2011-04-27 13:10:16Z joellipman $ --> 
  3.  <form> 
  4.       <fields name="profile"> 
  5.            <fieldset name="profile"> 
  6.                 <field 
  7.                      name="allergytime" 
  8.                      type="list" 
  9.                      id="allergytime" 
  10.                      description="PLG_USER_PROFILE5_FIELD_ALLERGYTIME_DESC" 
  11.                      filter="string" 
  12.                      label="PLG_USER_PROFILE5_FIELD_ALLERGYTIME_LABEL" 
  13.                      message="PLG_USER_PROFILE_FIELD_ALLERGYTIME_MESSAGE" 
  14.                      > 
  15.                           <option value="0">JOELLIPMAN_ALLERGYTIMEOPTION0</option> 
  16.                           <option value="1">JOELLIPMAN_ALLERGYTIMEOPTION1</option> 
  17.                           <option value="2">JOELLIPMAN_ALLERGYTIMEOPTION2</option> 
  18.                           <option value="3">JOELLIPMAN_ALLERGYTIMEOPTION3</option> 
  19.                           <option value="4">JOELLIPMAN_ALLERGYTIMEOPTION4</option> 
  20.                      </field> 
  21.                 <field name="allergytimedisplay" type="list" 
  22.                           description="PLG_USER_PROFILE5_FIELD_ALLERGYTIMEDISP_DESC" 
  23.                           label="PLG_USER_PROFILE5_FIELD_ALLERGYTIMEDISP_LABEL" 
  24.                      > 
  25.                           <option value="1">JOELLIPMAN_BOOLEANYES</option> 
  26.                           <option value="0">JOELLIPMAN_BOOLEANNO</option> 
  27.                      </field> 
  28.   
  29.            </fieldset> 
  30.       </fields> 
  31.  </form> 

Comments   

Dee
# Dee Mon, 23rd May 2011
great tut!
was trying to find about a solution on how to have this bloody panel by default open... no luck so far... any idea?
Like | Dislike | +4 Reply | Reply with quote | Quote
Joseph
# Joseph Wed, 2nd November 2011
Hi,
Thanks for this tip. I was just wondering if we need to run a SQL script to create fields for the items on the form. I followed the instructions above. I can see the new fields on the form but when I register a user, the items are not saved. I think this might be a missing step. Please clarify. Also if you want to have a list example fetch list of departments in the database, do u have any tips to share?
Like | Dislike | -1 Reply | Reply with quote | Quote
Laurens
# Laurens Fri, 17th February 2012
Any luck with the saving of the info? I'm facing the same problem on a Joomla 2.5 website, it's not saving the extra user info.
Like | Dislike | +1 Reply | Reply with quote | Quote
Todd
# Todd Sun, 20th November 2011
Thanks! I was struggling with the stock joomla user profile sample. Your example got me on the right track.
Like | Dislike | -2 Reply | Reply with quote | Quote
Vaibhav Tripathy
# Vaibhav Thu, 22nd March 2012
This is amazing article. I really enjoyed it. Thanks for sharing with us. Check this helpful link too it also having nice post with wonderful explanation...
Custom Plugin in Joomla
http://docs.joomla.org/Plugin
http://extensions.joomla.org/extensions/edition/custom-code-in-content
Like | Dislike | 0 Reply | Reply with quote | Quote
Chuck
# Chuck Thu, 17th May 2012
Hi Joel, thanks for the great post! It works fine except for one problem. I added a new field called "Referral Source" as a drop-down with sources (Google, Yahoo, etc). When the user registers or edits their profile it works fine. The problem is when displaying back their saved profile it does not show their selection as "Google", but it displays the number "1", which is the database key for Google. How do I get it to convert the database key "1" to display "Google"?
Like | Dislike | 0 Reply | Reply with quote | Quote
F. Saskiano
# F. Mon, 17th September 2012
Hello. No problem installing your custom profile, which works fine EXCEPT it does not seem to save any data to the users table!

If I go into User Manager and change the profile info, then Save, when I leave the User Manager, then come back and look at the same user for whom I just modified the profile information, it shows the default data! I also don't see any new fields in any Joomla! table in the db where the new info might have been written. Am I missing something?
Like | Dislike | 0 Reply | Reply with quote | Quote
PtitPhilippe
# PtitPhilippe Tue, 26th March 2013
Hello Joel, I tried the plugin to add extra fields but never found the way to add pictures
do you know how to process?
Like | Dislike | 0 Reply | Reply with quote | Quote
# Webmaster Wed, 27th March 2013
Salut PtitPhilippe,

The image selector is nicer in Joomla1.6+, you need to modify the \model\form\product.xml

Code:
<field name="img_icon" type="media" directory="images" hide_none="1" size="40" label="MYLABEL" description="MYDESC" />


But personally I prefer to try to use gravatar and the identicon.
Like | Dislike | 0 Reply | Reply with quote | Quote
Add Comment

Name:

Email:

Website:

Message:


Latest Posts

  • Joes Quicklist Weblinks (JQW)

    • Mon 27-Oct-14
      Hi. I was able to download today with my laptop in a different location. Both computers using Ubuntu ...
      Webcrawler  
    • Sun 26-Oct-14
      Hmm... thanks Webcrawler! I can't replicate the error as all the links work for both my superuser ...
      Webmaster  
    • Sun 26-Oct-14
      Can't download new version. I get the same error message as Kat a few post's up.... CTRL+F5 does not ...
      Webcrawler  
    • Sat 25-Oct-14
      Hi Traveller, Apologies for the incredible delay as I have been focusing on another app in development.
      Webmaster
  • SSIS Script: convert UPPERCASE to Mixed-Case using TitleCase

    • Tue 28-Oct-14
      This post was immensely helpful, thank you. I used the code for the script component and made some ...
      Dave L .