diff --git a/src/Form/EntityInlineForm.php b/src/Form/EntityInlineForm.php index cf8c1d5..c94207f 100644 --- a/src/Form/EntityInlineForm.php +++ b/src/Form/EntityInlineForm.php @@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Entity\RevisionableInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Field\WidgetBase; use Drupal\Core\Form\FormStateInterface; @@ -239,6 +240,26 @@ class EntityInlineForm implements InlineFormInterface { if ($entity_form['#save_entity']) { // The entity was already validated in entityFormValidate(). $entity->setValidationRequired(FALSE); + if (!empty($item['needs_save'])) { + if ($entity instanceof RevisionableInterface) { + $entity->setNewRevision(); + + // If a new revision is created, save the current user as revision + // author. + if (in_array('Drupal\node\NodeInterface', class_implements($entity))) { + $entity->setRevisionAuthorId(\Drupal::currentUser()->id()); + $entity->setRevisionCreationTime(REQUEST_TIME); + } + elseif (in_array('Drupal\entity\Revision\EntityRevisionLogInterface', class_implements($entity))) { + $entity->setRevisionUserId(\Drupal::currentUser()->id()); + $entity->setRevisionCreationTime(REQUEST_TIME); + } + elseif (in_array('\Drupal\Core\Entity\RevisionLogInterface', class_implements($entity))) { + $entity->setRevisionUserId(\Drupal::currentUser()->id()); + $entity->setRevisionCreationTime(REQUEST_TIME); + } + } + } $this->save($entity); } // TODO - this is field-only part of the code. Figure out how to refactor. diff --git a/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php b/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php index 7bcccd1..c55ea7d 100644 --- a/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php +++ b/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php @@ -23,7 +23,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 * ) @@ -176,6 +177,7 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $settings = $this->getSettings(); $target_type = $this->getFieldSetting('target_type'); + // Get the entity type labels for the UI strings. $labels = $this->getEntityTypeLabels(); @@ -650,6 +652,9 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF $item = $value; if (isset($item['entity'])) { $item['target_id'] = $item['entity']->id(); + if ($item['entity'] instanceof RevisionableInterface) { + $item['target_revision_id'] = $item['entity']->getRevisionId(); + } $items[] = $item; } else { diff --git a/src/Plugin/Field/FieldWidget/InlineEntityFormSimple.php b/src/Plugin/Field/FieldWidget/InlineEntityFormSimple.php index 797bb9b..88a919f 100644 --- a/src/Plugin/Field/FieldWidget/InlineEntityFormSimple.php +++ b/src/Plugin/Field/FieldWidget/InlineEntityFormSimple.php @@ -16,7 +16,8 @@ use Drupal\Core\Render\Element; * id = "inline_entity_form_simple", * label = @Translation("Inline entity form - Simple"), * field_types = { - * "entity_reference" + * "entity_reference", + * "entity_reference_revisions" * }, * multiple_values = false * )