Browse Source

Drupal 11 compatibility and some minor changes to further future proof the module

main
Paul Pound 2 weeks ago
parent
commit
a2c55399aa
  1. 12
      composer.json
  2. 126
      src/Plugin/Block/ReserveBlock.php
  3. 5
      upei_roblib_reserves.info.yml

12
composer.json

@ -0,0 +1,12 @@
{
"name": "upei/upei_roblib_reserves",
"description": "Consumes an Evergreen bookbag Atom feed and displays the results in a Drupal block.",
"type": "drupal-module",
"license": "GPL-2.0-or-later",
"require": {
"php": ">=8.1",
"ext-dom": "*",
"ext-libxml": "*",
"ext-xsl": "*"
}
}

126
src/Plugin/Block/ReserveBlock.php

@ -2,10 +2,23 @@
namespace Drupal\upei_roblib_reserves\Plugin\Block;
use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\node\NodeInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a Reserves Block base on Evergreen bookbags.
* Provides a Reserves Block based on Evergreen bookbags.
*
* @todo Remove the annotation once the site is on Drupal 10.2 or later;
* the #[Block] attribute is used from 10.2 onward.
*
* @Block(
* id = "upei_roblib_reserves_block",
@ -13,7 +26,62 @@ use Drupal\Core\Block\BlockBase;
* category = @Translation("UPEI Roblib"),
* )
*/
class ReserveBlock extends BlockBase {
#[Block(
id: "upei_roblib_reserves_block",
admin_label: new TranslatableMarkup("Roblib Reserves Block"),
category: new TranslatableMarkup("UPEI Roblib"),
)]
class ReserveBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* The Evergreen ILS bookbag Atom feed base URL.
*/
protected const BOOK_BAG_BASE_URL = 'https://islandpines.libraryupei.ca/opac/extras/feed/bookbag/atom-full/';
/**
* Constructs a new ReserveBlock.
*
* @param array $configuration
* The plugin configuration.
* @param string $plugin_id
* The plugin ID.
* @param mixed $plugin_definition
* The plugin definition.
* @param \GuzzleHttp\ClientInterface $httpClient
* The HTTP client.
* @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
* The current route match.
* @param \Drupal\Core\Extension\ModuleExtensionList $moduleExtensionList
* The module extension list.
* @param \Psr\Log\LoggerInterface $logger
* The logger channel.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
protected ClientInterface $httpClient,
protected RouteMatchInterface $routeMatch,
protected ModuleExtensionList $moduleExtensionList,
protected LoggerInterface $logger,
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('http_client'),
$container->get('current_route_match'),
$container->get('extension.list.module'),
$container->get('logger.factory')->get('upei_roblib_reserves'),
);
}
/**
* {@inheritdoc}
@ -36,7 +104,7 @@ class ReserveBlock extends BlockBase {
* {@inheritdoc}
*/
public function getCacheMaxAge() {
//Reading dynamic content from external source so no caching.
// Reading dynamic content from external source so no caching.
return 0;
}
@ -46,37 +114,55 @@ class ReserveBlock extends BlockBase {
* @return string
* HTML created from Atom XML feed.
*/
protected function getReserves() {
$node = \Drupal::routeMatch()->getParameter('node');
protected function getReserves(): string {
$node = $this->routeMatch->getParameter('node');
if (!$node instanceof NodeInterface || $node->bundle() !== 'course_reserve' || !$node->hasField('field_bookbag_id')) {
return '';
}
$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)) {
$book_bag_id = (string) $node->get('field_bookbag_id')->value;
if ($book_bag_id === '') {
return '';
}
if (!class_exists(\XSLTProcessor::class)) {
$this->logger->error('The PHP xsl extension is required to display course reserves.');
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);
$response = $this->httpClient->request('GET', self::BOOK_BAG_BASE_URL . rawurlencode($book_bag_id));
$atom = (string) $response->getBody();
if (empty($atom)) {
}
catch (GuzzleException $e) {
$this->logger->warning('Unable to retrieve bookbag @id: @message', [
'@id' => $book_bag_id,
'@message' => $e->getMessage(),
]);
return '';
}
if ($atom === '') {
return '';
}
catch (RequestException $e) {
$previous = libxml_use_internal_errors(TRUE);
try {
$xml = new \DOMDocument();
if (!$xml->loadXML($atom)) {
$this->logger->warning('Invalid Atom XML returned for bookbag @id.', ['@id' => $book_bag_id]);
return '';
}
$path = \Drupal::service('extension.list.module')->getPath('upei_roblib_reserves');
$xslt = new \XSLTProcessor();
$xsl = new \DOMDocument();
$xsl->load($path . '/xsl/atom2html.xsl');
$xsl->load($this->moduleExtensionList->getPath('upei_roblib_reserves') . '/xsl/atom2html.xsl');
$xslt = new \XSLTProcessor();
$xslt->importStylesheet($xsl);
$xml = new \DomDocument();
$xml->loadXML($atom);
$html = $xslt->transformToXML($xml);
}
return $html;
finally {
libxml_clear_errors();
libxml_use_internal_errors($previous);
}
return is_string($html) ? trim($html) : '';
}
}

5
upei_roblib_reserves.info.yml

@ -1,4 +1,7 @@
name: UPEI Robertson Library Reserves Module
type: module
core_version_requirement: ^9 || ^10
core_version_requirement: ^10 || ^11
description: A module to consume a bookbag from evergreen and display the results in Drupal
package: UPEI Roblib
dependencies:
- drupal:node

Loading…
Cancel
Save