From 6156c48fee9cd222c7c8de8fe8baad57e008bb5a Mon Sep 17 00:00:00 2001
From: Aaron Bauman <aaron@messageagency.com>
Date: Fri, 6 Oct 2017 16:53:41 -0400
Subject: [PATCH] - Fix pull exception in Related IDs when pulling an empty
 lookup value - Add httpRequestRaw to RestClient for fetching binary data

---
 .../SalesforceMappingField/RelatedIDs.php     | 11 ++++++++++-
 src/Rest/RestClient.php                       | 19 +++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RelatedIDs.php b/modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RelatedIDs.php
index 57494681..b8ba9e9d 100644
--- a/modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RelatedIDs.php
+++ b/modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RelatedIDs.php
@@ -89,10 +89,19 @@ class RelatedIDs extends SalesforceMappingFieldPluginBase {
     }
 
     $value = $sf_object->field($this->config('salesforce_field'));
+    // Empty value means nothing to do here.
+    if (empty($value)) {
+      return NULL;
+    }
 
     // If value is not an SFID, make it one.
     if (!($value instanceof SFID)) {
-      $value = new SFID($value);
+      try {
+        $value = new SFID($value);
+      }
+      catch (\Exception $e) {
+        return NULL;
+      }
     }
 
     // Convert SF Id to Drupal Id.
diff --git a/src/Rest/RestClient.php b/src/Rest/RestClient.php
index 264cc999..d76c1fa3 100644
--- a/src/Rest/RestClient.php
+++ b/src/Rest/RestClient.php
@@ -205,6 +205,25 @@ class RestClient implements RestClientInterface {
     return $this->httpRequest($url, $data, $headers, $method);
   }
 
+  /**
+   * Return raw response content from given URL. Useful for fetching data from
+   * binary fields like Attachments.
+   *
+   * @param string $url
+   * @return mixed
+   */
+  public function httpRequestRaw($url) {
+    if (!$this->getAccessToken()) {
+      throw new \Exception('Missing OAuth Token');
+    }
+    $headers = [
+      'Authorization' => 'OAuth ' . $this->getAccessToken(),
+      'Content-type' => 'application/json',
+    ];
+    $response = $this->httpRequest($url, NULL, $headers);
+    return $response->getBody()->getContents();
+  }
+
   /**
    * Make the HTTP request. Wrapper around drupal_http_request().
    *
-- 
GitLab