Skip to content

Commit

Permalink
NBBIB-451 Filter by New Brunswick jurisdiction
Browse files Browse the repository at this point in the history
  • Loading branch information
camilocodes committed Sep 5, 2024
1 parent ecb6104 commit 0aa0f73
Showing 1 changed file with 51 additions and 51 deletions.
102 changes: 51 additions & 51 deletions custom/modules/unblib_marc/script/import_marc.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,70 +107,70 @@
function migrateMarc(string $source, string $entity_type, array $map, bool $publish) {
$collection = Collection::fromFile($source);

$n = 0; // Debug.
foreach ($collection as $record) {
$entity = \Drupal::entityTypeManager()->getStorage($entity_type)->create();
$jurisdiction = getMarcValue($record, '593');
$filter = str_contains(strtolower($jurisdiction), 'brunswick');

foreach ($map as $field => $mapping) {
$field = $mapping['target'] ?? $mapping['target'] ?? $field;
$marc = $mapping['marc'] ?? $mapping['marc_fallback'] ?? NULL;
$multival = isset($mapping['multival']) and $mapping['multival'];
$append = isset($mapping['append']) and $mapping['append'];

if ($marc) {
$value = getMarcValue($record, $marc, $multival);
}
elseif ($mapping['default']) {
$value = $mapping['default'];
}
if ($filter) {
$entity = \Drupal::entityTypeManager()->getStorage($entity_type)->create();

foreach ($map as $field => $mapping) {
$field = $mapping['target'] ?? $mapping['target'] ?? $field;
$marc = $mapping['marc'] ?? $mapping['marc_fallback'] ?? NULL;
$multival = isset($mapping['multival']) and $mapping['multival'];
$append = isset($mapping['append']) and $mapping['append'];

if ($marc) {
$value = getMarcValue($record, $marc, $multival);
}
elseif ($mapping['default']) {
$value = $mapping['default'];
}

if ($value) {
if(isset($mapping['process'])) {
$callback = $mapping['process'];
if (is_callable($callback)) {
if (function_exists($callback)) {
$value = $callback($value);
if ($value) {
if(isset($mapping['process'])) {
$callback = $mapping['process'];
if (is_callable($callback)) {
if (function_exists($callback)) {
$value = $callback($value);
}
}
}
}

if ($append) {
$update = $entity->get($field)->getValue();

if (is_array($update)) {
$update = array_merge($update, $value);
$entity->set($field, $update);
if ($append) {
$update = $entity->get($field)->getValue();

if (is_array($update)) {
$update = array_merge($update, $value);
$entity->set($field, $update);
}
else {
$entity->set($field, $value);
}
}
else {
else {
$value = is_string($value) ?
text_trim($value) :
$value;
$entity->set($field, $value);
}
}
else {
$value = is_string($value) ?
text_trim($value) :
$value;
$entity->set($field, $value);
}
}
}

$title = $entity->getTitle();

// @TODO: Pass array of mandatory fields and only save if constraints met.
if ($title) {
echo "\nSaving unpublished [$entity_type] [$title]";
//$entity->setPublished($publish);
$contribs = $entity->getContributors();
//var_dump($contribs);
$entity->save();
$contribs = $entity->getContributors();
//var_dump($contribs);
}

$n++;
$title = $entity->getTitle();

// @TODO: Pass array of mandatory fields and only save if constraints met.
if ($title) {
echo "\nSaving unpublished [$entity_type] [$title]";
//$entity->setPublished($publish);
$contribs = $entity->getContributors();
//var_dump($contribs);
$entity->save();
$contribs = $entity->getContributors();
//var_dump($contribs);
}

if ($n > 5) {
exit;
$n++;
}
}

Expand Down

0 comments on commit 0aa0f73

Please sign in to comment.