From 889a42f96453d2e070172aaa53eb7a851e1a4449 Mon Sep 17 00:00:00 2001 From: jyhein <124268211+jyhein@users.noreply.github.com> Date: Thu, 12 Oct 2023 15:01:55 +0300 Subject: [PATCH] pkp/pkp-lib#7272 Simultaneously Displaying Multilingual Metadata on the Article Landing Page --- pages/preprint/PreprintHandler.php | 41 +++++++++++ plugins/themes/default/DefaultThemePlugin.php | 20 ++++++ plugins/themes/default/locale/en/locale.po | 17 ++++- .../styles/objects/preprint_details.less | 44 ++++++++++-- .../frontend/objects/preprint_details.tpl | 69 ++++++++++++------- 5 files changed, 161 insertions(+), 30 deletions(-) diff --git a/pages/preprint/PreprintHandler.php b/pages/preprint/PreprintHandler.php index 7e7ef6f035..fe1817ae0d 100644 --- a/pages/preprint/PreprintHandler.php +++ b/pages/preprint/PreprintHandler.php @@ -292,6 +292,8 @@ public function view($args, $request) $templateMgr->addHeader('canonical', ''); } + $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)); @@ -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; + } } diff --git a/plugins/themes/default/DefaultThemePlugin.php b/plugins/themes/default/DefaultThemePlugin.php index 99bea44fa8..d613046148 100755 --- a/plugins/themes/default/DefaultThemePlugin.php +++ b/plugins/themes/default/DefaultThemePlugin.php @@ -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'); diff --git a/plugins/themes/default/locale/en/locale.po b/plugins/themes/default/locale/en/locale.po index 22f90fc34b..6b9f589e95 100644 --- a/plugins/themes/default/locale/en/locale.po +++ b/plugins/themes/default/locale/en/locale.po @@ -97,4 +97,19 @@ msgid "plugins.themes.default.nextSlide" msgstr "Next slide" msgid "plugins.themes.default.prevSlide" -msgstr "Previous slide" \ No newline at end of file +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 " — " diff --git a/plugins/themes/default/styles/objects/preprint_details.less b/plugins/themes/default/styles/objects/preprint_details.less index 2cd82aa7f4..b2b626010d 100755 --- a/plugins/themes/default/styles/objects/preprint_details.less +++ b/plugins/themes/default/styles/objects/preprint_details.less @@ -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 { @@ -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 { diff --git a/templates/frontend/objects/preprint_details.tpl b/templates/frontend/objects/preprint_details.tpl index 9239d3cb42..c8903f8c08 100644 --- a/templates/frontend/objects/preprint_details.tpl +++ b/templates/frontend/objects/preprint_details.tpl @@ -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, *}
@@ -108,14 +109,26 @@ {translate key="navigation.breadcrumbSeparator"} {translate key="publication.version" version=$publication->getData('version')} -

- {$publication->getLocalizedTitle(null, 'html')|strip_unsafe_html} -

+
+

+ {$pubLocData.primaryTitle|strip_unsafe_html} +

+ {if $pubLocData.primarySubtitle} +

+ {$pubLocData.primarySubtitle|strip_unsafe_html} +

+ {/if} +
- {if $publication->getLocalizedData('subtitle')} -

- {$publication->getLocalizedSubTitle(null, 'html')|strip_unsafe_html} -

+ {if !empty(count($pubLocData.title.text))} +
+ {foreach from=$pubLocData.title.text key=locale item=title} +

+ {$title|strip_unsafe_html} + {if isset($pubLocData.subtitle.text[$locale])}{translate key="plugins.themes.default.titleSubtitleSeparator"}{$pubLocData.subtitle.text[$locale]|strip_unsafe_html}{/if} +

+ {/foreach} +
{/if}
@@ -168,25 +181,35 @@ {/if} {* Keywords *} - {if !empty($publication->getLocalizedData('keywords'))} -
-

- {capture assign=translatedKeywords}{translate key="preprint.subject"}{/capture} - {translate key="semicolon" label=$translatedKeywords} -

- - {foreach name="keywords" from=$publication->getLocalizedData('keywords') item="keyword"} - {$keyword|escape}{if !$smarty.foreach.keywords.last}{translate key="common.commaListSeparator"}{/if} - {/foreach} - -
+ {if !empty(count($pubLocData.keywords.text))} +
+ {foreach from=$pubLocData.keywords.text key=locale item=keywords} +
+

+ {capture assign=translatedKeywords}{translate key="preprint.subject" locale=$pubLocData.keywords.header[$locale]}{/capture} + {translate key="semicolon" label=$translatedKeywords locale=$pubLocData.keywords.header[$locale]} +

+ + {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} + +
+ {/foreach} +
{/if} {* Abstract *} - {if $publication->getLocalizedData('abstract')} -
-

{translate key="common.abstract"}

- {$publication->getLocalizedData('abstract')|strip_unsafe_html} + {if !empty(count($pubLocData.abstract.text))} +
+ {foreach from=$pubLocData.abstract.text key=locale item=abstract} +
+

+ {translate key="preprint.abstract" locale=$pubLocData.abstract.header[$locale]} +

+ {$abstract|strip_unsafe_html} +
+ {/foreach}
{/if}