diff --git a/plugins/selection/EntityReference_SelectionHandler_Views.class.php b/plugins/selection/EntityReference_SelectionHandler_Views.class.php
index 859e1e1..e987858 100644
--- a/plugins/selection/EntityReference_SelectionHandler_Views.class.php
+++ b/plugins/selection/EntityReference_SelectionHandler_Views.class.php
@@ -113,11 +113,26 @@ class EntityReference_SelectionHandler_Views implements EntityReference_Selectio
 
     $return = array();
     if ($result) {
+      // Check if view is grouped.
+      $handler = $this->view->display[$display_name]->handler;
+      if (!empty($handler->options['style_options']['grouping'][0]['field'])) {
+        $field = $handler->options['style_options']['grouping'][0]['field'];
+        $optgroup = $this->view->field[$field]->field_alias;
+        // Map entity ids to views result rows.
+        $rows = array_combine(array_keys($result), $this->view->result);
+      }
+
       $target_type = $this->field['settings']['target_type'];
       $entities = entity_load($target_type, array_keys($result));
       foreach($entities as $entity) {
         list($id,, $bundle) = entity_extract_ids($target_type, $entity);
-        $return[$bundle][$id] = $result[$id];
+        if (isset($optgroup)) {
+          $group_name = $rows[$id]->{$optgroup};
+          $return[$bundle][$group_name][$id] = $result[$id];
+        }
+        else {
+          $return[$bundle][$id] = $result[$id];
+        }
       }
     }
     return $return;
