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 Oct 12, 2023
1 parent ed9c07e commit 889a42f
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 30 deletions.
41 changes: 41 additions & 0 deletions pages/preprint/PreprintHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ public function view($args, $request)
$templateMgr->addHeader('canonical', '<link rel="canonical" href="' . $url . '">');
}

$templateMgr->assign('pubLocData', $this->getPublicationLocaleData($publication, $context->getPrimaryLocale(), $preprint->getData('locale'), $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 @@ -411,4 +413,43 @@ public function userCanViewGalley($request)
}
return false;
}

/**
* For preprint details, format display data.
*/
protected function getPublicationLocaleData(\APP\publication\Publication $publication, string $contextPrimaryLocale, string $submissionLocale, array $metadataOpts): array
{
$titles = $publication->getTitles('html');
$subtitles = $publication->getSubtitles('html');
$primaryLocale = isset($titles[$contextPrimaryLocale]) ? $contextPrimaryLocale : $submissionLocale;
$uiLocale = $contextPrimaryLocale;
$getOtherTitles = fn ($tt) => in_array('titles', $metadataOpts) ? array_filter($tt, fn ($locale) => $locale !== $primaryLocale, ARRAY_FILTER_USE_KEY) : [];
$getMdata = fn ($opt) => empty(count($mdata = array_filter($publication->getData($opt) ?? []))) || in_array($opt, $metadataOpts)
? $mdata
: (isset($mdata[$primaryLocale]) ? [$primaryLocale => $mdata[$primaryLocale]] : [$fk = array_key_first($mdata) => $mdata[$fk]]);

$pubLocData = [
'title' => ['text' => $getOtherTitles($titles)],
'subtitle' => ['text' => $getOtherTitles($subtitles)],
'keywords' => ['text' => $getMdata('keywords')],
'abstract' => ['text' => $getMdata('abstract')],
];

foreach($pubLocData as $opt => &$item) {
uksort($item['text'], fn ($a, $b) => $a === $primaryLocale ? -1 : ($b === $primaryLocale ? 1 : ($a < $b ? -1 : 1)));

$locales = array_keys($item['text']);
$hasSameHeaderLocale = in_array($opt, $metadataOpts);
$item['heading'] = [];
foreach($locales as $locale) {
$item['heading'][$locale] = $hasSameHeaderLocale ? $locale : $uiLocale;
}
}

$pubLocData['primaryTitle'] = $titles[$primaryLocale];
$pubLocData['primarySubtitle'] = $subtitles[$primaryLocale] ?? null;
$pubLocData['primaryLocale'] = $primaryLocale;

return $pubLocData;
}
}
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('showMetadata', 'FieldOptions', [
'label' => __('plugins.themes.default.option.metadata.label'),
'description' => __('plugins.themes.default.option.metadata.description'),
'options' => [
[
'value' => 'titles',
'label' => __('plugins.themes.default.option.metadata.titleAndSubtitle'),
],
[
'value' => 'keywords',
'label' => __('preprint.subject'),
],
[
'value' => 'abstract',
'label' => __('preprint.abstract'),
],
],
'default' => [],
]);

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

Expand Down
17 changes: 16 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,19 @@ 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.option.metadata.titleAndSubtitle"
msgstr "Title and subtitle"

msgid "plugins.themes.default.aria.multilingualSubmissionTitles"
msgstr "Preprint titles in other languages"

