Skip to content

Commit

Permalink
pkp/pkp-lib#7272 Simultaneously Displaying Multilingual Metadata on t…
Browse files Browse the repository at this point in the history
…he Article Landing Page
  • Loading branch information
jyhein committed Nov 27, 2023
1 parent f84ab62 commit 7b9e8ed
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 32 deletions.
44 changes: 44 additions & 0 deletions pages/preprint/PreprintHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use APP\facades\Repo;
use APP\handler\Handler;
use APP\observers\events\UsageEvent;
use APP\publication\Publication;
use APP\security\authorization\OpsServerMustPublishPolicy;
use APP\template\TemplateManager;
use Firebase\JWT\JWT;
Expand Down Expand Up @@ -169,6 +170,7 @@ public function initialize($request, $args = [])
*
* @param array $args
* @param \APP\core\Request $request
*
* @hook PreprintHandler::view [[&$request, &$preprint, $publication]]
* @hook PreprintHandler::view::galley [[&$request, &$this->galley, &$preprint, $publication]]
*/
Expand Down Expand Up @@ -294,6 +296,13 @@ public function view($args, $request)
$templateMgr->addHeader('canonical', '<link rel="canonical" href="' . $url . '">');
}

$templateMgr->assign('pubLocaleData', $this->getPublicationLocaleData(
$publication,
$context->getPrimaryLocale(),
$templateMgr->getTemplateVars('activeTheme')->getOption('showMultilingualMetadata') ?: [],
$templateMgr->getTemplateVars('activeTheme')->getOption('showMetadata') ?: []
));

if (!Hook::call('PreprintHandler::view', [&$request, &$preprint, $publication])) {
$templateMgr->display('frontend/pages/preprint.tpl');
event(new UsageEvent(Application::ASSOC_TYPE_SUBMISSION, $context, $preprint));
Expand Down Expand Up @@ -330,6 +339,7 @@ public function view($args, $request)
*
* @param array $args
* @param Request $request
*
* @hook PreprintHandler::download [[$this->preprint, &$this->galley, &$this->submissionFileId]]
* @hook FileManager::downloadFileFinished [[&$returner]]
*/
Expand Down Expand Up @@ -415,4 +425,38 @@ public function userCanViewGalley($request)
}
return false;
}

/**
* Format multilingual publication data for template:
* Default data at least includes in one language: full title, title, subtitle, keywords, abstract
* showMultilingualMetadataOpts adds multilingual metadata: title (by default includes fullTitle and subtitle), keywords, abstract, etc.
* showMetadataOpts: additional metadata
*/
protected function getPublicationLocaleData(Publication $publication, string $contextPrimaryLocale, array $showMultilingualMetadataOpts, array $showMetadataOpts): array
{
$titles = collect([
'title' => $publication->getTitles('html'),
'subtitle' => $publication->getSubtitles('html'),
'fullTitle' => $publication->getFullTitles('html'),
]);
$metadataOpts = collect(['keywords', 'abstract'])->concat($showMetadataOpts)->diff($titles->keys())->unique()->values();
$multilingualOpts = collect($showMultilingualMetadataOpts)
->when(in_array('title', $showMultilingualMetadataOpts), fn ($m) => $m->concat(['subtitle', 'fullTitle'])->unique()->values());
$primaryLocale = isset($titles->get('title')[$contextPrimaryLocale]) ? $contextPrimaryLocale : $publication->getData('locale');

$getText = fn (array $item, string $opt): array => [
$opt => [
'text' => ($text = array_filter($item, fn (string $locale) => $multilingualOpts->contains($opt) || $locale === $primaryLocale, ARRAY_FILTER_USE_KEY)),
'headingLang' => collect($text)->map(fn ($_, string $locale): string => $locale === $primaryLocale ? $contextPrimaryLocale : $locale)->toArray()
],
];

$pubLocaleData = $titles->mapWithKeys($getText)
->union($metadataOpts->mapWithKeys(fn (string $opt): array => $getText($publication->getData($opt) ?? [], $opt)));
return $pubLocaleData
->put('languages', $pubLocaleData->map(fn (array $item): array => array_keys($item['text']))
->flatten()->sort()->prepend($primaryLocale)->unique()->values())
->put('primaryLocale', $primaryLocale)
->toArray();
}
}
20 changes: 20 additions & 0 deletions plugins/themes/default/DefaultThemePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,26 @@ public function init()
'default' => 'none',
]);

