From 04cd39aa28b686402098ac790e27d51596fd122d Mon Sep 17 00:00:00 2001
From: Michael Moritz <mimo@restoel.net>
Date: Sat, 6 Jan 2018 18:15:06 +0100
Subject: [PATCH] add handler for D7 comerce product refernce

---
 .../CommerceProductReferenceHandler.php       | 28 +++++++++++++++++++
 1 file changed, 28 insertions(+)
 create mode 100644 src/Drupal/Driver/Fields/Drupal7/CommerceProductReferenceHandler.php

diff --git a/src/Drupal/Driver/Fields/Drupal7/CommerceProductReferenceHandler.php b/src/Drupal/Driver/Fields/Drupal7/CommerceProductReferenceHandler.php
new file mode 100644
index 00000000..312263c6
--- /dev/null
+++ b/src/Drupal/Driver/Fields/Drupal7/CommerceProductReferenceHandler.php
@@ -0,0 +1,28 @@
+<?php
+
+namespace Drupal\Driver\Fields\Drupal7;
+
+/**
+ * CommerceProductReference field handler for Drupal 7.
+ */
+class CommerceProductReferenceHandler extends AbstractHandler {
+  /**
+   * {@inheritdoc}
+   */
+  public function expand($values) {
+    $entity_type = 'commerce_product'; // $this->fieldInfo['settings']['target_type'];
+    $entity_info = entity_get_info($entity_type);
+
+    $return = array();
+    foreach ($values as $value) {
+      $product_id = db_select($entity_info['base table'], 't')
+        ->fields('t', array($entity_info['entity keys']['id']))
+        ->condition('t.' . $entity_info['entity keys']['label'], $value)
+        ->execute()->fetchField();
+      if ($product_id) {
+        $return[$this->language][] = array('product_id' => $product_id);
+      }
+    }
+    return $return;
+  }
+}