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 2, 2023
1 parent ebc2d05 commit eeaeedd
Show file tree
Hide file tree
Showing 5 changed files with 132 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 = array_filter($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('title', $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['header'] = [];
foreach($locales as $locale) {
$item['header'][$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' => '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
6 changes: 6 additions & 0 deletions plugins/themes/default/locale/en/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,9 @@ msgstr "Downloads"

msgid "plugins.themes.default.displayStats.noStats"
msgstr "Download data is not yet available."

msgid "plugins.themes.default.option.metadata.label"
msgstr "Show submission metadata on the book landing page"

msgid "plugins.themes.default.option.metadata.description"
msgstr "Select the metadata to show in an books's other languages."
31 changes: 31 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,20 @@
margin-bottom: @quadruple;
}

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

> p {
font-size: @font-base;
margin-top: 0;
margin-bottom: @half;

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

.main_entry {

.item {
Expand All @@ -30,6 +44,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}
<p lang="{$locale|replace:"_":"-"}">
{$title|strip_unsafe_html}
</p>
{/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.header[$locale]|replace:"_":"-"}">
{capture assign=translatedKeywords}{translate key="common.keywords" locale=$pubLocData.keywords.header[$locale]}{/capture}
{translate key="semicolon" label=$translatedKeywords locale=$pubLocData.keywords.header[$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.header[$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.header[$locale]|replace:"_":"-"}">
{translate key="submission.synopsis" locale=$pubLocData.abstract.header[$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 eeaeedd

Please sign in to comment.