2012-03-23

Joomla - create a profile plugin with custom fields

This plugin is based on the code found here:
http://docs.joomla.org/Creating_a_profile_plugin


Like I mentioned in an earlier post, where I showed how to add custom fields to the profile plugin that is already included with Joomla, the joomla example is very useful, but doesn't really get into adding new fields.  What I recommend is comparing that code with my code to see what I changed.  Also you can refer to my old post for some guidance.  I would use a diff tool like Meld.  To install in Fedora 16 just type:


yum install meld
in a command line terminal.


These files need to be created inside of your Joomla 2.5.4 installation.  The paths given are relative to the root of the installation.  These will not be overwritten during updates, because these names and paths do not exist in the original, but I do keep some backups, just in case.


The below 5 files comprise the entire plugin. First, create them all in their appropriate places on the server. (Just click the links below to download the files).


Then just go to the Extension Manager: Discover tab in the Joomla Administrator panel in the back end and click the blue "Discover" button on the upper right.  If you've done everything right, Joomla should see it listed as "Profile 7".  Check the box next to it and then click the yellow "Install" button on the upper right.  It should be ready to go.  You now should be able to find it in Joomla, Plug-in Manager: Plug-ins, and enable it.  Be sure and disable any other profile plugins you may have running, including the stock Joomla Profile plugin, or there could be conflicts OR they will both be displayed on your register and profile edit pages!

/administrator/language/en-GB/en-GB.plg_user_profile7.ini
/administrator/language/en-GB/en-GB.plg_user_profile7.sys.ini

(I want to point out that I originally posted the wrong path for the last file above as:
 /plugins/user/profile/profiles/profile.xml
I was missing a "7" there.  Thanks to Patrick for pointing that out, and sorry for any trouble this may have caused to anyone who was trying this.)

Please note, that the last file listed above is "profile.xml", not profile7 like the others.  This is VERY important.  Be careful!  These locations are in the Joomla installation.  Create them all with gedit or your text editor of choice (or just download them, now that I've posted links) and place them in their places on your server using an FTP client (or however you can get them there).  I use FileZilla.  (Sorry, but I don't have time to give a tut on using FTP or any other method of writing files to your server, maybe some other time.)

