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.
 
 
 

168 lines
4.8 KiB

<?php
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 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",
* admin_label = @Translation("Roblib Reserves Block"),
* category = @Translation("UPEI Roblib"),
* )
*/
#[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}
*/
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(): string {
$node = $this->routeMatch->getParameter('node');
if (!$node instanceof NodeInterface || $node->bundle() !== 'course_reserve' || !$node->hasField('field_bookbag_id')) {
return '';
}
$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 '';
}
$xsl = new \DOMDocument();
$xsl->load($this->moduleExtensionList->getPath('upei_roblib_reserves') . '/xsl/atom2html.xsl');
$xslt = new \XSLTProcessor();
$xslt->importStylesheet($xsl);
$html = $xslt->transformToXML($xml);
}
finally {
libxml_clear_errors();
libxml_use_internal_errors($previous);
}
return is_string($html) ? trim($html) : '';
}
}