Skip to content

Commit 889a42f

Browse files
committed
pkp/pkp-lib#7272 Simultaneously Displaying Multilingual Metadata on the Article Landing Page
1 parent ed9c07e commit 889a42f

File tree

5 files changed

+161
-30
lines changed

5 files changed

+161
-30
lines changed

pages/preprint/PreprintHandler.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ public function view($args, $request)
292292
$templateMgr->addHeader('canonical', '<link rel="canonical" href="' . $url . '">');
293293
}
294294

295+
$templateMgr->assign('pubLocData', $this->getPublicationLocaleData($publication, $context->getPrimaryLocale(), $preprint->getData('locale'), $templateMgr->getTemplateVars('activeTheme')->getOption('showMetadata') ?: []));
296+
295297
if (!Hook::call('PreprintHandler::view', [&$request, &$preprint, $publication])) {
296298
$templateMgr->display('frontend/pages/preprint.tpl');
297299
event(new UsageEvent(Application::ASSOC_TYPE_SUBMISSION, $context, $preprint));
@@ -411,4 +413,43 @@ public function userCanViewGalley($request)
411413
}
412414
return false;
413415
}
416+
417+
/**
418+
* For preprint details, format display data.
419+
*/
420+
protected function getPublicationLocaleData(\APP\publication\Publication $publication, string $contextPrimaryLocale, string $submissionLocale, array $metadataOpts): array
421+
{
422+
$titles = $publication->getTitles('html');
423+
$subtitles = $publication->getSubtitles('html');
424+
$primaryLocale = isset($titles[$contextPrimaryLocale]) ? $contextPrimaryLocale : $submissionLocale;
425+
$uiLocale = $contextPrimaryLocale;
426+
$getOtherTitles = fn ($tt) => in_array('titles', $metadataOpts) ? array_filter($tt, fn ($locale) => $locale !== $primaryLocale, ARRAY_FILTER_USE_KEY) : [];
427+
$getMdata = fn ($opt) => empty(count($mdata = array_filter($publication->getData($opt) ?? []))) || in_array($opt, $metadataOpts)
428+
? $mdata
429+
: (isset($mdata[$primaryLocale]) ? [$primaryLocale => $mdata[$primaryLocale]] : [$fk = array_key_first($mdata) => $mdata[$fk]]);
430+
431+
$pubLocData = [
432+
'title' => ['text' => $getOtherTitles($titles)],
433+
'subtitle' => ['text' => $getOtherTitles($subtitles)],
434+
'keywords' => ['text' => $getMdata('keywords')],
435+
'abstract' => ['text' => $getMdata('abstract')],
436+
];
437+
438+
foreach($pubLocData as $opt => &$item) {
439+
uksort($item['text'], fn ($a, $b) => $a === $primaryLocale ? -1 : ($b === $primaryLocale ? 1 : ($a < $b ? -1 : 1)));
440+
441+
$locales = array_keys($item['text']);
442+
$hasSameHeaderLocale = in_array($opt, $metadataOpts);
443+
$item['heading'] = [];
444+
foreach($locales as $locale) {
445+
$item['heading'][$locale] = $hasSameHeaderLocale ? $locale : $uiLocale;
446+
}
447+
}
448+
449+
$pubLocData['primaryTitle'] = $titles[$primaryLocale];
450+
$pubLocData['primarySubtitle'] = $subtitles[$primaryLocale] ?? null;
451+
$pubLocData['primaryLocale'] = $primaryLocale;
452+
453+
return $pubLocData;
454+
}
414455
}

plugins/themes/default/DefaultThemePlugin.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,26 @@ public function init()
125125
'default' => 'none',
126126
]);
127127

128+
$this->addOption('showMetadata', 'FieldOptions', [
129+
'label' => __('plugins.themes.default.option.metadata.label'),
130+
'description' => __('plugins.themes.default.option.metadata.description'),
131+
'options' => [
132+
[
133+
'value' => 'titles',
134+
'label' => __('plugins.themes.default.option.metadata.titleAndSubtitle'),
135+
],
136+
[
137+
'value' => 'keywords',
138+
'label' => __('preprint.subject'),
139+
],
140+
[
141+
'value' => 'abstract',
142+
'label' => __('preprint.abstract'),
143+
],
144+
],
145+
'default' => [],
146+
]);
147+
128148
// Load primary stylesheet
129149
$this->addStyle('stylesheet', 'styles/index.less');
130150

plugins/themes/default/locale/en/locale.po

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,19 @@ msgid "plugins.themes.default.nextSlide"
9797
msgstr "Next slide"
9898

9999
msgid "plugins.themes.default.prevSlide"
100-
msgstr "Previous slide"
100+
msgstr "Previous slide"
101+
102+
msgid "plugins.themes.default.option.metadata.label"
103+
msgstr "Show preprint metadata on the preprint landing page"
104+
105+
msgid "plugins.themes.default.option.metadata.description"
106+
msgstr "Select the preprint metadata to show in other languages."
107+
108+
msgid "plugins.themes.default.option.metadata.titleAndSubtitle"
109+
msgstr "Title and subtitle"
110+
111+
msgid "plugins.themes.default.aria.multilingualSubmissionTitles"
112+
msgstr "Preprint titles in other languages"
113+
114+
msgid "plugins.themes.default.titleSubtitleSeparator"
115+
msgstr " &mdash; "

