Skip to content
This repository was archived by the owner on Oct 20, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions persistent_identifiers.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ persistent_identifiers.settings:
_title: 'Persistent Identifiers'
requirements:
_permission: 'administer site configuration'
persistent_identifiers.persist_all:
path: '/admin/config/persistent_identifiers/persist_all'
defaults:
_controller: '\Drupal\persistent_identifiers\Controller\PersistentIdentifiersController::persistent_identifiers_mint_and_persist'
_title: 'Persist objects'
requirements:
_permission: 'administer content'

110 changes: 110 additions & 0 deletions src/Controller/PersistentIdentifiersController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace Drupal\persistent_identifiers\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Request;



class PersistentIdentifiersController extends ControllerBase {

/**
* The function calling the batch process.
*/
function persistent_identifiers_mint_and_persist(Request $request) {
$operations[] = ['\Drupal\persistent_identifiers\Controller\PersistentIdentifiersController::persistent_identifiers_batch_process', []];
$batch = [
'title' => t('Persistent Identifiers Mint and Persist'),
'operations' => $operations,
'finished' => '\Drupal\persistent_identifiers\Controller\PersistentIdentifiersController::persistent_identifiers_finished_callback',
'init_message' => t('Batch is starting.'),
'progress_message' => t('Processed @current out of @total.'),
];

// Adds the batch sets
batch_set($batch);
// Process the batch and after redirect to the frontpage
return batch_process('admin/config/persistent_identifiers/settings');


}
/**
* A Batch process function.
*/
function persistent_identifiers_batch_process(&$context) {
$config = \Drupal::config('persistent_identifiers.settings');
$types = $config->get('persistent_identifiers_bundles');

// QUICK HACK TODO: don't save zeroes if type not selected
foreach ($types as $key => $type) {
if ($types[$key] === 0) {
unset($types[$key]);
}
}
// END QUICK HACK

if (empty($context['sandbox'])) {
$query = \Drupal::entityQuery('node');

$count = $query->condition('type', $types)
->count()
->execute();

$context['sandbox']['progress'] = 0;
$context['sandbox']['current_id'] = 0;
$context['sandbox']['max'] = $count;
}

$limit = 25;
$query = \Drupal::entityQuery('node');

$result = $query->condition('type', $types)
->condition('nid', $context['sandbox']['current_id'], '>')
->sort('nid', 'ASC')
->range(0, $limit)
->execute();
$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($nids);

foreach ($nodes as $node) {
// Persist and Mint
$minter = \Drupal::service($config->get('persistent_identifiers_minter'));
$pid = $minter->mint($node);
if (is_null($pid)) {
\Drupal::logger('persistent_identifiers')->warning(t("Persistent identifier not created for node @nid.", ['@nid' => $node->id()]));
\Drupal::messenger()->addWarning(t("Problem creating persistent identifier for this node. Details are available in the Drupal system log."));
return;
}
$persister = \Drupal::service($config->get('persistent_identifiers_persister'));
if ($persister->persist($node, $pid)) {
\Drupal::logger('persistent_identifiers')->info(t("Persistent identifier %pid created for node @nid.", ['%pid' => $pid, '@nid' => $node->id()]));
\Drupal::messenger()->addStatus(t("Persistent identifier %pid created for this node.", ['%pid' => $pid]));
}

// Modify context for next batch run
$context['results'][] = $node->id . ' : ' . $node->title->value;
$context['sandbox']['progress']++;
$context['sandbox']['current_id'] = $node->id;
$context['message'] = $node->title->value;
}

if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}

/**
* Function that signals that the mint and persist is finished.
*/
public static function persistent_identifiers_finished_callback($success, $results, $operations) {

if ($success) {
$message = t('Persistent Identifers: Persisting complete');
}
else {
$message = t('There were some errors.');
}
drupal_set_message($message);
}

}
5 changes: 4 additions & 1 deletion src/Persister/Generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public function getName() {
public function persist(&$entity, $pid, $save = TRUE) {
$target_field = trim($this->config->get('persistent_identifiers_target_field'));
if (method_exists($entity, 'hasField') && $entity->hasField($target_field)) {
$entity->{$target_field}[] = $pid;
$existing_values = $entity->get($target_field)->getValue();
if(!in_array($pid, $existing_values)) {
$entity->{$target_field}[] = $pid;
}
}
else {
\Drupal::messenger()->addMessage(t('This node does not have the required field (@field)', ['@field' => $target_field]));
Expand Down
14 changes: 14 additions & 0 deletions src/Plugin/Form/PersistentIdentifiersSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

/**
* Admin settings form.
Expand Down Expand Up @@ -103,6 +104,19 @@ public function buildForm(array $form, FormStateInterface $form_state) {
],
],
];
$form['actions']['mint_and_persist'] = [
'#type' => 'link',
'#title' => 'Mint and Persist',
'#attributes' => [
'id' => 'mint-and-persist',
'class' => [
'button'
],
'data-twig-id' => 'goto-step-one',
],
'#weight' => 0,
'#url' => Url::fromRoute('persistent_identifiers.persist_all'),
];


return parent::buildForm($form, $form_state);
Expand Down