From 34c9cf2a0072a6688aeaae6eab63d37c25657d45 Mon Sep 17 00:00:00 2001 From: jyhein <124268211+jyhein@users.noreply.github.com> Date: Mon, 27 Nov 2023 12:23:02 +0200 Subject: [PATCH] pkp/pkp-lib#7272 Simultaneously Displaying Multilingual Metadata on the Article Landing Page --- pages/catalog/CatalogBookHandler.php | 41 ++++++++++ plugins/themes/default/DefaultThemePlugin.php | 20 +++++ plugins/themes/default/locale/en/locale.po | 9 +++ .../styles/objects/monograph_full.less | 5 +- templates/frontend/objects/monograph_full.tpl | 74 +++++++++++++------ 5 files changed, 124 insertions(+), 25 deletions(-) diff --git a/pages/catalog/CatalogBookHandler.php b/pages/catalog/CatalogBookHandler.php index 1e67c74858b..add76a40178 100755 --- a/pages/catalog/CatalogBookHandler.php +++ b/pages/catalog/CatalogBookHandler.php @@ -300,6 +300,13 @@ public function book($args, $request) $templateMgr->addHeader('canonical', ''); } + $templateMgr->assign('pubLocaleData', $this->getPublicationLocaleData( + $this->publication, + $request->getContext()->getPrimaryLocale(), + $templateMgr->getTemplateVars('activeTheme')->getOption('showMultilingualMetadata') ?: [], + $templateMgr->getTemplateVars('activeTheme')->getOption('showMetadata') ?: [] + )); + // Display if (!Hook::call('CatalogBookHandler::book', [&$request, &$submission, &$this->publication, &$this->chapter])) { $templateMgr->display('frontend/pages/book.tpl'); @@ -611,4 +618,38 @@ protected function getChaptersFirstPublishedDate(Submission $submission, Chapter return null; } + + /** + * 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(); + } } diff --git a/plugins/themes/default/DefaultThemePlugin.php b/plugins/themes/default/DefaultThemePlugin.php index 49a9c148ac4..4029f48bdb5 100644 --- a/plugins/themes/default/DefaultThemePlugin.php +++ b/plugins/themes/default/DefaultThemePlugin.php @@ -126,6 +126,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' => __('submission.synopsis'), + ], + ], + '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 92ba3303c69..6247431c25a 100644 --- a/plugins/themes/default/locale/en/locale.po +++ b/plugins/themes/default/locale/en/locale.po @@ -101,3 +101,12 @@ msgstr "Next slide" msgid "plugins.themes.default.prevSlide" msgstr "Previous slide" + +msgid "plugins.themes.default.option.metadata.label" +msgstr "Show book metadata on the book landing page" + +msgid "plugins.themes.default.option.metadata.description" +msgstr "Select the book metadata to show in other languages." + +msgid "plugins.themes.default.submissionMetadataInLanguage" +msgstr "Book Metadata in English" diff --git a/plugins/themes/default/styles/objects/monograph_full.less b/plugins/themes/default/styles/objects/monograph_full.less index 057bc84e016..2ba0411bc3c 100644 --- a/plugins/themes/default/styles/objects/monograph_full.less +++ b/plugins/themes/default/styles/objects/monograph_full.less @@ -24,8 +24,9 @@ .main_entry { - .item { - padding: 30px 0; + .item, + .metadata { + padding: @double 0; &:first-child { padding-top: 0; diff --git a/templates/frontend/objects/monograph_full.tpl b/templates/frontend/objects/monograph_full.tpl index 0999f472097..e0605307aee 100644 --- a/templates/frontend/objects/monograph_full.tpl +++ b/templates/frontend/objects/monograph_full.tpl @@ -71,6 +71,7 @@ * @uses $licenseUrl string The URL which provides license information. * @uses $ccLicenseBadge string An HTML string containing a CC license image and * text. Only appears when license URL matches a known CC license. + * @uses $pubLocaleData array Array of formatted publication locale metadata: titles, abstracts, keywords, *}
@@ -93,8 +94,8 @@
{/if} -

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

+ {$pubLocaleData.fullTitle.text[$pubLocaleData.primaryLocale]|strip_unsafe_html}

@@ -119,30 +120,57 @@
{/if} - {* Keywords *} - {if !empty($publication->getLocalizedData('keywords'))} -
-

- {capture assign=translatedKeywords}{translate key="common.keywords"}{/capture} - {translate key="semicolon" label=$translatedKeywords} + {* + * Show article keywords and abstract in ui, or submission, language by default. + * Show optional multilingual metadata: titles, keywords, abstracts. + *} + {foreach from=$pubLocaleData.languages item=lang} + - {/if} + {* Titles in other language *} + {if isset($pubLocaleData.fullTitle.text[$lang])} +
+ + {translate key="submission.title" locale=$pubLocaleData.fullTitle.headingLang[$lang]} + +

+ {$pubLocaleData.fullTitle.text[$lang]|strip_tags} +

+
+ {/if} + {/if} - {* Abstract *} -
-

- {translate key="submission.synopsis"} -

-
- {$publication->getLocalizedData('abstract')|strip_unsafe_html} + {* Keywords *} + {if isset($pubLocaleData.keywords.text[$lang])} +
+ + {translate key="common.keywords" locale=$pubLocaleData.keywords.headingLang[$lang]} + +

+ {foreach from=$pubLocaleData.keywords.text[$lang] item="keyword"} + {$keyword|escape}{if !$keyword@last}{translate key="common.commaListSeparator" locale=$pubLocaleData.keywords.headingLang[$lang]}{/if} + {/foreach} +

+
+ {/if} + + {* Abstract *} + {if isset($pubLocaleData.abstract.text[$lang])} +
+ + {translate key="submission.synopsis" locale=$pubLocaleData.abstract.headingLang[$lang]} + +

{$pubLocaleData.abstract.text[$lang]|strip_tags}

+
+ {/if}
-
+ {/foreach} {* Chapters *} {if $chapters|@count}