plugins/themes/default/styles/objects/preprint_details.less

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,30 @@
1010
*/
1111
.obj_preprint_details {
1212

13-
> .page_title {
13+
.page_titles {
1414
margin: 0;
15+
16+
> .subtitle {
17+
font-size: @font-base;
18+
line-height: @line-lead;
19+
font-weight: @normal;
20+
}
1521
}
1622

17-
> .subtitle {
18-
margin: 0;
19-
font-size: @font-base;
20-
line-height: @line-lead;
21-
font-weight: @normal;
23+
.page_locale_titles {
24+
margin: @quadruple 0 @triple 0;
25+
26+
> h2 {
27+
font-size: @font-base;
28+
line-height: @line-lead;
29+
font-weight: @normal;
30+
margin-top: 0;
31+
margin-bottom: @base;
32+
33+
&:last-of-type {
34+
margin-bottom: 0;
35+
}
36+
}
2237
}
2338

2439
.row {
@@ -60,6 +75,23 @@
6075
font-size: @font-bump;
6176
font-weight: @bold;
6277
}
78+
79+
80+
&.keywords > section {
81+
margin-bottom: @base;
82+
83+
&:last-of-type {
84+
margin-bottom: 0;
85+
}
86+
}
87+
88+
&.abstracts .abstract {
89+
margin-bottom: @quadruple;
90+
91+
&:last-of-type {
92+
margin-bottom: 0;
93+
}
94+
}
6395
}
6496

6597
.sub_item {

templates/frontend/objects/preprint_details.tpl

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
* @uses $licenseUrl string URL to license. Only assigned if license should be
6464
* included with published submissions.
6565
* @uses $ccLicenseBadge string An image and text with details about the license
66+
* @uses $pubLocData array Array of formatted publication locale metadata: titles, abstracts, keywords,
6667
*}
6768
<article class="obj_preprint_details">
6869

@@ -108,14 +109,26 @@
108109
<span class="separator">{translate key="navigation.breadcrumbSeparator"}</span>
109110
<span class="preprint_version">{translate key="publication.version" version=$publication->getData('version')}</span>
110111

111-
<h1 class="page_title">
112-
{$publication->getLocalizedTitle(null, 'html')|strip_unsafe_html}
113-
</h1>
112+
<hgroup class="page_titles">
113+
<h1 class="page_title" lang="{$pubLocData.primaryLocale|replace:"_":"-"}">
114+
{$pubLocData.primaryTitle|strip_unsafe_html}
115+
</h1>
116+
{if $pubLocData.primarySubtitle}
117+
<h2 class="subtitle" lang="{$pubLocData.primaryLocale|replace:"_":"-"}">
118+
{$pubLocData.primarySubtitle|strip_unsafe_html}
119+
</h2>
120+
{/if}
121+
</hgroup>
114122

115-
{if $publication->getLocalizedData('subtitle')}
116-
<h2 class="subtitle">
117-
{$publication->getLocalizedSubTitle(null, 'html')|strip_unsafe_html}
118-
</h2>
123+
{if !empty(count($pubLocData.title.text))}
124+
<hgroup class="page_locale_titles" aria-label="{translate key="plugins.themes.default.aria.multilingualSubmissionTitles"}">
125+
{foreach from=$pubLocData.title.text key=locale item=title}
126+
<h2 lang="{$locale|replace:"_":"-"}">
127+
{$title|strip_unsafe_html}
128+
{if isset($pubLocData.subtitle.text[$locale])}{translate key="plugins.themes.default.titleSubtitleSeparator"}{$pubLocData.subtitle.text[$locale]|strip_unsafe_html}{/if}
129+
</h2>
130+
{/foreach}
131+
</hgroup>
119132
{/if}
120133

121134
<div class="row">
@@ -168,25 +181,35 @@
168181
{/if}
169182

170183
{* Keywords *}
171-
{if !empty($publication->getLocalizedData('keywords'))}
172-
<section class="item keywords">
173-
<h2 class="label">
174-
{capture assign=translatedKeywords}{translate key="preprint.subject"}{/capture}
175-
{translate key="semicolon" label=$translatedKeywords}
176-
</h2>
177-
<span class="value">
178-
{foreach name="keywords" from=$publication->getLocalizedData('keywords') item="keyword"}
179-
{$keyword|escape}{if !$smarty.foreach.keywords.last}{translate key="common.commaListSeparator"}{/if}
180-
{/foreach}
181-
</span>
182-
</section>
184+
{if !empty(count($pubLocData.keywords.text))}
185+
<section class="item keywords">
186+
{foreach from=$pubLocData.keywords.text key=locale item=keywords}
187+
<section>
188+
<h2 class="label" lang="{$pubLocData.keywords.header[$locale]|replace:"_":"-"}">
189+
{capture assign=translatedKeywords}{translate key="preprint.subject" locale=$pubLocData.keywords.header[$locale]}{/capture}
190+
{translate key="semicolon" label=$translatedKeywords locale=$pubLocData.keywords.header[$locale]}
191+
</h2>
192+
<span class="value" lang="{$locale|replace:"_":"-"}">
193+
{foreach name="keywords" from=$keywords item="keyword"}
194+
{$keyword|escape}{if !$smarty.foreach.keywords.last}{translate key="common.commaListSeparator" locale=$pubLocData.keywords.header[$locale]}{/if}
195+
{/foreach}
196+
</span>
197+
</section>
198+
{/foreach}
199+
</section>
183200
{/if}
184201

185202
{* Abstract *}
186-
{if $publication->getLocalizedData('abstract')}
187-
<section class="item abstract">
188-
<h2 class="label">{translate key="common.abstract"}</h2>
189-
{$publication->getLocalizedData('abstract')|strip_unsafe_html}
203+
{if !empty(count($pubLocData.abstract.text))}
204+
<section class="item abstracts">
205+
{foreach from=$pubLocData.abstract.text key=locale item=abstract}
206+
<section class="abstract">
207+
<h2 class="label" lang="{$pubLocData.abstract.header[$locale]|replace:"_":"-"}">
208+
{translate key="preprint.abstract" locale=$pubLocData.abstract.header[$locale]}
209+
</h2>
210+
<span lang="{$locale|replace:"_":"-"}">{$abstract|strip_unsafe_html}</span>
211+
</section>
212+
{/foreach}
190213
</section>
191214
{/if}
192215

0 commit comments

Comments
 (0)