CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • populating profile fields on load
Pages: [1]

Author Topic: populating profile fields on load  (Read 2921 times)

chrism

  • Guest
populating profile fields on load
August 13, 2008, 01:55:44 am
Is there a way to load a profile form with a command in the address line to pre-populate a field value?

For example, I'm testing group sign-ups using profile pages (http://forum.civicrm.org/index.php/topic,4335.0.html). A profile page has an address like:
http://sandbox.civicrm.org/civicrm/profile/edit?reset=1&gid=5
and all the fields are blank.

A compelling email sign-up approach is a front page block with a single "Email" field and a "Sign-up" button.  It would be useful to have the "Sign-up" button create a link to the profile page and copy the email address in.  For example:
http://sandbox.civicrm.org/civicrm/profile/edit?reset=1&gid=5&email=bob@example.com
that would give the same profile page, but with "bob@example.com" already entered in the Email field.

Thanks for your thoughts.
Chris

Kurund Jalmi

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4169
  • Karma: 128
    • CiviCRM
  • CiviCRM version: 4.x, future
  • CMS version: Drupal 7, Joomla 3.x
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: populating profile fields on load
August 13, 2008, 03:25:05 am
You can you do this with CiviCRM v2.1 (alpha), using buildForm() hook.

So basically for:
http://sandbox.civicrm.org/civicrm/profile/edit?reset=1&gid=5&email=bob@example.com

You can retrieve value for 'email',  and set to field that you want.

HTH

kurund
Found this reply helpful? Support CiviCRM

chrism

  • Guest
Re: populating profile fields on load
August 13, 2008, 07:05:18 am
Great.

I guess inserting that into a custom profile template should do the trick nicely.

Thank you.

petednz

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4899
  • Karma: 193
    • Fuzion
  • CiviCRM version: 3.x - 4.x
  • CMS version: Drupal 6 and 7
Re: populating profile fields on load
August 26, 2008, 01:25:22 am
Maybe same question from me.

I have just set up a series of Profiles with each one carrying on from the last - basically a survey - using the URL of the profile in the Advanced Settings in the redirect URL box (eg http://www3.greens.org.nz/civicrm/profile/edit&gid=44&reset=1). But I need some help so that the data that is collected in the second, third and fourth 'pages' is attached to the contact who has put their details in the first 'screen'. Am I missing something major or minor?
Sign up to StackExchange and get free expert advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

pete davis : www.fuzion.co.nz : connect + campaign + communicate

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: populating profile fields on load
August 26, 2008, 01:37:34 am

If they are logged in users, it should work as you describe, i.e. we update their records and retrieve info from their record for subsequent screens

for anon users, i dont think you get the same behavior. we've refrained from storing contactID in session. There is an issue for this which is currently scheduled for 2.2: http://issues.civicrm.org/jira/browse/CRM-3257

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

petednz

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4899
  • Karma: 193
    • Fuzion
  • CiviCRM version: 3.x - 4.x
  • CMS version: Drupal 6 and 7
Re: populating profile fields on load
August 26, 2008, 04:19:43 am
indeed it does for logged in users - will have to put it all on to one profile for non-logged in ones, which isn't as pretty.
Sign up to StackExchange and get free expert advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

pete davis : www.fuzion.co.nz : connect + campaign + communicate

chrism

  • Guest
Re: populating profile fields on load
August 29, 2008, 07:39:30 am
Here is the solution I've implemented to populate a field using a variable in the URL for a specific profile page. In this example loading a default value for a Primary Email field if there is a value in the url "email=bob@example.com"

1. Create a custom profile template http://wiki.civicrm.org/confluence/display/CRMDOC/Customize+Built-in+and+Profile+Screens
2. Add the following code into the end of the template file
Code: [Select]
<script type="text/javascript">
{literal}
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
urlEmailArg = gup("email");
if (document.forms["Edit"].elements["email-Primary"].value == "" && urlEmailArg != "") {
   document.forms["Edit"].elements["email-Primary"].value = defaultEmail;
}
{/literal}
</script>

The gup function (from here http://www.netlobo.com/url_query_string_javascript.html) is used to scan the URL for a name value pair. If the form value does not already have a value, as may be the case with a logged in user, and there exists a value in the URL then the form element is set.

This doesn't use the CiviCRM buildForm hook framework, but all the changes are encapsulated in the template file. Using hook would seem to require additional coding for a module, and then filtering on the default ID to only apply the change to the desired profile.

If anyone has a more elegant solution I'd be interested to hear, otherwise hope this may be useful for someone.

Chris

chrism

  • Guest
Re: populating profile fields on load
September 01, 2008, 03:43:36 am
I've been experimenting with use of the buildForm hook for setting default values and doing some more complicated adjustment to forms.  Really liking the potential of this hook, vs how customizations needed to be done in CiviCRM 1.x!

However, I'm a bit stuck figuring out how to set private variables of a form object. 

e.g.
I have a module with buildForm implemented and am checking for the loading of a specific profile form. Once I match  $formName="CRM_Profile_Form_Edit", and gid=X then I want to modify variables of the CRM_Profile_Form_Edit object. 

Specifically I want to modity _defaultValues, _postURL, _cancelURL, but these are private variables so can't be set with commands like
Code: [Select]
$form->_defaultValues['email-Primary'] = "me@example.com"
and I can't see an API to do this.

I found HTML::QuickForm->setDefaults to set _defaultValues, but after looking through the list of other Form methods I didn't see any way to get _defaultValues, or get or set the URL varibles?  Have I missed functions to do this or is there a need for some new form APIs to let people do more in their buildForm hooks.

Thanks
Chris




Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: populating profile fields on load
September 01, 2008, 01:01:16 pm

One possible alternative might be to to use PHP function overloading via __call (http://nz2.php.net/manual/en/language.oop5.overloading.php) and implement a get/set function for all protected variables. A reference article is here: http://www.onlamp.com/pub/a/php/2005/06/16/overloading.html

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

chrism

  • Guest
Re: populating profile fields on load
September 01, 2008, 07:20:39 pm
thanks lobo, good suggestion! 

this is a workable solution.  I added the following function to /CRM/Core/Form.php
Code: [Select]
    function __call($method, $arguments) {
        $prefix = strtolower(substr($method, 0, 6));
        $property = substr($method, 6);

        if (empty($prefix) || empty($property)) {
            return;
        }

        if ($prefix == "getvar" && isset($this->$property)) {
            return $this->$property;
        }

        if ($prefix == "setvar") {
            $this->$property = $arguments[0];
        }
    }

two differences from the example code are I retained case sensitivity in the properties and to avoid conflicts with existing functions chose 'getvar' prefix instead of 'get'

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • populating profile fields on load

This forum was archived on 2017-11-26.