Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IIIF block layouts #2247

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions application/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,8 @@
'itemWithMetadata' => Site\BlockLayout\ItemWithMetadata::class,
'pageDateTime' => Site\BlockLayout\PageDateTime::class,
'blockGroup' => Site\BlockLayout\BlockGroup::class,
'iiifPresentation' => Site\BlockLayout\IiifPresentation::class,
'iiifImage' => Site\BlockLayout\IiifImage::class,
],
'factories' => [
'asset' => Service\BlockLayout\AssetFactory::class,
Expand Down
88 changes: 88 additions & 0 deletions application/src/Site/BlockLayout/IiifImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
namespace Omeka\Site\BlockLayout;

use Laminas\View\Renderer\PhpRenderer;
use Omeka\Api\Representation\SitePageBlockRepresentation;
use Omeka\Api\Representation\SitePageRepresentation;
use Omeka\Api\Representation\SiteRepresentation;
use Omeka\Entity\SitePageBlock;
use Omeka\Stdlib\ErrorStore;

class IiifImage extends AbstractBlockLayout
{
protected $defaultBlockData = [
'image_url' => null,
];

public function getLabel()
{
return 'IIIF image'; // @translate
}

public function form(PhpRenderer $view, SiteRepresentation $site, SitePageRepresentation $page = null, SitePageBlockRepresentation $block = null)
{
// Build the form.
$form = new \Laminas\Form\Form;
$form->add([
'type' => 'text',
'name' => 'o:block[__blockIndex__][o:data][image_url]',
'options' => [
'label' => 'Image URL', // @translate
],
]);

// Set form data.
$blockData = $this->getBlockData($block);
$form->setData([
'o:block[__blockIndex__][o:data][image_url]' => $blockData['image_url'],
]);

// Render the form.
return $view->formCollection($form);
}

public function onHydrate(SitePageBlock $block, ErrorStore $errorStore)
{
// Sanitize user data.
$blockData = $block->getData();
$blockData['image_url'] = is_string($blockData['image_url'])
? trim($blockData['image_url'])
: $this->defaultBlockData['image_url'];
$block->setData($blockData);
}

public function render(PhpRenderer $view, SitePageBlockRepresentation $block, $templateViewScript = 'common/faceted-browse/block-layout/faceted-browse-preview')
{
$blockData = $this->getBlockData($block);
if (!$blockData['image_url']) {
return;
}

$tileSourcesUrl = $view->assetUrl('vendor/openseadragon/openseadragon.min.js', 'Omeka');
$prefixUrl = $view->assetUrl('vendor/openseadragon/images/', 'Omeka', false, false);
$view->headScript()->appendFile($tileSourcesUrl);
$html = <<<'HTML'
<div id="%1$s" style="height: 400px;"></div>
<script type="text/javascript">
OpenSeadragon({
id: "%1$s",
prefixUrl: "%2$s",
tileSources: "%3$s"
});
</script>
HTML;
return sprintf(
$html,
sprintf('iiif-image-%s', $block->id()),
$view->escapeJs($prefixUrl),
$view->escapeJs($blockData['image_url'])
);
}

public function getBlockData(?SitePageBlockRepresentation $block)
{
$blockData = $block ? $block->data() : [];
$blockData = array_merge($this->defaultBlockData, $blockData);
return $blockData;
}
}
75 changes: 75 additions & 0 deletions application/src/Site/BlockLayout/IiifPresentation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
namespace Omeka\Site\BlockLayout;

use Laminas\View\Renderer\PhpRenderer;
use Omeka\Api\Representation\SitePageBlockRepresentation;
use Omeka\Api\Representation\SitePageRepresentation;
use Omeka\Api\Representation\SiteRepresentation;
use Omeka\Entity\SitePageBlock;
use Omeka\Stdlib\ErrorStore;

class IiifPresentation extends AbstractBlockLayout
{
protected $defaultBlockData = [
'manifest_url' => null,
];

public function getLabel()
{
return 'IIIF presentation'; // @translate
}

public function form(PhpRenderer $view, SiteRepresentation $site, SitePageRepresentation $page = null, SitePageBlockRepresentation $block = null)
{
// Build the form.
$form = new \Laminas\Form\Form;
$form->add([
'type' => 'text',
'name' => 'o:block[__blockIndex__][o:data][manifest_url]',
'options' => [
'label' => 'Manifest URL', // @translate
],
]);

// Set form data.
$blockData = $this->getBlockData($block);
$form->setData([
'o:block[__blockIndex__][o:data][manifest_url]' => $blockData['manifest_url'],
]);

// Render the form.
return $view->formCollection($form);
}

public function onHydrate(SitePageBlock $block, ErrorStore $errorStore)
{
// Sanitize user data.
$blockData = $block->getData();
$blockData['manifest_url'] = is_string($blockData['manifest_url'])
? trim($blockData['manifest_url'])
: $this->defaultBlockData['manifest_url'];
$block->setData($blockData);
}

public function render(PhpRenderer $view, SitePageBlockRepresentation $block, $templateViewScript = 'common/faceted-browse/block-layout/faceted-browse-preview')
{
$blockData = $this->getBlockData($block);
if (!$blockData['manifest_url']) {
return;
}
$query = [
'url' => $blockData['manifest_url'],
'mirador_config' => json_encode([
'window.sideBarOpen' => true,
]),
];
return $view->iiifViewer($query, []);
}

public function getBlockData(?SitePageBlockRepresentation $block)
{
$blockData = $block ? $block->data() : [];
$blockData = array_merge($this->defaultBlockData, $blockData);
return $blockData;
}
}