Skip to content

Commit

Permalink
Use migration to import cap org codes
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Oct 27, 2023
1 parent ca3c9fd commit ebec3a1
Show file tree
Hide file tree
Showing 4 changed files with 571 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
id: cap_orgs
migration_tags: {}
label: 'CAP Organizations'
deriver: Drupal\stanford_person_importer\Plugin\migrate\OrgCodeDeriver
migration_group: su_stanford_person
source:
authentication:
plugin: oauth2
base_uri: 'https://authz.stanford.edu'
token_url: /oauth/token
grant_type: client_credentials
client_id: '[client_id]'
client_secret: '[client_secret]'
urls:
- https://api.stanford.edu/cap/v1/orgs/AA00
track_changes: true
plugin: url
data_fetcher_plugin: http
data_parser_plugin: json
headers:
Accept: 'application/json; charset=utf-8'
Content-Type: application/json
orphan_action: delete
ids:
alias:
type: string
item_selector: children
constants:
vid: cap_org_codes
code_beginning: '('
code_ending: ')'
fields:
-
name: alias
label: alias
selector: alias
-
name: children
label: children
selector: children
-
name: name
label: name
selector: name
-
name: orgCodes
label: orgCodes
selector: orgCodes
-
name: type
label: type
selector: type
process:
codes:
-
plugin: skip_on_empty
method: process
source: orgCodes
-
plugin: concat
source: orgCodes
delimiter: ', '
code_title:
plugin: concat
source:
- constants/code_beginning
- '@codes'
- constants/code_ending
name:
plugin: concat
source:
- name
- '@code_title'
delimiter: ' '
vid: constants/vid
su_cap_org_code: orgCodes
destination:
plugin: 'entity:taxonomy_term'
migration_dependencies: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Drupal\stanford_person_importer\Plugin\migrate;

use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\migrate\Plugin\MigrationDeriverTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;

class OrgCodeDeriver extends DeriverBase implements ContainerDeriverInterface {

use MigrationDeriverTrait;
use StringTranslationTrait;

public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$base_plugin_id
);
}

public function __construct(protected $basePluginId) {}

public function getDerivativeDefinitions($base_plugin_definition) {
$config_pages = \Drupal::service('config_pages.loader');
$client_id = $config_pages->getValue('stanford_person_importer', 'su_person_cap_username', 0, 'value') ?? '';;
$client_secret = $config_pages->getValue('stanford_person_importer', 'su_person_cap_password', 0, 'value') ?? '';;
if (!$client_id || !$client_secret) {
return $this->derivatives;
}

$base_plugin_definition['source']['authentication']['client_id'] = $client_id;
$base_plugin_definition['source']['authentication']['client_secret'] = $client_secret;

/** @var \Drupal\migrate\Plugin\Migration $migration */
$migration = \Drupal::service('plugin.manager.migration')
->createStubMigration($base_plugin_definition);
$source = $migration->getSourcePlugin();
$all_data = [];
$source->rewind();
while ($source->valid()) {
/** @var \Drupal\migrate\Row $row */
$row = $source->current();
$all_data[] = $row->getSource();
$source->next();
}
$this->derivatives['base'] = $base_plugin_definition;
$this->createDerivatives($all_data, $base_plugin_definition, 'children', 'base');
return $this->derivatives;
}

protected function createDerivatives($data, $base_definition, $path, $parent_code = NULL) {
foreach ($data as $key => $item_data) {
if (empty($item_data['children'])) {
continue;
}

$plugin = $base_definition;
asort($item_data['orgCodes']);
$code = reset($item_data['orgCodes']);

$plugin['source']['urls'][0] = "https://api.stanford.edu/cap/v1/orgs/$code";
$plugin['process']['parent/target_id'] = [
[
'plugin' => 'default_value',
'default_value' => $item_data['alias'],
],
[
'plugin' => 'migration_lookup',
'migration' => "cap_orgs:$parent_code",
],
];

if ($parent_code) {
$plugin['migration_dependencies']['required'] = [
"cap_orgs:$parent_code",
];
}

$this->derivatives[strtolower($code)] = $plugin;
$this->createDerivatives($item_data['children'], $base_definition, "$path/$key/children", strtolower($code));
}
}

}
Loading

0 comments on commit ebec3a1

Please sign in to comment.