diff --git a/src/DefaultContentManager.php b/src/DefaultContentManager.php index 40acca2..2bbd335 100644 --- a/src/DefaultContentManager.php +++ b/src/DefaultContentManager.php @@ -7,6 +7,8 @@ use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityRepositoryInterface; +use Drupal\Core\Entity\RevisionableInterface; +use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Extension\InfoParserInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Session\AccountInterface; @@ -118,7 +120,7 @@ class DefaultContentManager implements DefaultContentManagerInterface { * The serializer service. * @param \Drupal\rest\Plugin\Type\ResourcePluginManager $resource_plugin_manager * The rest resource plugin manager. - * @param \Drupal\Core\Session|AccountInterface $current_user + * @param \Drupal\Core\Session\AccountInterface $current_user * The current user. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager * The entity type manager service. @@ -147,7 +149,7 @@ class DefaultContentManager implements DefaultContentManagerInterface { /** * {@inheritdoc} */ - public function importContent($module) { + public function importContent($module, $update_existing = FALSE) { $created = array(); $folder = drupal_get_path('module', $module) . "/content"; @@ -219,9 +221,31 @@ class DefaultContentManager implements DefaultContentManagerInterface { $contents = $this->parseFile($file); $class = $definition['serialization_class']; $entity = $this->serializer->deserialize($contents, $class, 'hal_json', array('request_method' => 'POST')); - $entity->enforceIsNew(TRUE); - $entity->save(); - $created[$entity->uuid()] = $entity; + $is_new = TRUE; + + // Allow existing entities overwrite. + if ($old_entity = $this->entityRepository->loadEntityByUuid($entity_type_id, $entity->uuid())) { + if ($update_existing) { + $original_id = $old_entity->id(); + $entity->{$entity->getEntityType()->getKey('id')} = $original_id; + $is_new = FALSE; + if ($this->isRevisionableEntity($entity)) { + $entity->setNewRevision(FALSE); + } + } + } + else { + if ($this->isRevisionableEntity($entity)) { + $entity->setNewRevision(TRUE); + } + } + + $is_new ? $entity->setOriginalId($original_id) : $entity->enforceIsNew($is_new); + + if (!$old_entity || $update_existing) { + $entity->save(); + $created[$entity->uuid()] = $entity; + } } } $this->eventDispatcher->dispatch(DefaultContentEvents::IMPORT, new ImportEvent($created, $module)); @@ -234,6 +258,19 @@ class DefaultContentManager implements DefaultContentManagerInterface { } /** + * Checks a given entity for revision support. + * + * @param EntityInterface $entity + * A typical drupal entity object. + * + * @return bool + * Whether this entity supports revisions. + */ + public function isRevisionableEntity(EntityInterface $entity) { + return $entity instanceof RevisionableInterface && $entity->getEntityType()->isRevisionable(); + } + + /** * {@inheritdoc} */ public function exportContent($entity_type_id, $entity_id) { diff --git a/src/DefaultContentManagerInterface.php b/src/DefaultContentManagerInterface.php index fdf67a9..9c63f22 100644 --- a/src/DefaultContentManagerInterface.php +++ b/src/DefaultContentManagerInterface.php @@ -20,11 +20,13 @@ interface DefaultContentManagerInterface { * * @param string $module * The module to create the default content for. + * @param bool $update_existing + * (optional) Force updating existing entities. Defaults to FALSE. * * @return array[\Drupal\Core\Entity\EntityInterface] * The created entities. */ - public function importContent($module); + public function importContent($module, $update_existing = FALSE); /** * Exports a single entity as importContent expects it.