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) : ''; } }