msgid "plugins.themes.default.titleSubtitleSeparator"
msgstr " &mdash; "
44 changes: 38 additions & 6 deletions plugins/themes/default/styles/objects/preprint_details.less
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,30 @@
*/
.obj_preprint_details {

> .page_title {
.page_titles {
margin: 0;

> .subtitle {
font-size: @font-base;
line-height: @line-lead;
font-weight: @normal;
}
}

> .subtitle {
margin: 0;
font-size: @font-base;
line-height: @line-lead;
font-weight: @normal;
.page_locale_titles {
margin: @quadruple 0 @triple 0;

> h2 {
font-size: @font-base;
line-height: @line-lead;
font-weight: @normal;
margin-top: 0;
margin-bottom: @base;

&:last-of-type {
margin-bottom: 0;
}
}
}

.row {
Expand Down Expand Up @@ -60,6 +75,23 @@
font-size: @font-bump;
font-weight: @bold;
}


&.keywords > section {
margin-bottom: @base;

&:last-of-type {
margin-bottom: 0;
}
}

&.abstracts .abstract {
margin-bottom: @quadruple;

&:last-of-type {
margin-bottom: 0;
}
}
}

.sub_item {
Expand Down
69 changes: 46 additions & 23 deletions templates/frontend/objects/preprint_details.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,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 $pubLocData array Array of formatted publication locale metadata: titles, abstracts, keywords,
*}
<article class="obj_preprint_details">

Expand Down Expand Up @@ -108,14 +109,26 @@
<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>
<hgroup class="page_titles">
<h1 class="page_title" lang="{$pubLocData.primaryLocale|replace:"_":"-"}">
{$pubLocData.primaryTitle|strip_unsafe_html}
</h1>
{if $pubLocData.primarySubtitle}
<h2 class="subtitle" lang="{$pubLocData.primaryLocale|replace:"_":"-"}">
{$pubLocData.primarySubtitle|strip_unsafe_html}
</h2>
{/if}
</hgroup>

{if $publication->getLocalizedData('subtitle')}
<h2 class="subtitle">
{$publication->getLocalizedSubTitle(null, 'html')|strip_unsafe_html}
</h2>
{if !empty(count($pubLocData.title.text))}
<hgroup class="page_locale_titles" aria-label="{translate key="plugins.themes.default.aria.multilingualSubmissionTitles"}">
{foreach from=$pubLocData.title.text key=locale item=title}
<h2 lang="{$locale|replace:"_":"-"}">
{$title|strip_unsafe_html}
{if isset($pubLocData.subtitle.text[$locale])}{translate key="plugins.themes.default.titleSubtitleSeparator"}{$pubLocData.subtitle.text[$locale]|strip_unsafe_html}{/if}
</h2>
{/foreach}
</hgroup>
{/if}

<div class="row">
Expand Down Expand Up @@ -168,25 +181,35 @@
{/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 !empty(count($pubLocData.keywords.text))}
<section class="item keywords">
{foreach from=$pubLocData.keywords.text key=locale item=keywords}
<section>
<h2 class="label" lang="{$pubLocData.keywords.header[$locale]|replace:"_":"-"}">
{capture assign=translatedKeywords}{translate key="preprint.subject" locale=$pubLocData.keywords.header[$locale]}{/capture}
{translate key="semicolon" label=$translatedKeywords locale=$pubLocData.keywords.header[$locale]}
</h2>
<span class="value" lang="{$locale|replace:"_":"-"}">
{foreach name="keywords" from=$keywords item="keyword"}
{$keyword|escape}{if !$smarty.foreach.keywords.last}{translate key="common.commaListSeparator" locale=$pubLocData.keywords.header[$locale]}{/if}
{/foreach}
</span>
</section>
{/foreach}
</section>
{/if}

{* Abstract *}
{if $publication->getLocalizedData('abstract')}
<section class="item abstract">
<h2 class="label">{translate key="common.abstract"}</h2>
{$publication->getLocalizedData('abstract')|strip_unsafe_html}
{if !empty(count($pubLocData.abstract.text))}
<section class="item abstracts">
{foreach from=$pubLocData.abstract.text key=locale item=abstract}
<section class="abstract">
<h2 class="label" lang="{$pubLocData.abstract.header[$locale]|replace:"_":"-"}">
{translate key="preprint.abstract" locale=$pubLocData.abstract.header[$locale]}
</h2>
<span lang="{$locale|replace:"_":"-"}">{$abstract|strip_unsafe_html}</span>
</section>
{/foreach}
</section>
{/if}

Expand Down

0 comments on commit 889a42f

Please sign in to comment.