From 54dde1a0a3e8642a6d04279d6430b9571f1ffabc Mon Sep 17 00:00:00 2001 From: ppound Date: Thu, 3 Feb 2022 10:58:49 -0400 Subject: [PATCH] loading doi data working --- includes/doi.inc | 26 ++++++++------ src/Form/RoblibIllLoanForm.php | 65 +++++++++++++++++----------------- upei_roblib_ill.install | 16 --------- 3 files changed, 48 insertions(+), 59 deletions(-) diff --git a/includes/doi.inc b/includes/doi.inc index 2d24371..2a765cd 100644 --- a/includes/doi.inc +++ b/includes/doi.inc @@ -18,22 +18,26 @@ * @return array * A Drupal form with updated values. */ + +use Drupal\Core\Url; + function upei_roblib_ill_doi_get_data($form, $doi) { if (empty($doi)) { $doi = 'Empty DOI'; } $response = upei_roblib_ill_doi_load($doi); - if ($response->data && strpos($response->data, "Malformed DOI") !== FALSE) { + $response_data = (string)$response->getBody(); + if ($response_data && strpos($response_data, "Malformed DOI") !== FALSE) { //try again without parsing the doi as sometimes parsing breaks it $response = upei_roblib_ill_doi_load($doi, FALSE); } - $headers = array_change_key_case($response->headers); - if ($response->data && - strpos($headers['content-type'], 'text/html') === FALSE && - strpos($response->data, "Malformed DOI") === FALSE + $headers = array_change_key_case($response->getHeaders()); + if ($response_data && + strpos($headers['content-type'][0], 'text/html') === FALSE && + strpos($response_data, "Malformed DOI") === FALSE ) { $crossref_xml = new DOMDocument('1.0'); - if (!$crossref_xml->loadXML($response->data)) { + if (!$crossref_xml->loadXML($response_data)) { drupal_set_message(t('Error parseing DOI response, @msg')); return $form; } @@ -159,9 +163,10 @@ function upei_roblib_ill_doi_name_from_url($doi_url) { function upei_roblib_ill_doi_load($doi, $parse = TRUE) { // Allows for $id to pass a DOI url string or the DOI name. $id = $parse ? upei_roblib_ill_doi_name_from_url($doi) : $doi; - $openurl = variable_get('upei_roblib_ill_doi_openurl', 'http://www.crossref.org/openurl'); - $openurl_pid = variable_get('upei_roblib_ill_doi_openurl_pid', 'user@example.com'); - $url = url($openurl, [ + $config = \Drupal::config('upei_roblib_ill.settings'); + $openurl = 'http://www.crossref.org/openurl'; + $openurl_pid = $config->get('ill_doi_openurl_pid'); + $url = Url::fromUri($openurl, [ 'query' => [ 'noredirect' => 'true', 'pid' => $openurl_pid, @@ -169,6 +174,7 @@ function upei_roblib_ill_doi_load($doi, $parse = TRUE) { 'id' => ((strpos($id, 'doi:') === 0) ? $id : 'doi:' . $id), ], ]); - return drupal_http_request($url); + $response = \Drupal::httpClient()->get($url->toString()); + return $response; } diff --git a/src/Form/RoblibIllLoanForm.php b/src/Form/RoblibIllLoanForm.php index fd1aed0..6604d1b 100644 --- a/src/Form/RoblibIllLoanForm.php +++ b/src/Form/RoblibIllLoanForm.php @@ -27,18 +27,6 @@ class RoblibIllLoanForm extends FormBase { return $this->ill_auth_form($form, $form_state); } return $this->ill_request_form($form, $form_state); - - /* $values = $form_state->getValues(); - if (empty($values)) { - $form_name = 'upei_roblib_ill_request_form'; - } - else { - $form_name = 'upei_roblib_ill_auth_form'; - } - $form_state->setValue('step', $from_name); - return $form_name === 'upei_roblib_ill_request_form' ? $this->ill_request_form($form, $form_state) : - $this->ill_auth_form($form, $form_state); - * */ } /** @@ -55,8 +43,8 @@ class RoblibIllLoanForm extends FormBase { if (!$values['certify']) { $form_state->setErrorByName('certify', t('Please certify that this is item is being sought for a fair dealing purpose')); } - - $campus_id = $this->ill_form_message_id($values['campus_id']); + + $campus_id = $this->ill_form_message_id($values['campus_id']); $aid = upei_roblib_ill_authenticate($campus_id, $values['Surname']); if (is_array($aid) && isset($aid['Problem']['Message'])) { @@ -72,22 +60,23 @@ class RoblibIllLoanForm extends FormBase { $form_state->set('aid', $aid); } } - //if (empty($form_state->get('step')) && $form_state['triggering_element']['#value'] !== 'Lookup DOI' && empty($form_state->get('Title'))) { - // form_set_error('Title', t('Journal/Book Title is required.')); - //} + $trigger = $form_state->getTriggeringElement(); + if (empty($form_state->get('step')) && $trigger['#value'] !== 'Lookup DOI' && empty($form_state->getValue('Title'))) { + $form_state->setErrorByName('Title', t('Journal/Book Title is required.')); + } } - + /** - * @param string $id - * The users campus id - * - * @return mixed - * the campus id with leading zeros and leading/trailing whitespace removed. - */ -function ill_form_message_id($id) { - $id = ltrim($id, '0'); - return trim($id); -} + * @param string $id + * The users campus id + * + * @return mixed + * the campus id with leading zeros and leading/trailing whitespace removed. + */ + function ill_form_message_id($id) { + $id = ltrim($id, '0'); + return trim($id); + } /** * @param array $form @@ -96,10 +85,13 @@ function ill_form_message_id($id) { * The current state of the form. */ public function submitStepOne(array &$form, FormStateInterface $form_state) { - $form_state - ->set('step', 2) - ->set('request', $form_state->getValues()) - ->setRebuild(TRUE); + $trigger = $form_state->getTriggeringElement(); + if ($trigger['#value'] != 'Lookup DOI') { + $form_state + ->set('step', 2) + ->set('request', $form_state->getValues()) + ->setRebuild(TRUE); + } } /** @@ -122,6 +114,13 @@ function ill_form_message_id($id) { ]; */ } + public function doiCallback(array &$form, FormStateInterface $form_state) { + module_load_include('inc', 'upei_roblib_ill', 'includes/doi'); + $doi = $form_state->getValue('doi'); + $form = upei_roblib_ill_doi_get_data($form, $doi); + return $form; + } + /** * The bibliographic portion of the ILL form. * @@ -177,7 +176,7 @@ function ill_form_message_id($id) { '#type' => 'submit', '#value' => 'Lookup DOI', '#ajax' => [ - 'callback' => 'upei_roblib_ill_doi_callback', + 'callback' => '::doiCallback', 'method' => 'replace', 'wrapper' => 'upei-roblib-ill-request-form', 'effect' => 'fade', diff --git a/upei_roblib_ill.install b/upei_roblib_ill.install index d270ca9..f02855f 100644 --- a/upei_roblib_ill.install +++ b/upei_roblib_ill.install @@ -5,22 +5,6 @@ */ -/** - * Implements hook_uninstall(). - */ -function upei_roblib_ill_uninstall() { - $variables = [ - 'upei_roblib_ill_add_url', - 'upei_roblib_ill_auth_url', - 'upei_roblib_ill_relais_key', - 'upei_roblib_ill_library_symbol', - 'upei_roblib_ill_contact_email', - 'upei_roblib_ill_contact_phonel', - 'upei_roblib_ill_header_message', - ]; - array_walk($variables, 'variable_del'); -} - function upei_roblib_ill_update_7100() { db_change_field('upei_roblib_ill_request', 'isbn', 'isbn', array(