Skip to content

Commit eeaeedd

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

File tree

5 files changed

+132
-19
lines changed

5 files changed

+132
-19
lines changed

pages/catalog/CatalogBookHandler.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,8 @@ public function book($args, $request)
298298
$templateMgr->addHeader('canonical', '<link rel="canonical" href="' . $url . '">');
299299
}
300300

301+
$templateMgr->assign('pubLocData', $this->getPublicationLocaleData($this->publication, $request->getContext()->getPrimaryLocale(), $submission->getData('locale'), $templateMgr->getTemplateVars('activeTheme')->getOption('showMetadata') ?: []));
302+
301303
// Display
302304
if (!Hook::call('CatalogBookHandler::book', [&$request, &$submission, &$this->publication, &$this->chapter])) {
303305
$templateMgr->display('frontend/pages/book.tpl');
@@ -605,4 +607,39 @@ protected function getChaptersFirstPublishedDate(Submission $submission, Chapter
605607

606608
return null;
607609
}
610+
611+
/**
612+
* For book details, format display data.
613+
*/
614+
protected function getPublicationLocaleData(\APP\publication\Publication $publication, string $contextPrimaryLocale, string $submissionLocale, array $metadataOpts): array
615+
{
616+
$titles = array_filter($publication->getFullTitles('html'));
617+
$primaryLocale = isset($titles[$contextPrimaryLocale]) ? $contextPrimaryLocale : $submissionLocale;
618+
$uiLocale = $contextPrimaryLocale;
619+
$getMData = fn ($opt) => empty(count($mdata = array_filter($publication->getData($opt) ?? []))) || in_array($opt, $metadataOpts)
620+
? $mdata
621+
: (isset($mdata[$primaryLocale]) ? [$primaryLocale => $mdata[$primaryLocale]] : [$fk = array_key_first($mdata) => $mdata[$fk]]);
622+
623+
$pubLocData = [
624+
'titles' => ['text' => in_array('title', $metadataOpts) ? array_filter($titles, fn ($locale) => $locale !== $primaryLocale, ARRAY_FILTER_USE_KEY) : []],
625+
'keywords' => ['text' => $getMData('keywords')],
626+
'abstract' => ['text' => $getMData('abstract')],
627+
];
628+
629+
foreach($pubLocData as $opt => &$item) {
630+
uksort($item['text'], fn ($a, $b) => $a === $primaryLocale ? -1 : ($b === $primaryLocale ? 1 : ($a < $b ? -1 : 1)));
631+
632+
$locales = array_keys($item['text']);
633+
$hasSameHeaderLocale = in_array($opt, $metadataOpts);
634+
$item['header'] = [];
635+
foreach($locales as $locale) {
636+
$item['header'][$locale] = $hasSameHeaderLocale ? $locale : $uiLocale;
637+
}
638+
}
639+
640+
$pubLocData['primaryTitle'] = $titles[$primaryLocale];
641+
$pubLocData['primaryLocale'] = $primaryLocale;
642+
643+
return $pubLocData;
644+
}
608645
}

plugins/themes/default/DefaultThemePlugin.php

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

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

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,9 @@ msgstr "Downloads"
9595

9696
msgid "plugins.themes.default.displayStats.noStats"
9797
msgstr "Download data is not yet available."
98+
99+
msgid "plugins.themes.default.option.metadata.label"
100+
msgstr "Show submission metadata on the book landing page"
101+
102+
msgid "plugins.themes.default.option.metadata.description"
103+
msgstr "Select the metadata to show in an books's other languages."

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@
2222
margin-bottom: @quadruple;
2323
}
2424

