diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..fdd2d7a --- /dev/null +++ b/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": "*" + } +} diff --git a/src/Plugin/Block/ReserveBlock.php b/src/Plugin/Block/ReserveBlock.php index f56528c..cc60c62 100644 --- a/src/Plugin/Block/ReserveBlock.php +++ b/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,47 +104,65 @@ 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; } /** * 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'); + 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)) { - 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) { + $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 ''; + } + + try { + $response = $this->httpClient->request('GET', self::BOOK_BAG_BASE_URL . rawurlencode($book_bag_id)); + $atom = (string) $response->getBody(); + } + catch (GuzzleException $e) { + $this->logger->warning('Unable to retrieve bookbag @id: @message', [ + '@id' => $book_bag_id, + '@message' => $e->getMessage(), + ]); + return ''; + } + if ($atom === '') { + return ''; + } + + $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) : ''; } } diff --git a/upei_roblib_reserves.info.yml b/upei_roblib_reserves.info.yml index 1f4afe0..3362eb8 100644 --- a/upei_roblib_reserves.info.yml +++ b/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