Browse Source

Added conditional UTF-8 conversion

pull/907/head
Alan Stanley 7 years ago
parent
commit
3a1a32ec16
  1. 1
      islandora_fits.module
  2. 12
      src/Plugin/Field/FieldFormatter/FitsFormatter.php
  3. 1
      templates/fits.html.twig

1
islandora_fits.module

@ -51,6 +51,7 @@ function islandora_fits_theme($existing, $type, $theme, $path) {
'fits' => [
'variables' => [
'title' => 'FITS data',
'link' => NULL,
'data' => [],
],
],

12
src/Plugin/Field/FieldFormatter/FitsFormatter.php

@ -79,7 +79,13 @@ class FitsFormatter extends FormatterBase {
protected function viewValue(FieldItemInterface $item) {
$fileItem = $item->getValue();
$file = File::load($fileItem['target_id']);
$contents = utf8_encode(file_get_contents($file->getFileUri()));
$url = Url::fromUri($file->url());
$link = Link::fromTextAndUrl("Link to XML", $url);
$link = $link->toRenderable();
$contents = file_get_contents($file->getFileUri());
if (mb_detect_encoding($contents) != 'UTF-8') {
$contents = utf8_encode($contents);
}
$xml = new \SimpleXMLElement($contents);
$xml->registerXPathNamespace('fits', 'http://hul.harvard.edu/ois/xml/ns/fits/fits_output');
$fits_metadata = $this->islandora_fits_child_xpath($xml);
@ -133,8 +139,10 @@ class FitsFormatter extends FormatterBase {
$renderable = [
'#theme' => 'fits',
'#title' => $this->t("FITS metadata"),
'#link' => $link,
'#data' => $variables['islandora_fits_fieldsets'],
'#title' => $this->t("Fits Metadata"),
];
return \Drupal::service('renderer')->render($renderable);
}

1
templates/fits.html.twig

@ -1,4 +1,5 @@
<h1>{{ title }}</h1>
<p>{{ link }}</p>
<div>{{ data }}</div>

Loading…
Cancel
Save