Here are the files.  I tried to highlight in blue the differences between my plugin and the standard plugin, I may have missed highlighting a "7" on a "Profile 7" here or there - they are peppered throughout all 5 files and finding and creating these inside the original code was the biggest challenge of the entire project.  (You see I couldn't just leave it with the name "profile" because then it would be overwritten every time you update, just like the standard plugin.) I will post both the content and links for download:
( NOTE:  I would LOVE to have these files posted in a prettier format.  If you happen to know a way to post frames inside of blogger or know anything that would make these look better, please let me know!)

en-GB.plg_user_profile7.ini (click for download)
-------------------------------------------------------------------------------------------------------------------

; Joomla! Project
; Copyright (C) 2005 - 2012 Open Source Matters. All rights reserved.
; License http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL, see LICENSE.php
; Note : All ini files need to be saved as UTF-8

PLG_USER_PROFILE7_XML_DESCRIPTION="User Profile 7 Plug-in"
PLG_USER_PROFILE7="User - Profile 7"
PLG_USER_PROFILE7_SLIDER_LABEL="User Profile 7"
PLG_USER_PROFILE7_FIELD_ABOUT_ME_DESC="Choose an option for the field About Me."
PLG_USER_PROFILE7_FIELD_ABOUT_ME_LABEL="About Me:"
PLG_USER_PROFILE7_FIELD_ADDRESS1_DESC="Choose an option for the field Address1"
PLG_USER_PROFILE7_FIELD_ADDRESS1_LABEL="Address 1:"
PLG_USER_PROFILE7_FIELD_ADDRESS2_DESC="Choose an option for the field Address2"
PLG_USER_PROFILE7_FIELD_ADDRESS2_LABEL="Address 2:"
PLG_USER_PROFILE7_FIELD_CITY_DESC="Choose an option for the field City"
PLG_USER_PROFILE7_FIELD_CITY_LABEL="City:"
PLG_USER_PROFILE7_FIELD_COUNTRY_DESC="Choose an option for the field Country"
PLG_USER_PROFILE7_FIELD_COUNTRY_LABEL="Country:"
PLG_USER_PROFILE7_FIELD_COUNTRY_MESSAGE="Country Required"
PLG_USER_PROFILE7_FIELD_DOB_DESC="Choose an option for the field Date of Birth"
PLG_USER_PROFILE7_FIELD_DOB_LABEL="Date of Birth:"
PLG_USER_PROFILE7_FIELD_FAVORITE_BOOK_DESC="Choose an option for the field Favourite Book"
PLG_USER_PROFILE7_FIELD_FAVORITE_BOOK_LABEL="Favourite Book:"
PLG_USER_PROFILE7_FIELD_NAME_PROFILE_REQUIRE_USER="User profile fields for Profile 7 edit form"
PLG_USER_PROFILE7_FIELD_NAME_REGISTER_REQUIRE_USER="User profile fields for registration and administrator user Profile 7 forms"
PLG_USER_PROFILE7_FIELD_PHONE_DESC="Choose an option for the field Phone"
PLG_USER_PROFILE7_FIELD_PHONE_LABEL="Phone:"
PLG_USER_PROFILE7_FIELD_POSTAL_CODE_DESC="Choose an option for the field Postal Code"
PLG_USER_PROFILE7_FIELD_POSTAL_CODE_LABEL="Postal / ZIP Code:"
PLG_USER_PROFILE7_FIELD_POSTAL_CODE_MESSAGE="Postal Code Required"
PLG_USER_PROFILE7_FIELD_REGION_DESC="Choose an option for the field Region"
PLG_USER_PROFILE7_FIELD_REGION_LABEL="Region:"
PLG_USER_PROFILE7_FIELD_TOS_DESC="Agree to terms of service"
PLG_USER_PROFILE7_FIELD_TOS_LABEL="Terms of Service"
PLG_USER_PROFILE7_FIELD_WEB_SITE_DESC="Choose an option for the field Web Site"
PLG_USER_PROFILE7_FIELD_WEB_SITE_LABEL="Web site:"
PLG_USER_PROFILE7_OPTION_AGREE="Agree"
PLG_USER_PROFILE7_OPTION_YES="YES"
PLG_USER_PROFILE7_FIELD_SUBSCRIBE_DESC="Subscribe to get emails about updates"
PLG_USER_PROFILE7_FIELD_SUBSCRIBE_LABEL="Receive Update Emails"
-------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------
; Joomla! Project
; Copyright (C) 2005 - 2012 Open Source Matters. All rights reserved.
; License http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL, see LICENSE.php
; Note : All ini files need to be saved as UTF-8

PLG_USER_PROFILE7="User - Profile 7"
PLG_USER_PROFILE7_XML_DESCRIPTION="User Profile 7 Plug-in"
-------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------

/**
 * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('JPATH_BASE') or die;

jimport('joomla.utilities.date');

jimport('joomla.user.helper');


jimport('joomla.user.user');


jimport('joomla.utilities.arrayhelper');


/**
 * An example custom profile plugin.
 *
 * @package Joomla.Plugin
 * @subpackage User.profile
 * @version 1.6
 */
class plgUserProfile7 extends JPlugin
{
/**
* Constructor
*
* @access      protected
* @param       object  $subject The object to observe
* @param       array   $config  An array that holds the plugin configuration
* @since       1.5
*/
public function __construct(& $subject, $config)
{
parent::__construct($subject, $config);
$this->loadLanguage();
}

/**
* @param string $context The context for the data
* @param int $data The user id
* @param object
*
* @return boolean
* @since 1.6
*/
function onContentPrepareData($context, $data)
{
// Check we are manipulating a valid form.
if (!in_array($context, array('com_users.profile', 'com_users.user', 'com_users.registration', 'com_admin.profile'))) {
return true;
}

if (is_object($data))
{
$userId = isset($data->id) ? $data->id : 0;

if (!isset($data->profile) and $userId > 0) {

// Load the profile data from the database.
$db = JFactory::getDbo();
$db->setQuery(
'SELECT profile_key, profile_value FROM #__user_profiles' .
' WHERE user_id = '.(int) $userId." AND profile_key LIKE 'profile7.%'" .
' ORDER BY ordering'
);
$results = $db->loadRowList();

// Check for a database error.
if ($db->getErrorNum())
{
$this->_subject->setError($db->getErrorMsg());
return false;
}

// Merge the profile data.
$data->profile7 = array();

foreach ($results as $v)
{
$k = str_replace('profile7.', '', $v[0]);
$data->profile7[$k] = json_decode($v[1], true);
if ($data->profile7[$k] === null)
{
$data->profile7[$k] = $v[1];
}
}
}

if (!JHtml::isRegistered('users.url')) {
JHtml::register('users.url', array(__CLASS__, 'url'));
}
if (!JHtml::isRegistered('users.calendar')) {
JHtml::register('users.calendar', array(__CLASS__, 'calendar'));
}
if (!JHtml::isRegistered('users.tos')) {
JHtml::register('users.tos', array(__CLASS__, 'tos'));
}
if (!JHtml::isRegistered('users.subscribe')) {
JHtml::register('users.subscribe', array(__CLASS__, 'subscribe'));
}
}

return true;
}

public static function url($value) { if (empty($value)) { return JHtml::_('users.value', $value); } else { $value = htmlspecialchars($value); if(substr ($value, 0, 4) == "http") { return '<a href="'.$value.'">'.$value.'</a>'; } else { return '<a href="http://'.$value.'">'.$value.'</a>'; } } }

public static function calendar($value)
{
if (empty($value)) {
return JHtml::_('users.value', $value);
} else {
return JHtml::_('date', $value, null, null);
}
}

public static function tos($value)
{
if ($value) {
return JText::_('JYES');
}
else {
return JText::_('JNO');
}
}

public static function subscribe($value)
{
if ($value) {
return JText::_('JYES');
}
else {
return JText::_('JNO');
}
}

/**
* @param JForm $form The form to be altered.
* @param array $data The associated data for the form.
*
* @return boolean
* @since 1.6
*/
function onContentPrepareForm($form, $data)
{

if (!($form instanceof JForm))
{
$this->_subject->setError('JERROR_NOT_A_FORM');
return false;
}

// Check we are manipulating a valid form.
$name = $form->getName();
if (!in_array($name, array('com_admin.profile', 'com_users.user', 'com_users.profile', 'com_users.registration'))) {
return true;
}

// Add the registration fields to the form.
JForm::addFormPath(dirname(__FILE__).'/profiles');
$form->loadFile('profile', false);

$fields = array(
'address1',
'address2',
'city',
'region',
'country',
'postal_code',
'phone',
'website',
'favoritebook',
'aboutme',
'tos',
'dob',
'subscribe',
);

foreach ($fields as $field) {
// Case using the users manager in admin
if ($name == 'com_users.user') {
// Remove the field if it is disabled in registration and profile
if ($this->params->get('register-require_' . $field, 1) == 0 &&
$this->params->get('profile-require_' . $field, 1) == 0) {
$form->removeField($field, 'profile7');
}
}
// Case registration
elseif ($name == 'com_users.registration') {
// Toggle whether the field is required.
if ($this->params->get('register-require_' . $field, 1) > 0) {
$form->setFieldAttribute($field, 'required', ($this->params->get('register-require_' . $field) == 2) ? 'required' : '', 'profile7');
}
else {
$form->removeField($field, 'profile7');
}
}
// Case profile in site or admin
elseif ($name == 'com_users.profile' || $name == 'com_admin.profile') {
// Toggle whether the field is required.
if ($this->params->get('profile-require_' . $field, 1) > 0) {
$form->setFieldAttribute($field, 'required', ($this->params->get('profile-require_' . $field) == 2) ? 'required' : '', 'profile7');
}
else {
$form->removeField($field, 'profile7');
}
}
}

return true;
}

function onUserAfterSave($data, $isNew, $result, $error)
{
$userId = JArrayHelper::getValue($data, 'id', 0, 'int');
//
//BEGIN CUSTOM CODE
//
//put this inside the onUserAfterSave function in profile7.php
// This code will check if the user clicked the radio button for email
// updates by checking the 'subscribe' boolean in the data
// then assign the user to that group (group 9) if true
$groupID = 9; //9 is the value for the subscriber group in the ACL
//DEBUG code that forces if statement to come up true:
//$subscribeFlag = 1;
if ($userId && (isset($data['profile7']['subscribe'])))
{
$subscribeFlag = ($data['profile7']['subscribe']);
if (!($subscribeFlag == 0)) 
{
try
{
JUserHelper::addUserToGroup($userId, $groupID);
}
catch (JException $e)
{
$this->_subject->setError($e->getMessage());
return false;
}
}
else
{
try
{
JUserHelper::removeUserFromGroup($userId, $groupID);
}
catch (JException $e)
{
$this->_subject->setError($e->getMessage());
return false;
}
}
}
//END CUSTOM CODE
//
if ($userId && $result && isset($data['profile7']) && (count($data['profile7'])))
{
try
{
//Sanitize the date
if (!empty($data['profile7']['dob'])) {
$date = new JDate($data['profile7']['dob']);
$data['profile7']['dob'] = $date->format('Y-m-d');
}

$db = JFactory::getDbo();
$db->setQuery(
'DELETE FROM #__user_profiles WHERE user_id = '.$userId .
" AND profile_key LIKE 'profile7.%'"
);

if (!$db->query()) {
throw new Exception($db->getErrorMsg());
}

$tuples = array();
$order = 1;

foreach ($data['profile7'] as $k => $v)
{
$tuples[] = '('.$userId.', '.$db->quote('profile7.'.$k).', '.$db->quote(json_encode($v)).', '.$order++.')';
}

$db->setQuery('INSERT INTO #__user_profiles VALUES '.implode(', ', $tuples));

if (!$db->query()) {
throw new Exception($db->getErrorMsg());
}

}
catch (JException $e)
{
$this->_subject->setError($e->getMessage());
return false;
}
}

return true;
}

/**
* Remove all user profile information for the given user ID
*
* Method is called after user data is deleted from the database
*
* @param array $user Holds the user data
* @param boolean $success True if user was succesfully stored in the database
* @param string $msg Message
*/
function onUserAfterDelete($user, $success, $msg)
{
if (!$success) {
return false;
}

$userId = JArrayHelper::getValue($user, 'id', 0, 'int');

if ($userId)
{
try
{
$db = JFactory::getDbo();
$db->setQuery(
'DELETE FROM #__user_profiles WHERE user_id = '.$userId .
" AND profile_key LIKE 'profile.%'"
);

if (!$db->query()) {
throw new Exception($db->getErrorMsg());
}
}
catch (JException $e)
{
$this->_subject->setError($e->getMessage());
return false;
}
}

return true;
}
}
----------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<extension version="2.5" type="plugin" group="user">
<name>plg_user_profile7</name>
<author>Joomla! Project</author>
<creationDate>January 2008</creationDate>
<copyright>(C) 2005 - 2012 Open Source Matters. All rights reserved.</copyright>
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
<authorEmail>admin@joomla.org</authorEmail>
<authorUrl>www.joomla.org</authorUrl>
<version>2.5.0</version>
<description>PLG_USER_PROFILE7_XML_DESCRIPTION</description>

<files>
<filename plugin="profile7">profile7.php</filename>
<filename>index.html</filename>
<folder>profiles</folder>
</files>
<languages>
<language tag="en-GB">en-GB.plg_user_profile7.ini</language>
<language tag="en-GB">en-GB.plg_user_profile7.sys.ini</language>
</languages>
<config>
<fields name="params">

<fieldset name="basic">
<field name="register-require-user" type="spacer" class="text"
label="PLG_USER_PROFILE7_FIELD_NAME_REGISTER_REQUIRE_USER"
/>

<field name="register-require_address1" type="list"

description="PLG_USER_PROFILE7_FIELD_ADDRESS1_DESC"
label="PLG_USER_PROFILE7_FIELD_ADDRESS1_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>

<field name="register-require_address2" type="list"

description="PLG_USER_PROFILE7_FIELD_ADDRESS2_DESC"
label="PLG_USER_PROFILE7_FIELD_ADDRESS2_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>

<field name="register-require_city" type="list"

description="PLG_USER_PROFILE7_FIELD_CITY_DESC"
label="PLG_USER_PROFILE7_FIELD_CITY_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>

<field name="register-require_region" type="list"

description="PLG_USER_PROFILE7_FIELD_REGION_DESC"
label="PLG_USER_PROFILE7_FIELD_REGION_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>

<field name="register-require_country" type="list"

description="PLG_USER_PROFILE7_FIELD_COUNTRY_DESC"
label="PLG_USER_PROFILE7_FIELD_COUNTRY_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>

<field name="register-require_postal_code" type="list"

description="PLG_USER_PROFILE7_FIELD_POSTAL_CODE_DESC"
label="PLG_USER_PROFILE7_FIELD_POSTAL_CODE_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>

<field name="register-require_phone" type="list"

description="PLG_USER_PROFILE7_FIELD_PHONE_DESC"
label="PLG_USER_PROFILE7_FIELD_PHONE_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>

<field name="register-require_website" type="list"

description="PLG_USER_PROFILE7_FIELD_WEB_SITE_DESC"
label="PLG_USER_PROFILE7_FIELD_WEB_SITE_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field name="register-require_favoritebook"
type="list"

label="PLG_USER_PROFILE7_FIELD_FAVORITE_BOOK_LABEL"
description="PLG_USER_PROFILE7_FIELD_FAVORITE_BOOK_DESC"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>

<field
name="register-require_aboutme"
type="list"

label="PLG_USER_PROFILE7_FIELD_ABOUT_ME_LABEL"
description="PLG_USER_PROFILE7_FIELD_ABOUT_ME_DESC"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field
name="register-require_tos"
type="list"

label="PLG_USER_PROFILE7_FIELD_TOS_LABEL"
description="PLG_USER_PROFILE7_FIELD_TOS_DESC"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field
name="register-require_subscribe"
type="list"

label="PLG_USER_PROFILE7_FIELD_SUBSCRIBE_LABEL"
description="PLG_USER_PROFILE7_FIELD_SUBSCRIBE_DESC">
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field
name="register-require_dob"
type="list"

label="PLG_USER_PROFILE7_FIELD_DOB_LABEL"
description="PLG_USER_PROFILE7_FIELD_DOB_DESC">
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>

<field name="spacer1" type="spacer"
hr="true"
/>
<field name="profile-require-user" type="spacer" class="text"
label="PLG_USER_PROFILE7_FIELD_NAME_PROFILE_REQUIRE_USER"
/>

<field name="profile-require_address1" type="list"

description="PLG_USER_PROFILE7_FIELD_ADDRESS1_DESC"
label="PLG_USER_PROFILE7_FIELD_ADDRESS1_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>

<field name="profile-require_address2" type="list"

description="PLG_USER_PROFILE7_FIELD_ADDRESS2_DESC"
label="PLG_USER_PROFILE7_FIELD_ADDRESS2_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>

<field name="profile-require_city" type="list"

description="PLG_USER_PROFILE7_FIELD_CITY_DESC"
label="PLG_USER_PROFILE7_FIELD_CITY_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>

<field name="profile-require_region" type="list"

description="PLG_USER_PROFILE7_FIELD_REGION_DESC"
label="PLG_USER_PROFILE7_FIELD_REGION_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>

<field name="profile-require_country" type="list"

description="PLG_USER_PROFILE7_FIELD_COUNTRY_DESC"
label="PLG_USER_PROFILE7_FIELD_COUNTRY_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>

<field name="profile-require_postal_code" type="list"

description="PLG_USER_PROFILE7_FIELD_POSTAL_CODE_DESC"
label="PLG_USER_PROFILE7_FIELD_POSTAL_CODE_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>

<field name="profile-require_phone" type="list"

description="PLG_USER_PROFILE7_FIELD_PHONE_DESC"
label="PLG_USER_PROFILE7_FIELD_PHONE_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>

<field name="profile-require_website" type="list"

description="PLG_USER_PROFILE7_FIELD_WEB_SITE_DESC"
label="PLG_USER_PROFILE7_FIELD_WEB_SITE_LABEL"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field name="profile-require_favoritebook"
type="list"

label="PLG_USER_PROFILE7_FIELD_FAVORITE_BOOK_LABEL"
description="PLG_USER_PROFILE7_FIELD_FAVORITE_BOOK_DESC">
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>

<field
name="profile-require_aboutme"
type="list"

label="PLG_USER_PROFILE7_FIELD_ABOUT_ME_LABEL"
description="PLG_USER_PROFILE7_FIELD_ABOUT_ME_DESC">
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field
name="profile-require_tos"
type="list"

label="PLG_USER_PROFILE7_FIELD_TOS_LABEL"
description="PLG_USER_PROFILE7_FIELD_TOS_DESC">
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field
name="profile-require_dob"
type="list"

label="PLG_USER_PROFILE7_FIELD_DOB_LABEL"
description="PLG_USER_PROFILE7_FIELD_DOB_DESC"
>
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
<field
name="profile-require_subscribe"
type="list"

label="PLG_USER_PROFILE7_FIELD_SUBSCRIBE_LABEL"
description="PLG_USER_PROFILE7_FIELD_SUBSCRIBE_DESC">
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>
</fieldset>

</fields>
</config>
</extension>

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<form>
<fields name="profile7">
<fieldset name="profile7"
label="PLG_USER_PROFILE7_SLIDER_LABEL"
>
<field
name="address1"
type="text"
id="address1"
description="PLG_USER_PROFILE7_FIELD_ADDRESS1_DESC"
filter="string"
label="PLG_USER_PROFILE7_FIELD_ADDRESS1_LABEL"
message="PLG_USER_PROFILE7_FIELD_ADDRESS1_MESSAGE"
size="30"
/>

<field
name="address2"
type="text"
id="address2"
description="PLG_USER_PROFILE7_FIELD_ADDRESS2_DESC"
filter="string"
label="PLG_USER_PROFILE7_FIELD_ADDRESS2_LABEL"
message="PLG_USER_PROFILE7_FIELD_ADDRESS1_MESSAGE"
size="30"
/>

<field
name="city"
type="text"
id="city"
description="PLG_USER_PROFILE7_FIELD_CITY_DESC"
filter="string"
label="PLG_USER_PROFILE7_FIELD_CITY_LABEL"
message="PLG_USER_PROFILE7_FIELD_CITY_MESSAGE"
size="30"
/>

<field
name="region"
type="text"
id="region"
description="PLG_USER_PROFILE7_FIELD_REGION_DESC"
filter="string"
label="PLG_USER_PROFILE7_FIELD_REGION_LABEL"
message="PLG_USER_PROFILE7_FIELD_REGION_MESSAGE"
size="30"
/>

<field
name="country"
type="text"
id="country"
description="PLG_USER_PROFILE7_FIELD_COUNTRY_DESC"
filter="string"
label="PLG_USER_PROFILE7_FIELD_COUNTRY_LABEL"
message="PLG_USER_PROFILE7_FIELD_COUNTRY_MESSAGE"
size="30"
/>

<field
name="postal_code"
type="text"
id="postal_code"
description="PLG_USER_PROFILE7_FIELD_POSTAL_CODE_DESC"
filter="string"
label="PLG_USER_PROFILE7_FIELD_POSTAL_CODE_LABEL"
message="PLG_USER_PROFILE7_FIELD_POSTAL_CODE_MESSAGE"
size="30"
/>

<field
name="phone"
type="tel"
id="phone"
description="PLG_USER_PROFILE7_FIELD_PHONE_DESC"
filter="string"
label="PLG_USER_PROFILE7_FIELD_PHONE_LABEL"
message="PLG_USER_PROFILE7_FIELD_PHONE_MESSAGE"
size="30"
/>

<field
name="website"
type="url"
id="website"
description="PLG_USER_PROFILE7_FIELD_WEB_SITE_DESC"
filter="string"
label="PLG_USER_PROFILE7_FIELD_WEB_SITE_LABEL"
message="PLG_USER_PROFILE7_FIELD_WEB_SITE_MESSAGE"
size="30"
/>
<field
name="favoritebook"
type="text"
description="PLG_USER_PROFILE7_FIELD_FAVORITE_BOOK_DESC"
filter="string"
label="PLG_USER_PROFILE7_FIELD_FAVORITE_BOOK_LABEL"
message="PLG_USER_PROFILE7_FIELD_FAVORITE_BOOK_MESSAGE"
size="30"
/>
<field
name="aboutme"
type="textarea"
description="PLG_USER_PROFILE7_FIELD_ABOUT_ME_DESC"
label="PLG_USER_PROFILE7_FIELD_ABOUT_ME_LABEL"
message="PLG_USER_PROFILE7_FIELD_ABOUT_ME_MESSAGE"
cols="30"
rows="5"
filter="safehtml"
/>
<field
name="subscribe"
type="radio"
default="0"
label="PLG_USER_PROFILE7_FIELD_SUBSCRIBE_LABEL"
description="PLG_USER_PROFILE7_FIELD_SUBSCRIBE_DESC">
<option value="0">PLG_USER_PROFILE7_OPTION_NO</option>
<option value="1">PLG_USER_PROFILE7_OPTION_YES</option>
</field>
<field
name="tos"
type="radio"
label="PLG_USER_PROFILE7_FIELD_TOS_LABEL"
description="PLG_USER_PROFILE7_FIELD_TOS_DESC">
<option value="1">PLG_USER_PROFILE7_OPTION_AGREE</option>
</field>

<field
name="dob"
type="calendar"
label="PLG_USER_PROFILE7_FIELD_DOB_LABEL"
description="PLG_USER_PROFILE7_FIELD_DOB_DESC">
</field>
</fieldset>
</fields>
</form>

------------------------------------------------------------------------------------------------------------

18 comments:

  1. Hello,
    I was creating my own Plugin with this manual. It works fine. I see my new Text-Field and the data was saved in the Database. But when I edit a user in the Frontend or Backend I can't see the data that I've insert before.
    Do you have any solution for me?

    ReplyDelete
  2. I'm not sure if I understand your question right. What sort of "data" are you referring to? You should be able to go to User Manager: Users, select a user and then view Basic Settings and User Info/Preferences on the right hand side of the interface. If the fields are not showing up there, then you may not have included them in the XML files properly. Check to make sure you have the appropriate xml included in the profiles/profile.xml folder. Does that help?

    ReplyDelete
    Replies
    1. Hello,
      I’m now one step farther. But from the beginning:
      I added a new field called "job". This works fine. I can see this field when I manage a user. Please see this link: http://www.patrickgrill.org/problem1.jpg
      When I write a value in this field and click "save", this value also will be saved in the database. Please see this link: http://www.patrickgrill.org/problem2.jpg
      And now the problem: I can’t see this value in the form-field "job" – only in the database. This is because the database request is

      SELECT profile_key, profile_value
      FROM *#!%_user_profiles
      WHERE user_id = *#
      AND profile_key LIKE 'profile.%'
      ORDER BY ordering

      and not

      SELECT profile_key, profile_value
      FROM *#!%_user_profiles
      WHERE user_id = *#
      AND profile_key LIKE 'profile7.%'
      ORDER BY ordering

      And this is because the file "plugins/user/profile/profile.php" is loaded and not the "plugins/user/profile7/ profile7.php". (When I edit the "normal" profile.php it works)

      But why is the "normal" profile.php loaded and not the profile7.php?

      Thank you very much for your help!

      Delete
    2. Hello!
      i solved the problem!
      You wrote an wrong patch in your manual. The correct patch from the profile.xml is "/plugins/user/profile7/profiles/" and not "/plugins/user/profile/profiles/".
      Best Regards

      Delete
  3. Thanks Patrick. My apologies. I was afraid I might do something like that. It appears I left out a 7 in a couple of places. I believe I have fixed the mistake. If there is anything else you saw that needs fixing, I'd love to know. Thanks man. Good looking out! Cheers!

    ReplyDelete
    Replies
    1. No problem. Thanks to you for this manual! Cheers!

      Delete
  4. Super informative post. I exactly find what I am looking about website development here. Thanks man…keep posting.

    ReplyDelete
  5. I have a question. How to put this on the frondend. It only appear in administrator.
    Thanks!

    ReplyDelete
  6. Is there anyone know how to put in frontend. I have registration in the site..

    ReplyDelete
  7. Well link4anything and Fritz, it's been months since I've played with this, and I can't figure why you wouldn't be seeing this on the front end. Of course you need to create a page and assign the plugin to it. You ARE seeing a form with all the other registration fields, right? If you are still having this problem, is there anything else you can tell me? PS. sorry for the delay was gone for the week.

    ReplyDelete
  8. Thanks for the tutorial!
    I'd like to change the groupid from 9 to 12. Is that possible and if so, how?

    In this section:

    //BEGIN CUSTOM CODE
    //
    //put this inside the onUserAfterSave function in profile7.php
    //This code will check if the user clicked the radio button for email
    // pdates by checking the 'subscribe' boolean in the data
    //then assign the user to that group (group 9) if true
    $groupID = 9; //9 is the value for the subscriber group in the ACL
    //DEBUG code that forces if statement to come up true:
    //$subscribeFlag = 1;

    ReplyDelete
    Replies
    1. You can make up any group that you like, but Joomla assigns the Group ID. In the Joomla Admin Backend just go to Users/Groups/Add New Group and input a name for your new group. Then go to the "User Manager: User Groups" screen and check the group ID in the far right column. In my case, it was 9. That is where the 9 comes from.

      Delete
  9. This comment has been removed by the author.

    ReplyDelete
  10. This is a great little plugin and works a charm but I'm having one problem where if I leave a required field blank and press register nothing happens...no error message is shown even though I gave a message value for the fields in the ini file. Also I've made sure that the line appears in the templates index.php file. Any idea how I can fix this? Thanks.

    ReplyDelete
    Replies
    1. I'm not sure what you mean by "nothing happens". Does the page just reload without accepting any entries? I haven't toyed with this in a minute and we are currently revamping the whole server where my Joomla installation, that uses this plugin, resides, so I can't test right now. Is there any more detailed information about this that you can give me?

      Delete
  11. I have added a couple of areas to my profile that use checkboxes, but it doesn't display the results in the profile (though it does remember the selections in the edit profile area) any ideas?

    ReplyDelete

Let me know what you think!