$this->addOption('showMultilingualMetadata', 'FieldOptions', [
'label' => __('plugins.themes.default.option.metadata.label'),
'description' => __('plugins.themes.default.option.metadata.description'),
'options' => [
[
'value' => 'title',
'label' => __('submission.title'),
],
[
'value' => 'keywords',
'label' => __('common.keywords'),
],
[
'value' => 'abstract',
'label' => __('common.abstract'),
],
],
'default' => [],
]);

// Load primary stylesheet
$this->addStyle('stylesheet', 'styles/index.less');

Expand Down
14 changes: 13 additions & 1 deletion plugins/themes/default/locale/en/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,16 @@ msgid "plugins.themes.default.nextSlide"
msgstr "Next slide"

msgid "plugins.themes.default.prevSlide"
msgstr "Previous slide"
msgstr "Previous slide"

msgid "plugins.themes.default.option.metadata.label"
msgstr "Show preprint metadata on the preprint landing page"

msgid "plugins.themes.default.option.metadata.description"
msgstr "Select the preprint metadata to show in other languages."

msgid "plugins.themes.default.submissionMetadataInLanguage"
msgstr "Preprint Metadata in English"

msgid "plugins.themes.default.titleSubtitleSeparator"
msgstr " &mdash; "
12 changes: 7 additions & 5 deletions plugins/themes/default/styles/objects/preprint_details.less
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
margin-bottom: 0;
}

> h2 + p {
> h2 + p, h3 + p {
margin-top: 0;
}
}
Expand All @@ -52,6 +52,10 @@

