Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.

Commit

Permalink
add a handle minter
Browse files Browse the repository at this point in the history
  • Loading branch information
elizoller committed Feb 3, 2020
1 parent 7f32af5 commit 3060a5a
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 0 deletions.
6 changes: 6 additions & 0 deletions modules/hdl/config/install/hdl.settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
hdl_prefix: '2286.9'
hdl_qualifier: 'R.2.N'
hdl_admin_handle: '2286/ASU_ADMIN'
hdl_admin_index: 300
hdl_handle_api_endpoint: 'https://handle-qa.lib.asu.edu:8000/api/handles/'
hdl_handle_permissions: '111111111111'
6 changes: 6 additions & 0 deletions modules/hdl/hdl.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: 'Handle (HDL) Persistent ID Minter'
description: "A minter accompanying the Persistent Identifers module for minting handles."
type: module
core: 8.x
dependencies:
- persistent_identifiers
150 changes: 150 additions & 0 deletions modules/hdl/hdl.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

/**
* @file
* Contains the hdl.module.
*/

use Drupal\Core\Form\FormStateInterface;

/**
* Implements hook_form_alter().
*
* This example adds a field to the form at
* /admin/config/persistent_identifiers/settings that is specific to the
* minter provided by this module. The value of the field is saved in the
* submit callback below.
*
* Also, note that the list of minters at /admin/config/persistent_identifiers/settings
* is derived from minters defined in minter modules' .services.yml files, not by
* code in hook_form_alter().
*/
function hdl_form_alter(&$form, FormStateInterface &$form_state, $form_id) {
if ($form_id == 'persistent_identifiers_admin_settings') {
$config = \Drupal::config('hdl.settings');
$form['hdl_prefix'] = [
'#type' => 'textfield',
'#access' => TRUE,
'#title' => 'Handle identifier prefix',
'#default_value' => $config->get('hdl_prefix'),
'#description' => t("This string will be prefix used for the handle. Ie. https://handle.net/2286.9 where 2286.9 is the prefix."),
'#states' => [
'visible' => [
':input[id="persistent_identifiers_minter"]' => ['value' => 'hdl.minter.hdl'],
],
],
];

$form['hdl_qualifier'] = [
'#type' => 'textfield',
'#access' => TRUE,
'#title' => 'Handle identifier qualifier',
'#default_value' => $config->get('hdl_qualifier'),
'#description' => t("This string will be prepended to identifier to make the unique handle. You might use it to distinguish different types of objects, like R.C. for Repository Collection and R.O. for Repository Object and R.M. for Repository Media. So your handle might look like https://handle.net/2286.9/R.O.7"),
'#states' => [
'visible' => [
':input[id="persistent_identifiers_minter"]' => ['value' => 'hdl.minter.hdl'],
],
],
];

$form['hdl_admin_handle'] = [
'#type' => 'textfield',
'#access' => TRUE,
'#title' => 'Admin Handle',
'#default_value' => $config->get('hdl_admin_handle'),
'#description' => t("The Admin Handle used for permissions in the handle JSON"),
'#states' => [
'visible' => [
':input[id="persistent_identifiers_minter"]' => ['value' => 'hdl.minter.hdl'],
],
],
];

$form['hdl_admin_index'] = [
'#type' => 'textfield',
'#access' => TRUE,
'#title' => 'Admin Index',
'#default_value' => $config->get('hdl_admin_index'),
'#description' => t("The Admin index used for permissions in the handle JSON"),
'#states' => [
'visible' => [
':input[id="persistent_identifiers_minter"]' => ['value' => 'hdl.minter.hdl'],
],
],
];

$form['hdl_handle_permissions'] = [
'#type' => 'textfield',
'#access' => TRUE,
'#title' => 'Handle Permissions',
'#default_value' => $config->get('hdl_handle_permissions'),
'#description' => t("The permissions to have to the admin user on the handle"),
'#states' => [
'visible' => [
':input[id="persistent_identifiers_minter"]' => ['value' => 'hdl.minter.hdl'],
],
],
];

$form['hdl_handle_api_endpoint'] = [
'#type' => 'textfield',
'#access' => TRUE,
'#title' => 'Handle API Endpoint',
'#default_value' => $config->get('hdl_handle_api_endpoint'),
'#description' => t("The API endpoint for your handle server"),
'#states' => [
'visible' => [
':input[id="persistent_identifiers_minter"]' => ['value' => 'hdl.minter.hdl'],
],
],
];

$form['hdl_handle_basic_auth_password'] = [
'#type' => 'password',
// '#access' => TRUE,
'#title' => 'Handle Basic Auth Password',
'#default_value' => $config->get('hdl_handle_basic_auth_password'),
'#description' => t("The password to be used with the admin handle and index to authenticate for the API"),
'#states' => [
'visible' => [
':input[id="persistent_identifiers_minter"]' => ['value' => 'hdl.minter.hdl'],
],
],
];

$form['#submit'][] = 'hdl_submit';
}
}

