ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Hacking the member templates to allow radio btns and checkboxes for custom profile fields

September 06, 2007 6:20pm

Subscribe [9]
  • #1 / Sep 06, 2007 6:20pm

    nathanpitman

    531 posts

    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]

  • #2 / Sep 07, 2007 12:21am

    Sue Crocker

    26054 posts

    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.

  • #3 / Sep 07, 2007 3:36am

    nathanpitman

    531 posts

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

    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 )...

  • #4 / Sep 07, 2007 10:29am

    Sue Crocker

    26054 posts

    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.

  • #5 / Sep 10, 2007 7:11am

    nathanpitman

    531 posts

    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.

    :?

  • #6 / Sep 10, 2007 12:54pm

    nathanpitman

    531 posts

    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.

  • #7 / Sep 10, 2007 12:55pm

    Robin Sowell

    13255 posts

    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.

  • #8 / Sep 10, 2007 1:00pm

    nathanpitman

    531 posts

    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?

  • #9 / Sep 10, 2007 1:02pm

    Robin Sowell

    13255 posts

    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.

  • #10 / Sep 10, 2007 3:46pm

    nathanpitman

    531 posts

    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?

  • #11 / Sep 10, 2007 5:39pm

    nathanpitman

    531 posts

    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! 😊

    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… 😉

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

  • #12 / Sep 10, 2007 6:08pm

    nathanpitman

    531 posts

    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." <input type='radio' name='".'m_field_id_'.$row['m_field_id']."' id='".'m_field_id_'.$row['m_field_id']."' value='".$v."' class='radio'".$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}', $group, $temp);                
    }
  • #13 / Sep 10, 2007 6:10pm

    nathanpitman

    531 posts

    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. 😊

  • #14 / Sep 10, 2007 6:45pm

    nathanpitman

    531 posts

    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." <input type='radio' name='".'m_field_id_'.$row['m_field_id']."' id='".'m_field_id_'.$row['m_field_id']."' value='".$v."' class='radio'".$checked." />   "; 
            } 
        }
    }

    😊

  • #15 / Sep 10, 2007 6:47pm

    Lisa Wess

    20502 posts

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

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases