Created
June 20, 2024 21:27
-
-
Save edutrul/a640751a7c1289c0ffb059fbef9136ea to your computer and use it in GitHub Desktop.
src/Service/CascadeMigrationService.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// File: src/Commands/CascadeMigrationCommands.php | |
namespace Drupal\custom_migration\Commands; | |
use Drush\Commands\DrushCommands; | |
use Drupal\custom_migration\Service\CascadeMigrationService; | |
/** | |
* A Drush command file for managing Cascade CMS migrations. | |
*/ | |
class CascadeMigrationCommands extends DrushCommands { | |
/** | |
* The migration service. | |
* | |
* @var \Drupal\custom_migration\Service\CascadeMigrationService | |
*/ | |
protected $migrationService; | |
/** | |
* Constructs a CascadeMigrationCommands object. | |
* | |
* @param \Drupal\custom_migration\Service\CascadeMigrationService $migrationService | |
* The migration service. | |
*/ | |
public function __construct(CascadeMigrationService $migrationService) { | |
$this->migrationService = $migrationService; | |
} | |
/** | |
* Run the migration for Cascade CMS pages. | |
* | |
* @command custom_migration:run | |
* @aliases cmr | |
*/ | |
public function runMigration() { | |
$this->migrationService->migratePages(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// File: src/Service/CascadeMigrationService.php | |
namespace Drupal\custom_migration\Service; | |
use Drupal\Core\Database\Connection; | |
use Drupal\node\Entity\Node; | |
use Drupal\Core\Entity\EntityTypeManagerInterface; | |
use Psr\Log\LoggerInterface; | |
/** | |
* Class CascadeMigrationService. | |
* | |
* Service for migrating pages from Cascade CMS to Drupal. | |
*/ | |
class CascadeMigrationService { | |
/** | |
* The database connection. | |
* | |
* @var \Drupal\Core\Database\Connection | |
*/ | |
protected $database; | |
/** | |
* The entity type manager. | |
* | |
* @var \Drupal\Core\Entity\EntityTypeManagerInterface | |
*/ | |
protected $entityTypeManager; | |
/** | |
* The logger service. | |
* | |
* @var \Psr\Log\LoggerInterface | |
*/ | |
protected $logger; | |
/** | |
* Constructs a CascadeMigrationService object. | |
* | |
* @param \Drupal\Core\Database\Connection $database | |
* The database connection. | |
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager | |
* The entity type manager. | |
* @param \Psr\Log\LoggerInterface $logger | |
* The logger service. | |
*/ | |
public function __construct(Connection $database, EntityTypeManagerInterface $entityTypeManager, LoggerInterface $logger) { | |
$this->database = $database; | |
$this->entityTypeManager = $entityTypeManager; | |
$this->logger = $logger; | |
} | |
/** | |
* Migrates pages from Cascade CMS to Drupal. | |
*/ | |
public function migratePages() { | |
$pages = $this->fetchCascadePages(); | |
foreach ($pages as $page) { | |
$this->migratePage($page); | |
} | |
$this->logger->info('Migration completed successfully.'); | |
} | |
/** | |
* Fetches all pages from Cascade CMS. | |
* | |
* @return array | |
* An array of pages from Cascade CMS. | |
*/ | |
protected function fetchCascadePages() { | |
$query = $this->database->select('cascade_pages', 'cp') | |
->fields('cp', ['id', 'title', 'content', 'parent_id']); | |
return $query->execute()->fetchAllAssoc('id'); | |
} | |
/** | |
* Migrates a single page from Cascade CMS to Drupal. | |
* | |
* @param object $page | |
* The page object from Cascade CMS. | |
*/ | |
protected function migratePage($page) { | |
if ($this->pageExists($page->id)) { | |
return; | |
} | |
// @todo: here goes even more logic for custom content type generation | |
$node = Node::create([ | |
'type' => 'page', | |
'title' => $page->title, | |
'body' => [ | |
'value' => $page->content, | |
'format' => 'full_html', | |
], | |
'field_cascade_id' => $page->id, | |
]); | |
if ($page->parent_id) { | |
$parent_id = $this->getDrupalParentId($page->parent_id); | |
if ($parent_id) { | |
$node->set('field_parent_page', $parent_id); | |
} | |
} | |
$node->save(); | |
$this->logger->info('Migrated page: ' . $page->title); | |
} | |
/** | |
* Checks if a page already exists in Drupal. | |
* | |
* @param int $cascade_id | |
* The Cascade CMS page ID. | |
* | |
* @return bool | |
* TRUE if the page exists, FALSE otherwise. | |
*/ | |
protected function pageExists($cascade_id) { | |
$query = $this->entityTypeManager->getStorage('node') | |
->getQuery() | |
->condition('type', 'page') | |
->condition('field_cascade_id', $cascade_id); | |
return (bool) $query->execute(); | |
} | |
// More code goes here... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
custom_migration.cascade_migration: | |
class: Drupal\custom_migration\Service\CascadeMigrationService | |
arguments: ['@database', '@entity_type.manager', '@logger.channel.default'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment