diff --git a/persistent_identifiers.routing.yml b/persistent_identifiers.routing.yml index d1f654f..55089b7 100644 --- a/persistent_identifiers.routing.yml +++ b/persistent_identifiers.routing.yml @@ -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' + diff --git a/src/Controller/PersistentIdentifiersController.php b/src/Controller/PersistentIdentifiersController.php new file mode 100644 index 0000000..93c1c84 --- /dev/null +++ b/src/Controller/PersistentIdentifiersController.php @@ -0,0 +1,110 @@ + 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); + } + +} diff --git a/src/Persister/Generic.php b/src/Persister/Generic.php index 47c8119..8f73490 100644 --- a/src/Persister/Generic.php +++ b/src/Persister/Generic.php @@ -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])); diff --git a/src/Plugin/Form/PersistentIdentifiersSettingsForm.php b/src/Plugin/Form/PersistentIdentifiersSettingsForm.php index 3b5fb99..7cadd1d 100644 --- a/src/Plugin/Form/PersistentIdentifiersSettingsForm.php +++ b/src/Plugin/Form/PersistentIdentifiersSettingsForm.php @@ -4,6 +4,7 @@ use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Url; /** * Admin settings form. @@ -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);