diff --git a/src/Form/EntityInlineForm.php b/src/Form/EntityInlineForm.php index 4dae436..5229906 100644 --- a/src/Form/EntityInlineForm.php +++ b/src/Form/EntityInlineForm.php @@ -8,10 +8,13 @@ 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\Entity\RevisionLogInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Field\WidgetBase; use Drupal\Core\Form\FormStateInterface; use Drupal\inline_entity_form\InlineFormInterface; +use Drupal\node\NodeInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -285,4 +288,47 @@ class EntityInlineForm implements InlineFormInterface { return EntityFormDisplay::collectRenderDisplay($entity, $form_mode); } + /** + * Determines wheter we should save a new revision. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The form state. + * + * @return bool + */ + protected function hasNewRevision(EntityInterface $entity, FormStateInterface $form_state) { + $new_revision = FALSE; + if ($entity instanceof RevisionableInterface && $entity->getEntityType()->isRevisionable()) { + if ($entity->isNewRevision()) { + $new_revision = TRUE; + } + + // Most of the time we don't know yet if the host entity is going to be + // saved as a new revision using RevisionableInterface::isNewRevision(). + // Most entity types (at least nodes) however use a boolean property named + // "revision" to indicate whether a new revision should be saved. Use that + // property. + elseif ($entity->getEntityType()->hasKey('revision') && $form_state->getValue('revision')) { + $new_revision = TRUE; + } + } + return $new_revision; + } + + /** + * {@inheritdoc} + */ + public function widgetSubmit($entity, $form, $form_state) { + if ($this->hasNewRevision($entity, $form_state) && $entity instanceof RevisionableInterface) { + $entity->setNewRevision(); + if ($entity instanceof RevisionLogInterface) { + // If a new revision is created, save the current user as revision author. + $entity->setRevisionUserId(\Drupal::currentUser()->id()); + $entity->setRevisionCreationTime(REQUEST_TIME); + } + } + } + } diff --git a/src/Form/NodeInlineForm.php b/src/Form/NodeInlineForm.php index 4f6c377..1ab5a9c 100644 --- a/src/Form/NodeInlineForm.php +++ b/src/Form/NodeInlineForm.php @@ -2,8 +2,10 @@ namespace Drupal\inline_entity_form\Form; +use Drupal\Core\Entity\RevisionableInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\node\NodeInterface; /** * Node inline form handler. @@ -57,4 +59,18 @@ class NodeInlineForm extends EntityInlineForm { return $entity_form; } + /** + * {@inheritdoc} + */ + public function widgetSubmit($entity, $form, $form_state) { + if ($this->hasNewRevision($entity, $form_state) && $entity instanceof RevisionableInterface) { + $entity->setNewRevision(); + if ($entity instanceof NodeInterface) { + $entity->setRevisionCreationTime(REQUEST_TIME); + // If a new revision is created, save the current user as revision author. + $entity->setRevisionAuthorId(\Drupal::currentUser()->id()); + } + } + } + } diff --git a/src/InlineFormInterface.php b/src/InlineFormInterface.php index c663602..2e3d395 100644 --- a/src/InlineFormInterface.php +++ b/src/InlineFormInterface.php @@ -126,4 +126,18 @@ interface InlineFormInterface extends EntityHandlerInterface { */ public function delete($ids, $context); + /** + * Submits the widget. + * + * This method is executed before ::save(). + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity. + * @param array $form + * The entity form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The form state of the parent form. + */ + public function widgetSubmit($entity, $form, $form_state); + } diff --git a/src/WidgetSubmit.php b/src/WidgetSubmit.php index 42f1c51..ddbe134 100644 --- a/src/WidgetSubmit.php +++ b/src/WidgetSubmit.php @@ -49,19 +49,8 @@ class WidgetSubmit { /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ $entity = $entity_item['entity']; $handler = InlineEntityForm::getInlineFormHandler($entity->getEntityTypeId()); - if ($entity instanceof RevisionableInterface && $entity->getEntityType()->isRevisionable()) { - $entity->setNewRevision(); - if ($entity instanceof NodeInterface) { - $entity->setRevisionCreationTime(REQUEST_TIME); - // If a new revision is created, save the current user as revision author. - $entity->setRevisionAuthorId(\Drupal::currentUser()->id()); - } - elseif ($entity instanceof RevisionLogInterface) { - // If a new revision is created, save the current user as revision author. - $entity->setRevisionUserId(\Drupal::currentUser()->id()); - $entity->setRevisionCreationTime(REQUEST_TIME); - } - } + + $handler->widgetSubmit($entity, $form, $form_state); $handler->save($entity); $widget_state['entities'][$delta]['needs_save'] = FALSE; }