Skip to content

Commit

Permalink
Add migration manager, which will clean up migrations records in orde…
Browse files Browse the repository at this point in the history
…r to keep everything in clean state.
  • Loading branch information
LOBsTerr committed Jan 27, 2021
1 parent 2f913a9 commit 11e423f
Show file tree
Hide file tree
Showing 18 changed files with 289 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* @file
* Provides newsroom_connector_country functionality.
*/

use Drupal\newsroom_connector_country\Plugin\newsroom\NewsroomCountryNewsroomProcessor;

/**
* Implements hook_entity_delete().
*/
function newsroom_connector_country_taxonomy_term_delete(Drupal\Core\Entity\EntityInterface $entity) {
if ($entity->bundle() == 'newsroom_country') {
// Clean up migrations.
\Drupal::service('newsroom_connector.migration_manager')->cleanUpMigrations(NewsroomCountryNewsroomProcessor::MIGRATION_COUNTRY, $entity);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@
*/
class NewsroomCountryNewsroomProcessor extends NewsroomProcessorBase {

const MIGRATION_COUNTRY = 'newsroom_country';

}
23 changes: 21 additions & 2 deletions modules/newsroom_connector_item/newsroom_connector_item.module
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
<?php

/**
* @file
* Provides newsroom_connector_item functionality.
*/

use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\MigrateSourceInterface;
use Drupal\migrate\Row;
use Drupal\Core\Link;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Url;
use Drupal\newsroom_connector_item\Plugin\newsroom\NewsroomItemNewsroomProcessor;

/**
* Implements hook_migrate_prepare_row().
Expand Down Expand Up @@ -138,9 +144,22 @@ function newsroom_connector_item_node_view(
/**
* Implements hook_entity_delete().
*/
function newsroom_connector_item_node_delete(Drupal\Core\Entity\EntityInterface $entity) {
function newsroom_connector_item_node_delete(EntityInterface $entity) {
if ($entity->bundle() == 'newsroom_item') {
if ($media = \Drupal::service('entity_type.manager')->getStorage('media')->load($entity->field_newsroom_image->target_id)) {

// Clean up migrations.
$migration_manager = \Drupal::service('newsroom_connector.migration_manager');
$migration_manager->cleanUpMigrations(NewsroomItemNewsroomProcessor::MIGRATION_ITEM, $entity);

// Remove media.
if ($media = $entity->field_newsroom_image->entity) {

// Clean up migrations.
$migration_manager->cleanUpMigrations(NewsroomItemNewsroomProcessor::MIGRATION_ITEM_IMAGE_MEDIA, $media);
if ($file = $media->field_media_image_newsroom->entity) {
$migration_manager->cleanUpMigrations(NewsroomItemNewsroomProcessor::MIGRATION_ITEM_IMAGE, $file);
}

$media->delete();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@
*/
class NewsroomItemNewsroomProcessor extends NewsroomProcessorBase {

// Newsroom item importers.
const MIGRATION_ITEM = 'newsroom_item';
const MIGRATION_ITEM_IMAGE_MEDIA = 'newsroom_item_image_media';
const MIGRATION_ITEM_IMAGE = 'newsroom_item_image';
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
function newsroom_connector_media_media_delete(Drupal\Core\Entity\EntityInterface $entity) {
if ($entity->bundle() == 'newsroom_image') {
if ($file = \Drupal::service('entity_type.manager')->getStorage('file')->load($entity->field_media_image_newsroom->target_id)) {
if ($file = $entity->field_media_image_newsroom->entity) {
$file->delete();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
<?php

/**
* @file
* Provides newsroom_connector_service functionality.
*/

use Drupal\newsroom_connector_service\Plugin\newsroom\NewsroomServiceNewsroomProcessor;

/**
* Implements hook_entity_delete().
*/
function newsroom_connector_service_taxonomy_term_delete(Drupal\Core\Entity\EntityInterface $entity) {
if ($entity->bundle() == 'newsroom_service') {
if ($media = \Drupal::service('entity_type.manager')->getStorage('media')->load($entity->field_newsroom_service_image->target_id)) {
// Clean up migrations.
$migration_manager = \Drupal::service('newsroom_connector.migration_manager');
$migration_manager->cleanUpMigrations(NewsroomServiceNewsroomProcessor::MIGRATION_SERVICE, $entity);

if ($media = $entity->field_newsroom_service_image->entity) {
$migration_manager->cleanUpMigrations(NewsroomServiceNewsroomProcessor::MIGRATION_SERVICE_IMAGE_MEDIA, $media);
if ($file = $media->field_media_image_newsroom->entity) {
$migration_manager->cleanUpMigrations(NewsroomServiceNewsroomProcessor::MIGRATION_SERVICE_IMAGE, $file);
}
$media->delete();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@
*/
class NewsroomServiceNewsroomProcessor extends NewsroomProcessorBase {

const MIGRATION_SERVICE = 'newsroom_service';
const MIGRATION_SERVICE_IMAGE_MEDIA = 'newsroom_service_logo_media';
const MIGRATION_SERVICE_IMAGE = 'newsroom_service_logo';

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* @file
* Provides newsroom_connector_team_responsible functionality.
*/

use Drupal\newsroom_connector_team_responsible\Plugin\newsroom\NewsroomTeamResponsibleNewsroomProcessor;

/**
* Implements hook_entity_delete().
*/
function newsroom_connector_team_responsible_taxonomy_term_delete(Drupal\Core\Entity\EntityInterface $entity) {
if ($entity->bundle() == 'newsroom_team_responsible') {
// Clean up migrations.
\Drupal::service('newsroom_connector.migration_manager')->cleanUpMigrations(NewsroomTeamResponsibleNewsroomProcessor::MIGRATION_TEAM_RESPONSIBLE, $entity);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
*/
class NewsroomTeamResponsibleNewsroomProcessor extends NewsroomProcessorBase {

const MIGRATION_TEAM_RESPONSIBLE = 'team_responsible';
}
19 changes: 19 additions & 0 deletions modules/newsroom_connector_topic/newsroom_connector_topic.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* @file
* Provides newsroom_connector_topic functionality.
*/

use Drupal\newsroom_connector_topic\Plugin\newsroom\NewsroomTopicNewsroomProcessor;

/**
* Implements hook_entity_delete().
*/
function newsroom_connector_topic_taxonomy_term_delete(Drupal\Core\Entity\EntityInterface $entity) {
if ($entity->bundle() == 'newsroom_topic') {
// Clean up migrations.
\Drupal::service('newsroom_connector.migration_manager')->cleanUpMigrations(NewsroomTopicNewsroomProcessor::MIGRATION_TOPIC, $entity);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@
*/
class NewsroomTopicNewsroomProcessor extends NewsroomProcessorBase {

const MIGRATION_TOPIC = 'newsroom_topic';

}
14 changes: 6 additions & 8 deletions modules/newsroom_connector_type/newsroom_connector_type.module
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
* Provides newsroom_connector_type functionality.
*/

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\newsroom_connector_type\Plugin\newsroom\NewsroomTypeNewsroomProcessor;

/**
* Implements hook_help().
* Implements hook_entity_delete().
*/
function newsroom_connector_type_help($route_name, RouteMatchInterface $route_match) {
if ($route_name == 'help.page.token') {
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The <a href=":project">Newsroom Connector Type</a> module provides a newsroom item taxonomy vocabulary and importer for it. This module is part of newsroom connector') . '</p>';
$output .= '<dl>';
return $output;
function newsroom_connector_type_taxonomy_term_delete(Drupal\Core\Entity\EntityInterface $entity) {
if ($entity->bundle() == 'newsroom_type') {
// Clean up migrations.
\Drupal::service('newsroom_connector.migration_manager')->cleanUpMigrations(NewsroomTypeNewsroomProcessor::MIGRATION_TYPE, $entity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@
*/
class NewsroomTypeNewsroomProcessor extends NewsroomProcessorBase {

// Newsroom type importer.
const MIGRATION_TYPE = 'newsroom_type';
}
4 changes: 4 additions & 0 deletions newsroom_connector.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ services:
arguments: ['@config.factory']
class: Drupal\newsroom_connector\UniverseManager

newsroom_connector.migration_manager:
arguments: [ '@plugin.manager.migration', '@language_manager' ]
class: Drupal\newsroom_connector\MigrationManager

newsroom_connector.plugin.manager.newsroom_processor:
class: Drupal\newsroom_connector\Plugin\NewsroomProcessorManager
parent: default_plugin_manager
87 changes: 87 additions & 0 deletions src/MigrationManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Drupal\newsroom_connector;

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\migrate\Plugin\MigrationPluginManager;

class MigrationManager implements MigrationManagerInterface {

/**
* Migration plugin manager service.
*
* @var \Drupal\migrate\Plugin\MigrationPluginManager
*/
protected $migrationPluginManager;

/**
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;

/**
* {@inheritdoc}
*/
public function __construct(MigrationPluginManager $migrationPluginManager, LanguageManagerInterface $languageManager) {
$this->migrationPluginManager = $migrationPluginManager;
$this->languageManager = $languageManager;
}

/**
* {@inheritdoc}
*/
public function cleanUpMigrations($migration_id, ContentEntityInterface $entity) {
$id = $entity->getEntityType()->getKey('id');
$destination_keys = [];
$destination_keys[$id] = $entity->id();
$this->deleteDestination($migration_id, $destination_keys);

// Run cleanup for translations' migrations.
$languages = $this->languageManager->getLanguages();
foreach ($languages as $language) {
$destination_keys['langcode'] = $language->getId();
$this->deleteDestination($this->getTranslationMigrationId($migration_id, $language->getId()), $destination_keys);
}
}

/**
* {@inheritdoc}
*/
public function normalizeLanguage($language_id) {
// For languages pt-pt, we take the first part only.
if (strpos($language_id, '-') !== FALSE) {
$parts = explode('-', $language_id);
$language_id = $parts[0];
}

return $language_id;
}

/**
* {@inheritdoc}
*/
public function getTranslationMigrationId($migration_id, $language_id) {
$language_id = $this->normalizeLanguage($language_id);
return "{$migration_id}_translations:{$language_id}";
}

/**
* Delete migration records based on keys.
*
* @param string $migration_id
* Migration id.
* @param array $destination_keys
* Destination keys.
*/
protected function deleteDestination($migration_id, array $destination_keys) {
$migration = $this->migrationPluginManager->createInstance($migration_id);
if (empty($migration)) {
return;
}
/** @var \Drupal\migrate\Plugin\MigrateIdMapInterface $id_map */
$id_map = $migration->getIdMap();
$id_map->deleteDestination($destination_keys);
}

}
42 changes: 42 additions & 0 deletions src/MigrationManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Drupal\newsroom_connector;

use Drupal\Core\Entity\ContentEntityInterface;

interface MigrationManagerInterface {

/**
* Clean migration records for a deleted entity.
*
* @param string $migration_id
* Migration id
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* Deleted entity.
*/
public function cleanUpMigrations($migration_id, ContentEntityInterface $entity);

/**
* Normalize language to newsroom format.
*
* @param string $language_id
* Language id.
* @return string
* Normalized language.
*/
public function normalizeLanguage($language_id);

/**
* Get migration id for translations.
*
* @param string $migration_id
* Plugin id
* @param string $language_id
* Language id.
*
* @return string
* Translation migration id.
*/
public function getTranslationMigrationId($migration_id, $language_id);

}
Loading

0 comments on commit 11e423f

Please sign in to comment.