From 67c998f4c34d1a0043a1b7d103de67390c8e893f Mon Sep 17 00:00:00 2001
From: Jitendra Purohit <jitendra@fuzion.co.nz>
Date: Tue, 11 Jul 2017 08:59:07 +0530
Subject: [PATCH] CRM-20834 - Drupal user not created via Profile due to
 missing email

---
 CRM/Contribute/Form/Contribution/Confirm.php | 8 ++++++++
 CRM/Contribute/Form/ContributionBase.php     | 2 ++
 2 files changed, 10 insertions(+)

diff --git a/CRM/Contribute/Form/Contribution/Confirm.php b/CRM/Contribute/Form/Contribution/Confirm.php
index 419f94912938..85a9130faf3b 100644
--- a/CRM/Contribute/Form/Contribution/Confirm.php
+++ b/CRM/Contribute/Form/Contribution/Confirm.php
@@ -2046,6 +2046,14 @@ protected function processFormSubmission($contactID) {
     if (!empty($this->_ccid)) {
       $this->_params['contribution_id'] = $this->_ccid;
     }
+    //Set email-bltID if pre/post profile contains an email.
+    if ($this->_emailExists == TRUE) {
+      foreach ($this->_params as $key => $val) {
+        if (substr($key, 0, 6) == 'email-' && empty($this->_params["email-{$this->_bltID}"])) {
+          $this->_params["email-{$this->_bltID}"] = $this->_params[$key];
+        }
+      }
+    }
     // add a description field at the very beginning
     $this->_params['description'] = ts('Online Contribution') . ': ' . (($this->_pcpInfo['title']) ? $this->_pcpInfo['title'] : $this->_values['title']);
 
diff --git a/CRM/Contribute/Form/ContributionBase.php b/CRM/Contribute/Form/ContributionBase.php
index fe5b484c0d79..cffecc784e36 100644
--- a/CRM/Contribute/Form/ContributionBase.php
+++ b/CRM/Contribute/Form/ContributionBase.php
@@ -227,6 +227,7 @@ public function preProcess() {
       // lets just bump this to a regular session error and redirect user to main page
       $this->controller->invalidKeyRedirect();
     }
+    $this->_emailExists = $this->get('emailExists');
 
     // this was used prior to the cleverer this_>getContactID - unsure now
     $this->_userID = CRM_Core_Session::singleton()->getLoggedInContactID();
@@ -652,6 +653,7 @@ public function buildCustom($id, $name, $viewOnly = FALSE, $profileContactType =
               !in_array($profileContactType, array('honor', 'onbehalf'))
           ) {
             $this->_emailExists = TRUE;
+            $this->set('emailExists', TRUE);
           }
         }