Loads Evergreen bookbags in a drupal block based on bookbag id stored in bookbag_id field of course reserve content type. This version is for Drupal 9 or greater. The d7 version is in github in the roblib organization.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

82 lines
2.0 KiB

<?php
namespace Drupal\upei_roblib_reserves\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a Reserves Block base on Evergreen bookbags.
*
* @Block(
* id = "upei_roblib_reserves_block",
* admin_label = @Translation("Roblib Reserves Block"),
* category = @Translation("UPEI Roblib"),
* )
*/
class ReserveBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
$html = $this->getReserves();
if (empty($html)) {
return '';
}
return [
'#theme' => 'upei_roblib_reserves_block',
'#atom_to_html' => [
'#markup' => $html,
],
];
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
//Reading dynamic content from external source so no caching.
return 0;
}
/**
* Queries Evergreen for an Atom XML feed based on book bag list.
*
* @return string
* HTML created from Atom XML feed.
*/
protected function getReserves() {
$node = \Drupal::routeMatch()->getParameter('node');
$html = '';
if ($node && $node instanceof \Drupal\node\NodeInterface && $node->bundle() == 'course_reserve') {
$book_bag_id = (string) $node->field_bookbag_id->value;
if (empty($book_bag_id)) {
return '';
}
//The evergreen ILS base URL.
$book_bag_base_url = 'http://islandpines.roblib.upei.ca/opac/extras/feed/bookbag/atom-full/';
try {
$response = \Drupal::httpClient()->get($book_bag_base_url . $book_bag_id);
$atom = (string) $response->getBody();
if (empty($atom)) {
return '';
}
}
catch (RequestException $e) {
return '';
}
$path = \Drupal::service('extension.list.module')->getPath('upei_roblib_reserves');
$xslt = new \XSLTProcessor();
$xsl = new \DOMDocument();
$xsl->load($path . '/xsl/atom2html.xsl');
$xslt->importStylesheet($xsl);
$xml = new \DomDocument();
$xml->loadXML($atom);
$html = $xslt->transformToXML($xml);
}
return $html;
}
}