Skip to content

Commit

Permalink
Added an option to display a button to the public view of a resource.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Berthereau authored and Daniel Berthereau committed Jan 9, 2023
1 parent e77308c commit 66243f4
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 4 deletions.
101 changes: 97 additions & 4 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,21 @@ public function warnUninstall(Event $event): void

public function attachListeners(SharedEventManagerInterface $sharedEventManager): void
{
// Display a warn before uninstalling.
// Manage buttons in admin resources.
$sharedEventManager->attach(
'Omeka\Controller\Admin\Module',
'view.details',
[$this, 'warnUninstall']
'Omeka\Controller\Admin\Item',
'view.layout',
[$this, 'handleViewLayoutResource']
);
$sharedEventManager->attach(
'Omeka\Controller\Admin\ItemSet',
'view.layout',
[$this, 'handleViewLayoutResource']
);
$sharedEventManager->attach(
'Omeka\Controller\Admin\Media',
'view.layout',
[$this, 'handleViewLayoutResource']
);

// Content lockiing in admin board.
Expand Down Expand Up @@ -235,6 +245,89 @@ public function attachListeners(SharedEventManagerInterface $sharedEventManager)
'form.add_elements',
[$this, 'handleMainSettings']
);

// Display a warn before uninstalling.
$sharedEventManager->attach(
'Omeka\Controller\Admin\Module',
'view.details',
[$this, 'warnUninstall']
);
}

public function handleViewLayoutResource(Event $event): void
{
/** @var \Laminas\View\Renderer\PhpRenderer $view */
$view = $event->getTarget();
$params = $view->params()->fromRoute();
$action = $params['action'] ?? 'browse';
if ($action !== 'show') {
return;
}

$controller = $params['__CONTROLLER__'] ?? $params['controller'] ?? '';
$controllers = [
'item' => 'items',
'item-set' => 'item_sets',
'media' => 'media',
'Omeka\Controller\Admin\Item' => 'items',
'Omeka\Controller\Admin\ItemSet' => 'item_sets',
'Omeka\Controller\Admin\Media' => 'media',
];
if (!isset($controllers[$controller])) {
return;
}

// The resource is not available in the main view.
$id = isset($params['id']) ? (int) $params['id'] : 0;
if (!$id) {
return;
}

$plugins = $view->getHelperPluginManager();

$setting = $plugins->get('setting');
$interface = $setting('easyadmin_interface') ?: [];
if (!in_array('resource_public_view', $interface)) {
return;
}

$vars = $view->vars();
if ($vars->offsetExists('resource')) {
$resource = $vars->offsetGet('resource');
} else {
$api = $plugins->get('api');
try {
$resource = $api->read($controllers[$controller], ['id' => $id], ['initialize' => false, 'finalize' => false])->getContent();
} catch (\Exception $e) {
return;
}
}

$defaultSite = $plugins->get('defaultSite');
$defaultSiteSlug = $defaultSite('slug');
if (!$defaultSiteSlug) {
return;
}

$url = $view->publicResourceUrl($resource);
if (!$url) {
return;
}

$linkPublicView = $view->hyperlink(
$view->translate('Public view'), // @translate
$url,
['class' => 'button', 'target' => '_blank']
);

$html = preg_replace(
'~<div id="page-actions">(.*?)</div>~s',
'<div id="page-actions">' . $linkPublicView . ' $1 ' . '</div>',
$vars->offsetGet('content'),
1
);

$vars->offsetSet('content', $html);
}

public function contentLockingOnEdit(Event $event): void
Expand Down
3 changes: 3 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@
],
'easyadmin' => [
'settings' => [
'easyadmin_interface' => [
'resource_public_view',
],
'easyadmin_content_lock' => true,
// 86400 seconds = 24 hours.
'easyadmin_content_lock_duration' => 86400,
Expand Down
16 changes: 16 additions & 0 deletions src/Form/SettingsFieldset.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ public function init(): void
->setAttribute('id', 'easy-admin')
->setOption('element_groups', $this->elementGroups)

->add([
'name' => 'easyadmin_interface',
'type' => EasyAdminElement\OptionalMultiCheckbox::class,
'options' => [
'element_group' => 'resources',
'label' => 'Elements to display in resources admin pages', // @translate
'value_options' => [
'resource_public_view' => 'Button Public view', // @translate
],
'use_hidden_element' => true,
],
'attributes' => [
'id' => 'easyadmin_interface',
],
])

->add([
'name' => 'easyadmin_content_lock',
'type' => Element\Checkbox::class,
Expand Down

0 comments on commit 66243f4

Please sign in to comment.