Newsletter subscription in CIVICRM 4 – Drupal 7 combo
LeviaIT recently got me working on a very exciting project, modifying CIVICRM – Drupal combo to work for a certain niche market. And while i heard and read about CIVICRM before this was the first time working with it. And while everyone talks about CIVICRM and Drupal working nicely together, that it up to the point of them actually “working” together…
So one of the issues i came upon was lack of subscription form for the newsletter module built in in the CIVICRM. The inherent issue was more then just a missing form for integration, since CIVICRM newsletter module is meant to track who you send the newsletters to and what happens after that… that means that if anonymous user wants to signup to the newsletter, he would / should force him to fill out the full registration form first, which would be doable, but really annoying. So i wanted to share our solution:
- Add a custom field in user, with a yes / no selection (could be done by going to yoursite.com/civicrm/admin/custom/group?reset=1 )
- Next step is to create a smart group (smart groups automatically add users to a group based on a search criteria), based on if the value for “Receive the Newsletter” is “Yes”.
- Create a new webform with an email field. (check what the webform id is you will need it for the next step)
- wright a custom drupal module to do the rest of the magic. (explained below)
Now lets get to the module part:
As usual the first step is to create the hooks.info file:
|
1 2 3 4 5 6 7 |
name = Hooks
description = Implement CiviCRM and Drupal hooks specific to hineni
package = CiviCRM
dependencies[] = civicrm
dependencies[] = webform
core = 7.x
version = 0.1 |
Next is the hooks.module file:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
<!--?php</p-->
/**
* Implementation of hook_form_alter().
*/
function hooks_form_alter(&$form, &$form_state, $form_id) {
// Add validation for a particular Webform node:
if ($form_id == 'webform_client_form_8') {
// Add the submit handler after the existing Webform submit handler,
// but before the second Webform handler. Pop off the first one and add
// ours second.
$first = array_shift($form['#submit']);
array_unshift($form['#submit'], $first, 'hooks_submit_8');
}
}
/**
* Submit handler for Webform ID #8.
*/
function hooks_submit_8(&$form, &$form_state) {
// Insert things into other database tables or modify properties.
$submitted_tree = $form_state['values']['submitted_tree'];
$submitted_email = $submitted_tree['e_mail'];
// see if user email already in table
$rs = db_query("SELECT contact_id
FROM 'civicrm_email'
WHERE 'contact_id' IS NOT NULL
AND 'email' LIKE '$submitted_email'");
$contact = $rs->fetchObject();
if($contact->contact_id != ''){ // if in table
// get account id
$account_id = $contact->contact_id;
// pull up subscription table
$subscrip_table = db_query("SELECT id,'entity_id','would_you_like_to_receive_our_ne_69'
FROM 'civicrm_value_subscribe_to_newsletter_18'
WHERE 'entity_id' = '$account_id'");
$subscription = $subscrip_table->fetchObject();
if($subscription->id == ""){
// if user not in table, add contact to subscriptions
db_query("INSERT INTO 'civicrm_value_subscribe_to_newsletter_18'
('entity_id', 'would_you_like_to_receive_our_ne_69')
VALUES ('$account_id','1')");
}
elseif($subscription->id > 0 && $subscription->civicrm_value_subscribe_to_newsletter_18 == 0){
// if is in table but not subscribed, enable subscription
db_query("UPDATE 'civicrm_value_subscribe_to_newsletter_18'
SET 'would_you_like_to_receive_our_ne_69'='1'
WHERE 'id' = '$subscription->id'");
}
}
else { // if not in table
// create empty contact, filling in 'email_greeting_display' temporarily with user's email
db_query("INSERT INTO 'civicrm_contact'('contact_type','email_greeting_display') VALUES ('Individual','$submitted_email')");
// retrieving id of contact just created using email from 'email_greeting_display' column
$rs = db_query("SELECT id
FROM 'civicrm_contact'
WHERE 'email_greeting_display'
LIKE '$submitted_email'");
$contact = $rs->fetchObject();
$account_id = $contact->id;
// removed temporary storage of email address in 'email_greeting_display'
db_query("UPDATE 'civicrm_contact'
SET 'email_greeting_display' = NULL
WHERE 'id' = $account_id");
// add email address to email table
db_query("INSERT INTO 'civicrm_email'('contact_id', 'email', 'is_primary')
VALUES ('$account_id','$submitted_email','1')");
// add contact to subscription table
db_query("INSERT INTO 'civicrm_value_subscribe_to_newsletter_18'
('entity_id', 'would_you_like_to_receive_our_ne_69')
VALUES ('$account_id','1')");
}
}
?> |
Please be aware though that there is a couple of variables that you will need to find in your civicrm database and substitute those values in the code the list is as following:
- Webform ID as mentioned above – in my example the is was “webform_client_form_8″
- Look into database to find out the name of the table where the values for our custom nesletter field are stored – in our example the table name is “civicrm_value_subscribe_to_newsletter_18″
- In the above table the last collumn will be called something like this – “would_you_like_to_receive_our_ne_69″
Now all you need to do is substitute these values with values from your CIVICRM database, upload and activate the module. Your newsletter webform is ready!
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=aac6718d-bdde-442f-a025-abfc3398c727)

