1 of 2
1
Hacking the member templates to allow radio btns and checkboxes for custom profile fields
Posted: 06 September 2007 04:20 PM   [ Ignore ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  456
Joined  10-14-2005

So, I’m after a bit of advice from the Ellis Lab staff on whether it’s remotely possible to hack the member templates to allow for input (via registration form and edit profile form) of data through radio buttons and checkboxes?

I can see that in theory this could work, I did a quick test by adding a custom field and hard coding it in the registration form:

<input name="m_field_id_1" value="1" type="checkbox">

However, I’m at a bit of a loss as to how I might query that this custom field is of ‘checkbox type’ and display a checkbox with the correct value on the edit profile form?

Any pointers would be greatly appreciated.

[Mod Edit: Moved to the How To Forum]

 Signature 

Nathan Pitman

Nine Four Ltd - a member of the EEPro Network

Profile
 
 
Posted: 06 September 2007 10:21 PM   [ Ignore ]   [ # 1 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  11142
Joined  04-29-2002

Freeform example for checkboxes

In the case for freeform, if the item is checked, the value of on is returned for that form. If you do the same sort of thing for m_field_id_1 and add/edit a person you should be able to see what gets added to the table.

 Signature 

Quick Reference - EE Trial Options - EE Wiki - Docs for updating a build

Profile
MSG
 
 
Posted: 07 September 2007 01:36 AM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  456
Joined  10-14-2005

On is certainly what gets added, I’ve tried that. smile

However I’m interested to work out how I might be able to display the value and correct field type in the edit profile page. For example does the SQL module work within member templates? I’m thinking that as I will know the user_id I could do a custom select to find the field type (which I could manually modify in the exp_member_fields )…

 Signature 

Nathan Pitman

Nine Four Ltd - a member of the EEPro Network

Profile
 
 
Posted: 07 September 2007 08:29 AM   [ Ignore ]   [ # 3 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  11142
Joined  04-29-2002

You can use the query module to return results for *any* table in the database. (Even ones that aren’t related to EE if you have them.) But you can’t inside of the member templates.

Unless you wanted to build your own module/extension whatever to do so.

I usually use a weblog entry to hold member information. It’s a bit more robust.

 Signature 

Quick Reference - EE Trial Options - EE Wiki - Docs for updating a build

Profile
MSG
 
 
Posted: 10 September 2007 05:11 AM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  456
Joined  10-14-2005

I guess what I’m after is the ability to query the DB somehow within the member templates to find the value of the ‘m_field_type’ in ‘exp_member_fields’ such that I can manually alter the value to something like ‘checkbox’ and then present the correct form element on the edit profile page.

Is there ‘any’ way that this might be possible? I’m just slightly confused as to how I might record a user registration at the ‘same time’ as recording extra member data to a custom weblog. My project requirements dictate that the information must be recorded at the same time.

:?

 Signature 

Nathan Pitman

Nine Four Ltd - a member of the EEPro Network

Profile
 
 
Posted: 10 September 2007 10:54 AM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  456
Joined  10-14-2005

Sue, do you happen to know what files I should be looking through if I want to hand hack the values I need into the membership templates? Any guidance would be greatly appreciated as this is something I need to do both for this project and 2 others that I will be working on over the next couple of months.

 Signature 

Nathan Pitman

Nine Four Ltd - a member of the EEPro Network

Profile
 
 
Posted: 10 September 2007 10:55 AM   [ Ignore ]   [ # 6 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  21212
Joined  05-20-2002

It depends a bit on how deeply/cleanly you need the integration of the ‘checkbox’ type.  If it were me?  I’d leave them as text fields- and hard code a list of fields that need to be checkboxes.  Then- I’d hack member register and the edit profile code.  You MIGHT could do it via extension.  Anyway- for the edit page- look in modules/member/mod.member_settings.php- the edit_profile function.  For the registration page, look in mod.member_register.php.

You MIGHT be able to do it purely in the member templates if you hard code the custom fields- rather than loop through the {custom_fields} variable (or whatever it’s called).  I’d try that quickly first- just a test.  I’m not dead sure it would work, particularly for the edit page.  For registration, it likely would.

Of course- either option isn’t terribly dynamic, but once you’ve got your member fields set up, it doesn’t usually need to be all that dynamic.

 Signature 

AKA rob1

Help Request Tips * Pro Network

Profile
 
 
Posted: 10 September 2007 11:00 AM   [ Ignore ]   [ # 7 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  456
Joined  10-14-2005

Thanks very much Robin. I’ll have a look at this tonight and report back on how I get on. While I have your attention could I ask if you know how I might be able to include option profile fields like ‘Occupation’ etc in the registration form?

 Signature 

Nathan Pitman

Nine Four Ltd - a member of the EEPro Network

Profile
 
 
Posted: 10 September 2007 11:02 AM   [ Ignore ]   [ # 8 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  21212
Joined  05-20-2002

Sure- just make sure when you create or edit that custom profile field?  You select ‘yes’ for ‘Is field visible in registration page?’.  Should show up.

 Signature 

AKA rob1

Help Request Tips * Pro Network

Profile
 
 
Posted: 10 September 2007 01:46 PM   [ Ignore ]   [ # 9 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  456
Joined  10-14-2005

Sorry Robin, what I was referring to was the existing additional fields that a user can populate on the edit profile page; location, occupation, interests etc (as in exp_members). Is there some way to add these fields to the registration page rather than take the route of creating new custom profile fields for the exact same content just so that I can populate them via the registration step?

 Signature 

Nathan Pitman

Nine Four Ltd - a member of the EEPro Network

Profile
 
 
Posted: 10 September 2007 03:39 PM   [ Ignore ]   [ # 10 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  456
Joined  10-14-2005

Ok, I’ve hacked mod.member_settings.php to display checkbox type fields on the edit profile page. Here’s what I’ve done.

I’ve attached my copy of mod.member_settings.php for ease but for the record the additional code I have added is as follows. In the ‘edit_profile’ fucntion beneath the code snippet that renders pull down menus:

elseif ($row['m_field_type'] == 'checkbox')
{
/** ----------------------------------------
/**  Render checkbox fields
/** ----------------------------------------*/

    // Is checked?
    
if ($REGX->form_prep($field_data) == "y") {
        $field_checked
= " checked='checked'";
    
} else {
        $field_checked
= "";
    
}

    $input
= "<input type='checkbox' name='".'m_field_id_'.$row['m_field_id']."' id='".'m_field_id_'.$row['m_field_id']."' value='y' class='checkbox'".$field_checked." />";

    
$temp = str_replace('{lang:profile_field}', $required.$row['m_field_label'], $temp);
    
$temp = str_replace('{lang:profile_field_description}', $row['m_field_description'], $temp);
    
$temp = str_replace('{form:custom_profile_field}', $input, $temp);
}

And in the update_profile function immediately beneath the ‘Update the custom fields’ comment:

// Check for checkboxes without values
// Populate this array with the field name which are of checkbox type
$checkboxes = array("m_field_id_1");
foreach (
$checkboxes as $checkbox_field)
{
    
if (!isset($_POST[$checkbox_field])) {
        $_POST[$checkbox_field]
= "n";
    
}
}

Note that you will need to edit the ‘checkboxes’ array in snippet above to include the field names which you have added that are of the checkbox type. You will also need to manually change the m_field_type value for each field to ‘checkbox’ for this hack to work. I guess this could be improved by dynamically generating the array from the database, doing a select of all fields with a type of ‘checkbox’. If anyone wants to pitch in with that, feel welcome! smile

Let me know if you can see anywhere else that this can be improved. I’m no PHP guru by any stretch of the imagination! Now onto getting radio buttons to work… wink

Mod Edit: Attachment removed. Please review our license about distributing core files.

 Signature 

Nathan Pitman

Nine Four Ltd - a member of the EEPro Network

Profile
 
 
Posted: 10 September 2007 04:08 PM   [ Ignore ]   [ # 11 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  456
Joined  10-14-2005

Haha! I think I got radio buttons working too! Just add a new custom field, use the ‘select’ type and then manually change the field type in the DB to ‘radio’.

Now add the following to the ‘edit_profile’ function in mod.member_settings.php:

elseif ($row['m_field_type'] == 'radio')
{

    
/** ----------------------------------------
    /**  Render radio fields
    /** ----------------------------------------*/
                    
    
$group = "";
                
    foreach (
explode("\n", trim($row['m_field_list_items'])) as $v)
    
{
        $v
= trim($v);
        
$checked = ($field_data == $v) ? " checked='checked'" : '';
        
$group .= "".$v."&nbsp;<input type='radio' name='".'m_field_id_'.$row['m_field_id']."' id='".'m_field_id_'.$row['m_field_id']."' value='".$v."' class='radio'".$checked." />&nbsp;&nbsp;&nbsp;";                            
    
}
    $temp
= str_replace('{lang:profile_field}', $required.$row['m_field_label'], $temp);
    
$temp = str_replace('{lang:profile_field_description}', $row['m_field_description'],    $temp);
    
$temp = str_replace('{form:custom_profile_field}', $group, $temp);                
}

 Signature 

Nathan Pitman

Nine Four Ltd - a member of the EEPro Network

Profile
 
 
Posted: 10 September 2007 04:10 PM   [ Ignore ]   [ # 12 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  456
Joined  10-14-2005

Mod Edit: Attachment removed. Please review our license about distributing core files.

Sorry, my bad. So excited I didn’t stop to think about that. smile

 Signature 

Nathan Pitman

Nine Four Ltd - a member of the EEPro Network

Profile
 
 
Posted: 10 September 2007 04:45 PM   [ Ignore ]   [ # 13 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  456
Joined  10-14-2005

Ok, to complete this thread (unless I’ve missed anything) we need to add these new ‘checkbox’ and ‘radio’ type custom fields to the registration page. To do so you’ll need to add the following code snippet to the mod.member_register.php file in the registration_field function beneath the ‘drop down fields’ code segment (line 193):

elseif ($row['m_field_type'] == 'checkbox')
{  
    $field
= "<input type=\"checkbox\" name=\"m_field_id_".$row['m_field_id']."\" value=\"y\" class=\"checkbox\" />";
}

elseif ($row['m_field_type'] == 'radio')
{
    $items
= trim($row['m_field_list_items']);
    if (
$items != '')
    
{
        $group
= "";
        foreach (
explode("\n", $items) as $v)
        
{   
            $v
= trim($v);
            
$field .= "".$v."&nbsp;<input type='radio' name='".'m_field_id_'.$row['m_field_id']."' id='".'m_field_id_'.$row['m_field_id']."' value='".$v."' class='radio'".$checked." />&nbsp;&nbsp;&nbsp;";
        
}
    }
}

smile

 Signature 

Nathan Pitman

Nine Four Ltd - a member of the EEPro Network

Profile
 
 
Posted: 10 September 2007 04:47 PM   [ Ignore ]   [ # 14 ]  
Moderator
Avatar
RankRankRankRankRankRankRankRank
Total Posts:  31280
Joined  05-14-2004

So, Nathan, you’re going to add this in our wiki, right? You know you want to.

 Signature 
Profile
MSG
 
 
Posted: 10 September 2007 04:49 PM   [ Ignore ]   [ # 15 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  456
Joined  10-14-2005

Will do. smile

After the pointers regarding which files to mod, it was actually surprisingly easy. Makes me wonder why the functionality isn’t already there in EE proper if a muppet like myself can hack it???

 Signature 

Nathan Pitman

Nine Four Ltd - a member of the EEPro Network

Profile
 
 
Posted: 10 September 2007 05:27 PM   [ Ignore ]   [ # 16 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  456
Joined  10-14-2005

This thread is now summarised in a wiki article. Hope it reads ok!

Enable radio buttons and checkboxes for custom member profile fields.

 Signature 

Nathan Pitman

Nine Four Ltd - a member of the EEPro Network

Profile
 
 
Posted: 10 September 2007 05:42 PM   [ Ignore ]   [ # 17 ]  
Moderator
Avatar
RankRankRankRankRankRankRankRank
Total Posts:  31280
Joined  05-14-2004

Thank you, Nathan!

 Signature 
Profile
MSG
 
 
Posted: 11 September 2007 10:35 AM   [ Ignore ]   [ # 18 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  456
Joined  10-14-2005

Ok, one final (maybe) question for you. Is there ‘any’ way via a hack that I can display some content on the register and edit profile templates (profile_theme.php) when a certain custom field ID is the current custom field row.

For example, I wish to interrupt the flow of custom fields that EE spits out onto the page with a seperator.

 Signature 

Nathan Pitman

Nine Four Ltd - a member of the EEPro Network

Profile
 
 
   
1 of 2
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 10:33 AM
Total Registered Members: 61058 Total Logged-in Users: 25
Total Topics: 73867 Total Anonymous Users: 11
Total Replies: 398451 Total Guests: 470
Total Posts: 472318    
Members ( View Memberlist )
Newest Members:  tanjadebieSan2kakifemreSchottec2kmeenazterjinFuadgeorge_k_alliskhalidmhamood