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 Oct 12, 2023
1 parent 0c88b6a commit 48932e1
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 19 deletions.
37 changes: 37 additions & 0 deletions pages/catalog/CatalogBookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ public function book($args, $request)
$templateMgr->addHeader('canonical', '<link rel="canonical" href="' . $url . '">');
}

$templateMgr->assign('pubLocData', $this->getPublicationLocaleData($this->publication, $request->getContext()->getPrimaryLocale(), $submission->getData('locale'), $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 @@ -605,4 +607,39 @@ protected function getChaptersFirstPublishedDate(Submission $submission, Chapter

return null;
}

/**
* For book details, format display data.
*/
protected function getPublicationLocaleData(\APP\publication\Publication $publication, string $contextPrimaryLocale, string $submissionLocale, array $metadataOpts): array
{
$titles = $publication->getFullTitles('html');
$primaryLocale = isset($titles[$contextPrimaryLocale]) ? $contextPrimaryLocale : $submissionLocale;
$uiLocale = $contextPrimaryLocale;
$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 = [
'titles' => ['text' => in_array('titles', $metadataOpts) ? array_filter($titles, fn ($locale) => $locale !== $primaryLocale, ARRAY_FILTER_USE_KEY) : []],
'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['primaryLocale'] = $primaryLocale;

return $pubLocData;
}
}
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('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' => __('common.keywords'),
],
[
'value' => 'abstract',
'label' => __('submission.synopsis'),
],
],
'default' => [],
]);

// Load primary stylesheet
$this->addStyle('stylesheet', 'styles/index.less');

Expand Down
15 changes: 15 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,18 @@ 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.option.metadata.titleAndSubtitle"
msgstr "Title and subtitle"

msgid "plugins.themes.default.aria.multilingualSubmissionTitles"
msgstr "Book titles in other languages"

msgid "plugins.themes.default.titleSubtitleSeparator"
msgstr " &mdash; "
32 changes: 32 additions & 0 deletions plugins/themes/default/styles/objects/monograph_full.less
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@
margin-bottom: @quadruple;
}

> .locale_titles {
margin: @double 0 @triple 0;

> h2 {
font-size: @font-base;
font-weight: @normal;
margin-top: 0;
margin-bottom: @base;

&:last-of-type {
margin-bottom: 0;
}
}
}

.main_entry {

.item {
Expand All @@ -30,6 +45,23 @@
&:first-child {
padding-top: 0;
}


&.keywords > div {
margin-bottom: @triple;

&:last-of-type {
margin-bottom: 0;
}
}

&.abstracts .abstract {
margin-bottom: @quadruple;

&:last-of-type {
margin-bottom: 0;
}
}
}

.label {
Expand Down
57 changes: 38 additions & 19 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 $pubLocData array Array of formatted publication locale metadata: titles, abstracts, keywords,
*}
<div class="obj_monograph_full">

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

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

{if !empty(count($pubLocData.titles.text))}
<div class="locale_titles">
{foreach from=$pubLocData.titles.text key=locale item=title}
<h2 lang="{$locale|replace:"_":"-"}">
{$title|strip_unsafe_html}
</h2>
{/foreach}
</div>
{/if}

<div class="row">
<div class="main_entry">

Expand All @@ -120,29 +131,37 @@
{/if}

{* Keywords *}
{if !empty($publication->getLocalizedData('keywords'))}
{if !empty(count($pubLocData.keywords.text))}
<div class="item keywords">
<h2 class="label">
{capture assign=translatedKeywords}{translate key="common.keywords"}{/capture}
{translate key="semicolon" label=$translatedKeywords}
</h2>
<span class="value">
{foreach name="keywords" from=$publication->getLocalizedData('keywords') item=keyword}
{$keyword|escape}{if !$smarty.foreach.keywords.last}, {/if}
{/foreach}
</span>
{foreach from=$pubLocData.keywords.text key=locale item=keywords}
<div>
<h2 class="label" lang="{$pubLocData.keywords.heading[$locale]|replace:"_":"-"}">
{capture assign=translatedKeywords}{translate key="common.keywords" locale=$pubLocData.keywords.heading[$locale]}{/capture}
{translate key="semicolon" label=$translatedKeywords locale=$pubLocData.keywords.heading[$locale]}
</h2>
<span class="value" lang="{$locale|replace:"_":"-"}">
{foreach name="keywords" from=$keywords item="keyword"}
{$keyword|escape}{if !$smarty.foreach.keywords.last}{translate key="common.commaListSeparator" locale=$pubLocData.keywords.heading[$locale]}{/if}
{/foreach}
</span>
</div>
{/foreach}
</div>
{/if}

{* Abstract *}
<div class="item abstract">
<h2 class="label">
{translate key="submission.synopsis"}
</h2>
<div class="value">
{$publication->getLocalizedData('abstract')|strip_unsafe_html}
{if !empty(count($pubLocData.abstract.text))}
<div class="item abstracts">
{foreach from=$pubLocData.abstract.text key=locale item=abstract}
<div class="abstract">
<h2 class="label" lang="{$pubLocData.abstract.heading[$locale]|replace:"_":"-"}">
{translate key="submission.synopsis" locale=$pubLocData.abstract.heading[$locale]}
</h2>
<span lang="{$locale|replace:"_":"-"}">{$abstract|strip_unsafe_html}</span>
</div>
{/foreach}
</div>
</div>
{/if}

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

0 comments on commit 48932e1

Please sign in to comment.