Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DDST-136: Create destination plugin for creating entities with entity revisions #130

Merged
merged 6 commits into from
Jun 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions src/Plugin/migrate/destination/DgiRevisionedEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Drupal\dgi_migrate\Plugin\migrate\destination;

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides a custom destination plugin for generating entity revisions.
*
* This class extends the EntityContentBase class to provide a custom
* destination plugin for Drupal migrations. It allows for the creation of
* revisions during the migration process, and maintains revision history.
*
* @see \Drupal\migrate\Plugin\migrate\destination\EntityContentBase
*
* @MigrateDestination(
* id = "dgi_revisioned_entity"
* )
*/
class DgiRevisionedEntity extends EntityContentBase {

/**
* The entity type.
*
* @var string
*/
public string $entityType;

/**
* The migration ID.
*
* @var string
*/
protected string $migrationId;

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
$entity_type = $configuration['entity_type'] ?? 'node';
$instance = parent::create($container, $configuration, 'entity:' . $entity_type, $plugin_definition, $migration);
$instance->entityType = $entity_type;
$instance->migrationId = $migration->id();
return $instance;
}

/**
* {@inheritdoc}
*/
public function import(Row $row, array $old_destination_id_values = []) {
$this->rollbackAction = MigrateIdMapInterface::ROLLBACK_DELETE;
$entity = $this->getEntity($row, $old_destination_id_values);

if (!$entity) {
throw new MigrateException('Unable to get entity');
}

assert($entity instanceof ContentEntityInterface);

if ($this->isEntityValidationRequired($entity)) {
$this->validateEntity($entity);
}

if (!$entity->isNew()) {
$entity->setNewRevision();
$entity->setRevisionCreationTime(time());

$revision_message = $this->generateRevisionMessage();
$entity->setRevisionLogMessage($revision_message);
}

$ids = $this->save($entity, $old_destination_id_values);

if ($this->isTranslationDestination()) {
$ids[] = $entity->language()->getId();
}

return $ids;
}

/**
* Generates a detailed revision message.
*
* @return string
* The generated revision message.
*/
protected function generateRevisionMessage() {
return sprintf('New revision generated by migration `%s`.', $this->migrationId);
}

}
Loading