/**
* Submit callback.
*
* Saves the value of the minter-specific field defined in the implementation
* of hook_form_alter() above.
*
* @param array $form
* The form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
function hdl_submit(array &$form, FormStateInterface $form_state) {
$existing_config = \Drupal::config('hdl.settings');
$admin_handle = $form_state->getValue('hdl_admin_handle', $existing_config->get('hdl_admin_handle'));
$prefix = $form_state->getValue('hdl_prefix', $existing_config->get('hdl_prefix'));
$qualifier = $form_state->getValue('hdl_qualifier', $existing_config->get('hdl_qualifier'));
$api_endpoint = $form_state->getValue('hdl_handle_api_endpoint', $existing_config->get('hdl_handle_api_endpoint'));
$index = $form_state->getValue('hdl_admin_index', $existing_config->get('hdl_admin_index'));
$permissions = $form_state->getValue('hdl_handle_permissions', $existing_config->get('hdl_handle_permissions'));
$password = $form_state->getValue('hdl_handle_basic_auth_password', $existing_config->get('hdl_handle_basic_auth_password'));
$config_factory = \Drupal::configFactory();
$config_factory->getEditable('hdl.settings')
->set('hdl_admin_handle', trim($admin_handle))
->set('hdl_prefix', trim($prefix))
->set('hdl_qualifier', trim($qualifier))
->set('hdl_handle_api_endpoint', trim($api_endpoint))
->set('hdl_admin_index', trim($index))
->set('hdl_handle_permissions', trim($permissions))
->set('hdl_handle_basic_auth_password', trim($password))
->save();
}
5 changes: 5 additions & 0 deletions modules/hdl/hdl.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
# The list of minters at /admin/config/persistent_identifiers/settings is derived
# from minters that follow this naming pattern.
hdl.minter.hdl:
class: Drupal\hdl\Minter\Hdl
97 changes: 97 additions & 0 deletions modules/hdl/src/Minter/Hdl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Drupal\hdl\Minter;

use Drupal\persistent_identifiers\MinterInterface;

/**
* A Handle class.
*
* Mints a persistent identifier using a configurable
* namespace and a random string.
*/
class Hdl implements MinterInterface {

/**
* Returns the minter's name.
*
* @return string
* Appears in the Persistent Identifiers config form.
*/
public function getName() {
return t('Handle Minter');
}

/**
* Returns the minter's type.
*
* @return string
* Appears in the entity edit form next to the checkbox.
*/
public function getPidType() {
return t('Handle');
}

/**
* Mints the identifier.
*
* This sample minter simply returns a random string prepended by
* a namespace, but this method is where you would request a new
* DOI, ARK, etc.
*
* @param object $entity
* The entity.
* @param mixed $extra
* Extra data the minter needs, for example from the node edit form.
*
* @return string
* The identifier.
*/
public function mint($entity, $extra = NULL) {
$config = \Drupal::config('hdl.settings');
$handle_prefix = $config->get('hdl_prefix');
$handle_type_qualifier = $config->get('hdl_qualifier');
$handle = $handle_prefix . '/' . $handle_type_qualifier . '.' . $entity->id();
$host = \Drupal::request()->getSchemeAndHttpHost();
$url = $host . $entity->toUrl()->toString();
$admin_handle = $config->get('hdl_admin_handle');
$handle_admin_index = $config->get('hdl_admin_index');
$endpoint_url = $config->get('hdl_handle_api_endpoint');
$permissions = $config->get('hdl_handle_permissions');
$password = $config->get('hdl_handle_basic_auth_password');
$handle_json = [
[
'index' => 1,
'type' => "URL",
'data' => [
'format' => "string",
'value' => $url,
],
],
[
'index' => 100,
'type' => 'HS_ADMIN',
'data' => [
'format' => 'admin',
'value' => [
'handle' => $admin_handle,
'index' => $handle_admin_index,
'permissions' => $permissions,
],
],
],
];

$client = \Drupal::httpClient();
try {
$request = $client->request('PUT', $endpoint_url . $handle . "?overwrite=true", ['json' => $handle_json, 'auth' => [$handle_admin_index . '%3A' . $admin_handle, $password], 'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json']]);
\Drupal::logger('persistent identifiers')->info(print_r($request, TRUE));
} catch (ClientException $e) {
\Drupal::logger('persistent identifiers')->error(print_r($e, TRUE));
return FALSE;
}
$full_handle = "https://handle.net/" . $handle;
return $full_handle;
}

}

0 comments on commit 3060a5a

Please sign in to comment.