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 Nov 27, 2023
1 parent 03a7259 commit 34c9cf2
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 25 deletions.
41 changes: 41 additions & 0 deletions pages/catalog/CatalogBookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,13 @@ public function book($args, $request)
$templateMgr->addHeader('canonical', '<link rel="canonical" href="' . $url . '">');
}

$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');
Expand Down Expand Up @@ -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();
}
}
20 changes: 20 additions & 0 deletions plugins/themes/default/DefaultThemePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
9 changes: 9 additions & 0 deletions plugins/themes/default/locale/en/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"
5 changes: 3 additions & 2 deletions plugins/themes/default/styles/objects/monograph_full.less
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@

.main_entry {

.item {
padding: 30px 0;
.item,
.metadata {
padding: @double 0;

&:first-child {
padding-top: 0;
Expand Down
74 changes: 51 additions & 23 deletions templates/frontend/objects/monograph_full.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
*}
<div class="obj_monograph_full">

Expand All @@ -93,8 +94,8 @@
</div>
{/if}

<h1 class="title">
{$publication->getLocalizedFullTitle(null, 'html')|strip_unsafe_html}
<h1 class="title" lang="{$pubLocaleData.primaryLocale|replace:"_":"-"}">
{$pubLocaleData.fullTitle.text[$pubLocaleData.primaryLocale]|strip_unsafe_html}
</h1>

<div class="row">
Expand All @@ -119,30 +120,57 @@
</div>
{/if}

{* Keywords *}
{if !empty($publication->getLocalizedData('keywords'))}
<div class="item keywords">
<h2 class="label">
{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}
<div class="metadata">
{assign "hLvl" "2"}
{* Multilingual metadata title *}
{if $lang !== $pubLocaleData.primaryLocale}
{assign "hLvl" "3"}
<h2 class="label page_metadata_title" lang={$lang|replace:"_":"-"}>
{translate key="plugins.themes.default.submissionMetadataInLanguage" locale=$lang}
</h2>
<span class="value">
{foreach name="keywords" from=$publication->getLocalizedData('keywords') item=keyword}
{$keyword|escape}{if !$smarty.foreach.keywords.last}, {/if}
{/foreach}
</span>
</div>
{/if}
{* Titles in other language *}
{if isset($pubLocaleData.fullTitle.text[$lang])}
<div class="item page_locale_title">
<h{$hLvl} class="label" lang="{$pubLocaleData.fullTitle.headingLang[$lang]|replace:"_":"-"}">
{translate key="submission.title" locale=$pubLocaleData.fullTitle.headingLang[$lang]}
</h{$hLvl}>
<p lang="{$lang|replace:"_":"-"}">
{$pubLocaleData.fullTitle.text[$lang]|strip_tags}
</p>
</div>
{/if}
{/if}

{* Abstract *}
<div class="item abstract">
<h2 class="label">
{translate key="submission.synopsis"}
</h2>
<div class="value">
{$publication->getLocalizedData('abstract')|strip_unsafe_html}
{* Keywords *}
{if isset($pubLocaleData.keywords.text[$lang])}
<div class="item keywords">
<h{$hLvl} class="label" lang="{$pubLocaleData.keywords.headingLang[$lang]|replace:"_":"-"}">
{translate key="common.keywords" locale=$pubLocaleData.keywords.headingLang[$lang]}
</h{$hLvl}>
<p class="value" lang="{$lang|replace:"_":"-"}">
{foreach from=$pubLocaleData.keywords.text[$lang] item="keyword"}
{$keyword|escape}{if !$keyword@last}{translate key="common.commaListSeparator" locale=$pubLocaleData.keywords.headingLang[$lang]}{/if}
{/foreach}
</p>
</div>
{/if}

{* Abstract *}
{if isset($pubLocaleData.abstract.text[$lang])}
<div class="item abstract">
<h{$hLvl} class="label" lang="{$pubLocaleData.abstract.headingLang[$lang]|replace:"_":"-"}">
{translate key="submission.synopsis" locale=$pubLocaleData.abstract.headingLang[$lang]}
</h{$hLvl}>
<p lang="{$lang|replace:"_":"-"}">{$pubLocaleData.abstract.text[$lang]|strip_tags}</p>
</div>
{/if}
</div>
</div>
{/foreach}

{* Chapters *}
{if $chapters|@count}
Expand Down

0 comments on commit 34c9cf2

Please sign in to comment.