Skip to content

Commit

Permalink
Merge pull request #127 from chrismacdonaldw/fdr-623
Browse files Browse the repository at this point in the history
FDR-623: Create alter support module
  • Loading branch information
nchiasson-dgi authored Mar 6, 2024
2 parents e63765d + d1150b2 commit 0edeba3
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 0 deletions.
49 changes: 49 additions & 0 deletions modules/dgi_migrate_alter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# DGI Migrate Alter

## Introduction
A module to easily alter migrations using the `MigrationAlter` plugin.

## Features
The module adds the `MigrationAlter` plugin base, which allows for the easy alteration of migrations.

The `MigrationAlter` plugin base has four fields:
- `id`
- `label`
- `description`
- `migration_id`
- The id of the migration you wish to alter

The path to place the alter plugins is as follows:
- `module/Plugin/dgi_migrate_alter/spreadsheet`
- `module/Plugin/dgi_migrate_alter/foxml`

There is no strict difference between the two, and are pathed as such strictly for organization.

The alter itself should be written in the `alter` function.

## Installation

Install as usual, see
[this](https://drupal.org/documentation/install/modules-themes/modules-8) for
further information.

## Troubleshooting/Issues

Having problems or solved a problem? Contact
[discoverygarden](http://support.discoverygarden.ca).

## Maintainers and Sponsors

Current maintainers:

* [discoverygarden](http://www.discoverygarden.ca)

## Development/Contribution

If you would like to contribute to this module, please check out github's helpful
[Contributing to projects](https://docs.github.com/en/get-started/quickstart/contributing-to-projects) documentation and Islandora community's [Documention for developers](https://islandora.github.io/documentation/contributing/CONTRIBUTING/#github-issues) to create an issue or pull request and/or
contact [discoverygarden](http://support.discoverygarden.ca).

## License

[GPLv3](http://www.gnu.org/licenses/gpl-3.0.txt)
5 changes: 5 additions & 0 deletions modules/dgi_migrate_alter/dgi_migrate_alter.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: 'DGI Migrate Alter'
description: "Allows easy alterations of migrations through plugins."
type: module
package: DGI
core_version_requirement: ^9 || ^10
45 changes: 45 additions & 0 deletions modules/dgi_migrate_alter/dgi_migrate_alter.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* @file
* Contains dgi_migrate_alter.module.
*/

use Drupal\dgi_migrate_alter\Exception\MigrationNotFoundException;

/**
* Implements hook_migration_plugins_alter().
*/
function dgi_migrate_alter_migration_plugins_alter(array &$migrations) {
$logger = \Drupal::logger('dgi_migrate_alter');

$spreadsheet_alter_manager = \Drupal::service('plugin.manager.dgi_migrate_alter.spreadsheet');
$foxml_alter_manager = \Drupal::service('plugin.manager.dgi_migrate_alter.foxml');

$spreadsheet_definitions = $spreadsheet_alter_manager->getDefinitions();
$foxml_definitions = $foxml_alter_manager->getDefinitions();

foreach ($spreadsheet_definitions as $id => $definition) {
$logger->info('Altering migrations with spreadsheet plugin: ' . $id);
$plugin = $spreadsheet_alter_manager->createInstance($id);

try {
$plugin->alterMigrations($migrations);
}
catch (MigrationNotFoundException $e) {
$logger->error('Migration not found: ' . $e->getMessage());
}
}

foreach ($foxml_definitions as $id => $definition) {
$logger->info('Altering migrations with foxml plugin: ' . $id);
$plugin = $foxml_alter_manager->createInstance($id);

try {
$plugin->alterMigrations($migrations);
}
catch (MigrationNotFoundException $e) {
$logger->error('Migration not found: ' . $e->getMessage());
}
}
}
8 changes: 8 additions & 0 deletions modules/dgi_migrate_alter/dgi_migrate_alter.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
services:
plugin.manager.dgi_migrate_alter.spreadsheet:
class: Drupal\dgi_migrate_alter\Plugin\MigrationAlterPluginManager
arguments: [spreadsheet, '@container.namespaces', '@cache.discovery', '@module_handler']
plugin.manager.dgi_migrate_alter.foxml:
class: Drupal\dgi_migrate_alter\Plugin\MigrationAlterPluginManager
arguments: [foxml, '@container.namespaces', '@cache.discovery', '@module_handler']
46 changes: 46 additions & 0 deletions modules/dgi_migrate_alter/src/Annotation/MigrationAlter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Drupal\dgi_migrate_alter\Annotation;

use Drupal\Component\Annotation\Plugin;

/**
* Defines a MigrationAlter annotation object.
*
* @Annotation
*/
class MigrationAlter extends Plugin {

/**
* The plugin ID.
*
* @var string
*/
public $id;

/**
* The human-readable name of the migration alteration.
*
* @var \Drupal\Core\Annotation\Translation
*
* @ingroup plugin_translatable
*/
public $label;

/**
* The description of the migration alteration.
*
* @var \Drupal\Core\Annotation\Translation
*
* @ingroup plugin_translatable
*/
public $description;

/**
* The migration ID.
*
* @var string
*/
public $migration_id;

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

namespace Drupal\dgi_migrate_alter\Exception;

/**
* To throw when the Plugin Manager fails to find a migration to alter.
*/
class MigrationNotFoundException extends \Exception {}
39 changes: 39 additions & 0 deletions modules/dgi_migrate_alter/src/Plugin/MigrationAlterBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Drupal\dgi_migrate_alter\Plugin;

use Drupal\Component\Plugin\PluginBase;
use Drupal\dgi_migrate_alter\Exception\MigrationNotFoundException;

/**
* Base class for migration alter plugins.
*/
abstract class MigrationAlterBase extends PluginBase implements MigrationAlterInterface {

/**
* {@inheritdoc}
*
* @throws \Drupal\dgi_migrate_alter\Exception\MigrationNotFoundException
*/
final public function alterMigrations(array &$migrations) {
$migration_id = $this->getPluginDefinition()['migration_id'];
if (isset($migrations[$migration_id])) {
$this->alter($migrations[$migration_id]);
}
else {
throw new MigrationNotFoundException("Migration $migration_id not found.");
}
}

/**
* Alters the specified migration array provided by alterMigrations.
*
* This method is intended to be overridden in child classes.
* Each child class should implement its own logic.
*
* @param array &$migration
* The migration array to alter.
*/
abstract protected function alter(array &$migration);

}
20 changes: 20 additions & 0 deletions modules/dgi_migrate_alter/src/Plugin/MigrationAlterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Drupal\dgi_migrate_alter\Plugin;

use Drupal\Component\Plugin\PluginInspectionInterface;

/**
* Interface for CSV Alteration plugins.
*/
interface MigrationAlterInterface extends PluginInspectionInterface {

/**
* Alters the migrations.
*
* @param array $migrations
* The migrations array.
*/
public function alterMigrations(array &$migrations);

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

namespace Drupal\dgi_migrate_alter\Plugin;

use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;

/**
* Provides the migration alter plugin manager.
*/
class MigrationAlterPluginManager extends DefaultPluginManager {

/**
* Constructs a new MigrationAlterManager object.
*
* @param string $type
* The type of the plugin: spreadsheet, foxml.
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* Cache backend instance to use.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*/
public function __construct($type, \Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct(
"Plugin/dgi_migrate_alter/$type",
$namespaces,
$module_handler,
'Drupal\dgi_migrate_alter\Plugin\MigrationAlterInterface',
'Drupal\dgi_migrate_alter\Annotation\MigrationAlter'
);
$this->alterInfo('dgi_migrate_alter_' . $type . '_info');
$this->setCacheBackend($cache_backend, 'dgi_migrate_alter' . $type . '_plugins');
}

}

0 comments on commit 0edeba3

Please sign in to comment.