Drupal/CiviCRM Integration
Example Use Case
This example is a one event page that has tabs of many types of content and views attached, and it allows an anonymous user to view the info, see registered participants, register for the event via one webform. This also automatically creates a contact, membership, active drupal user, and participant event registration to the event, logs in the user, and sets his user entered password via text field on the webform.
Using this method, you can create easily customizable membership joins and event registration pages, which leverages the full gauntlet of advanced Drupal 7 modules and the easiness of customizing the layout and style with css in the Drupal theme.
Views
Wiki instructions,http://wiki.civicrm.org/confluence/display/CRMDOC42/Views3+Integration, to give Drupal Views access to CiviCRM tables.
Entities
Drupal entities are data structures returned as objects and accessed and manipulated with object methods--full blown Object-oriented approach.
Excerpts from Intro to Drupal Entities Book Page - http://drupal.org/node/1261744
The Drupal community often compares site building through configuration to a favorite childhood toy: LEGO bricks. We can build Entity types, which can make Bundles, to which we can add Fields and then create Entities. This article explains the relationships between Entity types > Bundles > Fields > Entities. This was one of the most important changes of Drupal 7, and it brought components from some well-loved contributed modules -- such as CCK -- into the core system.
Entity types
In earlier versions of Drupal, the field system was only used on content types. Now, thanks to the Entity API, we can add fields to other things such as comments. Fieldable entities make Drupal eminently flexible. An entity type is a useful abstraction to group together fields. Let’s consider some examples of entity types:
- Nodes (content)
- Comments
- Taxonomy terms
- User profiles
You can also build new kinds of entity types where the options above don't suit your needs.
Eileen's CiviCRM Entity Module
Core CiviCRM drupal module exposes a few entities, mainly contacts, groups, and mailings. Exposes 11 CiviCRM data entities such as memberships, groups, participants, mailings, activities, pledges, events to views, rules, and entity reference modules as Drupal entities. Actually allows drupalized interaction with many more civicrm data structures including the tags,relationships, and custom data field sets via Relationships in views. http://drupal.org/sandbox/eileen/1923028
I hacked CiviCRM core views handling to add group relationships to views (For Civi 4.2, unnecessary in 4.3). See issue page for instructions.
Webform CiviCRM Module
From Project Page - http://drupal.org/project/webform_civicrm
This project brings the power and flexibility of Drupal Webforms to the CiviCRM community, allowing for tighter integration of your website and CRM database. This module can link any webform with CiviCRM, creating and updating contacts, group subscriptions, tags, relationships, cases, activities and event participants. Includes support for custom data set fields attached to contacts
How it's done
This document describes how to setup a Drupal content type of "Event", which will be used as a container to provide an extended interface for a CiviCRM event. Using fieldgroups, EVA, and Entity Reference, among the modules listed below, one can use site-building techniques to setup only a set of tabs--each with different associated information that a user may find interesting to have at their fingertips.
I created tabs for:
- The event information
- Travel Information (additional Drupal CT)
- Participant list
- Embedded Webform (allows for contact creation and event registration)
- Map (via Openlayers) (Registration via the webform will trigger php code with rules to create a synced drupal user, set the user to active, log the user in, set the password based on a webform field, create a free membership and sync the user's roles.
Modules Used
- core PHP Filter
- Views
- Entity Reference
- Enity Views Attachment(EVA)
- Field Group
- Rules
- Openlayers (for mapping)
- Webform
- Webform CiviCRM
- CiviCRM Entity Use Git to get latest version
Create the CiviCRM Event:
- Create Event via CiviCRM admin interface.
- Set Title, Description, Event Type: Exhibition, Free, Online Registration, Start and End Dates, Event Capacity, enter address, enter longitude and latitude coordinates in appropriate fields.
-
Creating CiviCRM Webform
- Create view with References views display that lists all CiviCRM events (How to create Entity Reference view).
- Create Drupal Content Type Event.
- Add entity reference field called Event Reference; choose to use view created first to give options to select list.
- Create Drupal Content Type Travel Information.
- Add exisiting entity reference field called Event Reference; choose to use view created first to give options to select list as well as other fields as desired.
- Add exisiting entity reference field called Event Reference to Webform Content Type.
- Create Drupal content Event node and set entity reference field to newly created CiviCRM event.
- Create Drupal content Travel Information node and set entity reference field to newly created CiviCRM event.
- Create Membership Type in CiviCRM titled "Free Member".
-
Setting up content data structures and content
- Create Webform node, title appropriately, enter body text, SET Event Reference field to the newly created CiviCRM event, save
- Click webform tab and add a regular webform text field for a password.
- Click CiviCRM tab on Webform node view page.
- Check checkbox labeled "Enable CiviCRM Processing".
-
Contact 1 Vertical Tab Settings
- Select Contact Type for Webform fields.
- If you want the webform to autopopulate with logged in user's CiviCRM contact data, leave "Exisiting Contact" checkbox checked.
- Check checkboxes for CiviCRM contact data fields to include in Webform.
- Include any Email, Phone, Tags, Relationships, Groups, Websites, or ANY CUSTOM DATA FIELD SETS THAT HAVE BEEN CREATED (COOOL!!).
- In this case, try checking the first and last name boxes under Contact Fields.
-
Event Registration Verical Tab Settings
- Select Register all contacts for same event.
- Select show events of type the same as the event that was created earlier.
- Select "Always" for Show Remaining Space in Events.
- Under Registration, Select 1 for Number of Event Sets (yes you can register for multiple events in one webform).
- Select the Event, Participant Role, and Registration status fields appropriately.
Create Views (The basics ignoring styling)
Create CiviCRM Event info EVA View:
- Create new view, list of CiviCRM events entities.
- Add EVA views display.
- In the Entity content settings category,
- set Entity to Node.
- set Bundle to Event.
- set Arguments to token with the value of [node:field-event-reference:id].
- Add Relationship: Entity Reference: Referencing entity.
- Add fields as you see fit.
- Add Contextual Filter: Content: Event Reference (field_event_reference).
- Relationship to use should be set automatically, but make sure that the Content: Event Reference is used.
- Under When the filter value is NOT available, set hide view.
- Add filter criteria Content: Type = Event.
- Save.
Create Participant Info EVA View (Create view of CiviCRM Participant Entities)
- Add EVA views display.
- In the Entity content settings category,
- set Entity to Node.
- set Bundle to Event.
- set Arguments to token with the value of [node:field-event-reference:id].
- Add Relationships:
- CiviCRM Participants: Event ID
- CiviCRM Participants: Participant's Contact ID
- Add Contextual Filter: CiviCRM Events: Event ID.
- Relationship to use should be set automatically but make sure that the CiviCRM Participants: Event ID Relationship is used.
- Under When the filter value is NOT available, set hide view.
- Add fields as you see appropriate.
- Save.
Create Registration Webform EVA
- Create view, list of Content of Type Webform.
- Add EVA views display.
- In the Entity content settings category,
- Set Entity to Node.
- Set Bundle to Event.
- Set Arguments to token with the value of [node:field-event-reference:id].
- Add Relationship Entity Reference: Event Reference.
- Add Contextual Filter: CiviCRM Events: Event ID.
- Relationship to use should be set automatically, but make sure that the Entity Reference: Event Reference Relationship is used.
- Under When the filter value is NOT available, set hide view
- Under Format: set to Rendered Entity, Full Content.
- Make sure you have a Filter Criteria: Type=Webform.
- Save.
- View for Travel Information is the same except the Filter Criteria should be Type=Travel Information.
Create Openlayers Map Custom Map
- Go to admin/structure/openlayers/maps/add.
- Set title to "Event Map".
- Save (to be continued).
Create Event Map EVA
- Create new view, list of CiviCRM events entities.
- Add EVA views display.
- In the Entity content settings category,
- Set Entity to Node.
- Set Bundle to Event.
- Leave Arguments set to id.
- Set Format to Openlayers Map.
- Set Map setting to Event Map.
- Add Contextual Filter: CiviCRM Events: Event ID.
- Under When the filter value is NOT available, set hide view.
- Add Openlayers Data Overlay views display to view.
- Set Display name: Event Location Point.
- Set Format:Show to Fields
- MAKE SURE AND SET THE FOR SELECT OPTION ABOVE TO "This openlayers (override)"
- Add fields:
- CiviCRM Address: Latitude
- CiviCRM Address: Longitude
- CiviCRM Events: Title
- Set Format to Openlayers Data Overlay.
- MAKE SURE AND SET THE FOR SELECT OPTION ABOVE TO "This openlayers (override)".
- Set settings to Format: OpenLayers Data Overlay | Settings.
- Set Map Data Sources to Lat/Lon Pair.
- Set latitude setting to CiviCRM Address: Latitude.
- Set longitude setting to CiviCRM Address: Longitude.
- Set Title setting to CiviCRM Events: Title.
- Apply settings and save view.
Create Openlayers Map Custom Map Continued
- Edit Event Map settings.
-
Under Layers and Styles tab:
- Choose base layer maps and choose a default.
- At the bottom of the list, there is a section called Openlayers layers.
- Find the one with Event Location Point in it.
- Check the Enabled and Activated checkboxes.
- Set pointers in Style and Select Style options.
- Click save and then edit again.
-
Under Behaviors Tab
- Under Tooltip for Features, check the Tooltip for Features checkbox.
- Also check the checkbox for the overlay view that we had just created.
- Under Zoom to Layer, check the Zoom to Layer checkbox.
- Also check the checkbox for the overlay view that we had just created.
- Save.
- Under Tooltip for Features, check the Tooltip for Features checkbox.
Manage Drupal Event content type Display
- Manage Display for event content type default display.
- Create horizontal tab group.
- Create 5 groups: Event Info, Travel Info, Participants, Register, and Map.
- Nest the groups in the horizontal tab group.
- You will see EVA fields have appeared since we created several EVA Views.
- Nest the Event EVA into Event Info, Participants EVA under Participant group, etc.
Rules, now we get jiggy
The Webform civicrm module always creates a contact from the webform submission.
Create rule Create Membership after contact creation
- Create free membership for webform, and CiviCRM event registration pages.
-
- Add rule.
- React to "CiviCRM Contact has been created."
- Add action Execute PHP code.
- Automagic member create code.
- Membership type id of 1 is for the Free Member type created in the beginning.
- Check the id for your membership type and change in code.
- Also change the join, start, and end dates.
- Generally you want the join and start dates to be whatever today is, or write some date code yourself.
-
Code: I know its CIvi API2 and not the best but it works for this example
require_once 'api/v2/Membership.php'; //require_once 'api/api.php'; $params = array( 'contact_id' => $civicrm_contact->id, 'membership_type_id' => '1', 'join_date' => '2013-06-13', 'start_date' => '2013-06-13', 'end_date' => '2014-12-21', 'is_override' => 1, 'status_id' => 1, ); $result = civicrm_contact_membership_create( $params );
-
Create rule Create Drupal user after webform submission.
-
- React to "Webform was submitted."
- Switch to "data selection" and leave form_id as the value in the "Data selector" field.
- Condition "Webform has name", select appropriate name.
- Add action execute php code.
-
Add Code:
$email = $data['components']['civicrm_1_contact_1_email_email']['value'][0]; $password = $data['components']['desired_password']['value'][0]; $params2 = array( 'name' => $email, 'mail' => $email, 'email' => $email, ); $errors = array(); $config = CRM_Core_Config::singleton(); $config->userSystem->checkUserNameEmailExists($params, $errors); if (! empty($errors)) { drupal_set_message(" could not create user record " . $errors['email']); return FALSE; } $params2['cms_name'] = $params2['name'] = $newuser['name'] = $params2['mail']; $params2['cms_pass'] = $newuser['pass'] = $password ; //substr(str_shuffle("abcefghijklmnopqrstuvwxyz"), 0, 8); $params2['status'] = 1; $params2['notify'] = TRUE; $newuser['uid'] = $config->userSystem->createUser($params2, 'email'); $newuser['mail'] = $params2['mail']; $user_object = (object) $newuser; CRM_Core_BAO_UFMatch::synchronizeUFMatch($user_object, $user_object->uid, $email, 'drupal', NULL, NULL, TRUE); $user_updated = db_update('users') ->fields(array( 'status' => 1, )) ->condition('uid', $user_object->uid, '=') ->execute(); global $user; $user = user_load( array('uid'=>$user_object->uid ) ); $pass = user_hash_password($password); db_query("UPDATE {users} SET pass = :pwd WHERE uid = :uid", array('pwd'=>$pass,':uid' =>$user->uid)); _civicrm_member_roles_sync($user->uid);
- creates user with username as email address, password set by webform textfield, sets user to active, logs them in and syncs roles