From 023ff1bb6312693c8d264c6e73ec5e8748592471 Mon Sep 17 00:00:00 2001
From: David Lohmeyer <vilepickle@gmail.com>
Date: Tue, 12 Jan 2016 15:33:49 -0500
Subject: [PATCH] Made it so IEF will create revisions for
 entity_reference_revisions widgets if revisioning for the entity is enabled

---
 src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php | 14 +++++++++++++-
 src/Plugin/Field/FieldWidget/InlineEntityFormSimple.php  |  3 ++-
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php b/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php
index a7781ff..7c710f5 100644
--- a/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php
+++ b/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php
@@ -26,7 +26,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
  *   id = "inline_entity_form_complex",
  *   label = @Translation("Inline entity form - Complex"),
  *   field_types = {
- *     "entity_reference"
+ *     "entity_reference",
+ *     "entity_reference_revisions"
  *   },
  *   multiple_values = true
  * )
@@ -615,7 +616,14 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF
       foreach ($values as $delta => &$item) {
         /** @var \Drupal\Core\Entity\EntityInterface $entity */
         $entity = $item['entity'];
+        $is_revisionable = \Drupal::entityManager()->getStorage($entity->getEntityTypeId())->getEntityType()->isRevisionable();
         if (!empty($item['needs_save'])) {
+          if ($is_revisionable) {
+            $entity->setNewRevision();
+            // If a new revision is created, save the current user as revision author.
+            $entity->setRevisionCreationTime(REQUEST_TIME);
+            $entity->setRevisionAuthorId(\Drupal::currentUser()->id());
+          }
           $entity->save();
         }
         if (!empty($item['delete'])) {
@@ -652,7 +660,11 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF
     foreach ($values as $value) {
       $item = $value;
       if (isset($item['entity'])) {
+        $is_revisionable = \Drupal::entityManager()->getStorage($item['entity']->getEntityTypeId())->getEntityType()->isRevisionable();
         $item['target_id'] = $item['entity']->id();
+        if ($is_revisionable) {
+          $item['target_revision_id'] = $item['entity']->getRevisionId();
+        }
         $items[] = $item;
       }
     }
diff --git a/src/Plugin/Field/FieldWidget/InlineEntityFormSimple.php b/src/Plugin/Field/FieldWidget/InlineEntityFormSimple.php
index 553605c..8c3d56c 100644
--- a/src/Plugin/Field/FieldWidget/InlineEntityFormSimple.php
+++ b/src/Plugin/Field/FieldWidget/InlineEntityFormSimple.php
@@ -20,7 +20,8 @@ use Drupal\Core\Form\FormStateInterface;
  *   id = "inline_entity_form_simple",
  *   label = @Translation("Inline entity form - Simple"),
  *   field_types = {
- *     "entity_reference"
+ *     "entity_reference",
+ *     "entity_reference_revisions"
  *   },
  *   multiple_values = false
  * )
-- 
2.1.0