25+
> .locale_titles {
26+
margin: @double 0 @triple 0;
27+
28+
> p {
29+
font-size: @font-base;
30+
margin-top: 0;
31+
margin-bottom: @half;
32+
33+
&:last-of-type {
34+
margin-bottom: 0;
35+
}
36+
}
37+
}
38+
2539
.main_entry {
2640

2741
.item {
@@ -30,6 +44,23 @@
3044
&:first-child {
3145
padding-top: 0;
3246
}
47+
48+
49+
&.keywords > div {
50+
margin-bottom: @triple;
51+
52+
&:last-of-type {
53+
margin-bottom: 0;
54+
}
55+
}
56+
57+
&.abstracts .abstract {
58+
margin-bottom: @quadruple;
59+
60+
&:last-of-type {
61+
margin-bottom: 0;
62+
}
63+
}
3364
}
3465

3566
.label {

templates/frontend/objects/monograph_full.tpl

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
* @uses $licenseUrl string The URL which provides license information.
7272
* @uses $ccLicenseBadge string An HTML string containing a CC license image and
7373
* text. Only appears when license URL matches a known CC license.
74+
* @uses $pubLocData array Array of formatted publication locale metadata: titles, abstracts, keywords,
7475
*}
7576
<div class="obj_monograph_full">
7677

@@ -93,10 +94,20 @@
9394
</div>
9495
{/if}
9596

96-
<h1 class="title">
97-
{$publication->getLocalizedFullTitle(null, 'html')|strip_unsafe_html}
97+
<h1 class="title" lang="{$pubLocData.primaryLocale|replace:"_":"-"}">
98+
{$pubLocData.primaryTitle|strip_unsafe_html}
9899
</h1>
99100

101+
{if !empty(count($pubLocData.titles.text))}
102+
<div class="locale_titles">
103+
{foreach from=$pubLocData.titles.text key=locale item=title}
104+
<p lang="{$locale|replace:"_":"-"}">
105+
{$title|strip_unsafe_html}
106+
</p>
107+
{/foreach}
108+
</div>
109+
{/if}
110+
100111
<div class="row">
101112
<div class="main_entry">
102113

@@ -120,29 +131,37 @@
120131
{/if}
121132

122133
{* Keywords *}
123-
{if !empty($publication->getLocalizedData('keywords'))}
134+
{if !empty(count($pubLocData.keywords.text))}
124135
<div class="item keywords">
125-
<h2 class="label">
126-
{capture assign=translatedKeywords}{translate key="common.keywords"}{/capture}
127-
{translate key="semicolon" label=$translatedKeywords}
128-
</h2>
129-
<span class="value">
130-
{foreach name="keywords" from=$publication->getLocalizedData('keywords') item=keyword}
131-
{$keyword|escape}{if !$smarty.foreach.keywords.last}, {/if}
132-
{/foreach}
133-
</span>
136+
{foreach from=$pubLocData.keywords.text key=locale item=keywords}
137+
<div>
138+
<h2 class="label" lang="{$pubLocData.keywords.header[$locale]|replace:"_":"-"}">
139+
{capture assign=translatedKeywords}{translate key="common.keywords" locale=$pubLocData.keywords.header[$locale]}{/capture}
140+
{translate key="semicolon" label=$translatedKeywords locale=$pubLocData.keywords.header[$locale]}
141+
</h2>
142+
<span class="value" lang="{$locale|replace:"_":"-"}">
143+
{foreach name="keywords" from=$keywords item="keyword"}
144+
{$keyword|escape}{if !$smarty.foreach.keywords.last}{translate key="common.commaListSeparator" locale=$pubLocData.keywords.header[$locale]}{/if}
145+
{/foreach}
146+
</span>
147+
</div>
148+
{/foreach}
134149
</div>
135150
{/if}
136151

137152
{* Abstract *}
138-
<div class="item abstract">
139-
<h2 class="label">
140-
{translate key="submission.synopsis"}
141-
</h2>
142-
<div class="value">
143-
{$publication->getLocalizedData('abstract')|strip_unsafe_html}
153+
{if !empty(count($pubLocData.abstract.text))}
154+
<div class="item abstracts">
155+
{foreach from=$pubLocData.abstract.text key=locale item=abstract}
156+
<div class="abstract">
157+
<h2 class="label" lang="{$pubLocData.abstract.header[$locale]|replace:"_":"-"}">
158+
{translate key="submission.synopsis" locale=$pubLocData.abstract.header[$locale]}
159+
</h2>
160+
<span lang="{$locale|replace:"_":"-"}">{$abstract|strip_unsafe_html}</span>
161+
</div>
162+
{/foreach}
144163
</div>
145-
</div>
164+
{/if}
146165

147166
{* Chapters *}
148167
{if $chapters|@count}

0 commit comments

Comments
 (0)