.main_entry {

.metadata > .page_metadata_title {
margin: 0;
}

.item {

.label {
Expand Down Expand Up @@ -118,8 +122,7 @@
}
}

.item.doi,
.item.keywords {
.item.doi {
padding-top: 0;
}

Expand Down Expand Up @@ -302,8 +305,7 @@
font-weight: @bold;
}

&.doi .label,
&.keywords .label {
&.doi .label {
display: inline;
font-size: @font-base;
}
Expand Down
84 changes: 58 additions & 26 deletions templates/frontend/objects/preprint_details.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
* @uses $licenseUrl string URL to license. Only assigned if license should be
* included with published submissions.
* @uses $ccLicenseBadge string An image and text with details about the license
* @uses $pubLocaleData array Array of formatted publication locale metadata: titles, abstracts, keywords,
*}
<article class="obj_preprint_details">

Expand Down Expand Up @@ -109,13 +110,12 @@
<span class="separator">{translate key="navigation.breadcrumbSeparator"}</span>
<span class="preprint_version">{translate key="publication.version" version=$publication->getData('version')}</span>

<h1 class="page_title">
{$publication->getLocalizedTitle(null, 'html')|strip_unsafe_html}
<h1 class="page_title" lang="{$pubLocaleData.primaryLocale|replace:"_":"-"}">
{$pubLocaleData.title.text[$pubLocaleData.primaryLocale]|strip_unsafe_html}
</h1>

{if $publication->getLocalizedData('subtitle')}
<h2 class="subtitle">
{$publication->getLocalizedSubTitle(null, 'html')|strip_unsafe_html}
{if isset($pubLocaleData.subtitle.text[$pubLocaleData.primaryLocale])}
<h2 class="subtitle" lang="{$pubLocaleData.primaryLocale|replace:"_":"-"}">
{$pubLocaleData.subtitle.text[$pubLocaleData.primaryLocale]|strip_unsafe_html}
</h2>
{/if}

Expand Down Expand Up @@ -168,28 +168,60 @@
</section>
{/if}

{* Keywords *}
{if !empty($publication->getLocalizedData('keywords'))}
<section class="item keywords">
<h2 class="label">
{capture assign=translatedKeywords}{translate key="preprint.subject"}{/capture}
{translate key="semicolon" label=$translatedKeywords}
</h2>
<span class="value">
{foreach name="keywords" from=$publication->getLocalizedData('keywords') item="keyword"}
{$keyword|escape}{if !$smarty.foreach.keywords.last}{translate key="common.commaListSeparator"}{/if}
{/foreach}
</span>
</section>
{/if}
{*
* Show preprint keywords and abstract in ui, or submission, language by default.
* Show optional multilingual metadata: titles, keywords, abstracts.
*}
{foreach from=$pubLocaleData.languages item=lang}
<section class="metadata">
{assign "hLvl" "2"}
{* Multilingual metadata title *}
{if $lang !== $pubLocaleData.primaryLocale}
{assign "hLvl" "3"}
<h2 class="item label page_metadata_title" lang={$lang|replace:"_":"-"}>
{translate key="plugins.themes.default.submissionMetadataInLanguage" locale=$lang}
</h2>
{* Title in other language *}
{if isset($pubLocaleData.title.text[$lang])}
<section class="item page_locale_title">
<h{$hLvl} class="label" lang="{$pubLocaleData.title.headingLang[$lang]|replace:"_":"-"}">
{translate key="submission.title" locale=$pubLocaleData.title.headingLang[$lang]}
</h{$hLvl}>
<p lang="{$lang|replace:"_":"-"}">
{$pubLocaleData.title.text[$lang]|strip_tags}
{if isset($pubLocaleData.subtitle.text[$lang])}
{translate key="plugins.themes.default.titleSubtitleSeparator" locale=$pubLocaleData.title.headingLang[$lang]}{$pubLocaleData.subtitle.text[$lang]|strip_tags}
{/if}
</p>
</section>
{/if}
{/if}

{* Abstract *}
{if $publication->getLocalizedData('abstract')}
<section class="item abstract">
<h2 class="label">{translate key="common.abstract"}</h2>
{$publication->getLocalizedData('abstract')|strip_unsafe_html}
{* Keywords *}
{if isset($pubLocaleData.keywords.text[$lang])}
<section class="item keywords">
<h{$hLvl} class="label" lang="{$pubLocaleData.keywords.headingLang[$lang]|replace:"_":"-"}">
{translate key="common.keywords" locale=$pubLocaleData.keywords.headingLang[$lang]}
</h{$hLvl}>
<p class="value" lang="{$lang|replace:"_":"-"}">
{foreach from=$pubLocaleData.keywords.text[$lang] item="keyword"}
{$keyword|escape}{if !$keyword@last}{translate key="common.commaListSeparator" locale=$pubLocaleData.keywords.headingLang[$lang]}{/if}
{/foreach}
</p>
</section>
{/if}

{* Abstract *}
{if isset($pubLocaleData.abstract.text[$lang])}
<section class="item abstract">
<h{$hLvl} class="label" lang="{$pubLocaleData.abstract.headingLang[$lang]|replace:"_":"-"}">
{translate key="common.abstract" locale=$pubLocaleData.abstract.headingLang[$lang]}
</h{$hLvl}>
<p lang="{$lang|replace:"_":"-"}">{$pubLocaleData.abstract.text[$lang]|strip_tags}</p>
</section>
{/if}
</section>
{/if}
{/foreach}

{call_hook name="Templates::Preprint::Main"}

Expand Down

0 comments on commit 7b9e8ed

Please sign in to comment.