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 9d37378 commit f6308d1
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 21 deletions.
37 changes: 37 additions & 0 deletions pages/preprint/PreprintHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ public function view($args, $request)
$templateMgr->addHeader('canonical', '<link rel="canonical" href="' . $url . '">');
}

$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));
Expand Down Expand Up @@ -411,4 +413,39 @@ 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 = array_filter($publication->getTitles('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 @@ -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' => 'title',
'label' => __('submission.title'),
],
[
'value' => 'keywords',
'label' => __('preprint.subject'),
],
[
'value' => 'abstract',
'label' => __('preprint.abstract'),
],
],
'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 @@ -92,3 +92,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 preprint landing page"

msgid "plugins.themes.default.option.metadata.description"
msgstr "Select the metadata to show in an preprint's other languages."
31 changes: 31 additions & 0 deletions plugins/themes/default/styles/objects/preprint_details.less
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@
margin: 0;
}

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

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

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

> .subtitle {
margin: 0;
font-size: @font-base;
Expand Down Expand Up @@ -60,6 +74,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 {
Expand Down
63 changes: 42 additions & 21 deletions templates/frontend/objects/preprint_details.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
*}
<article class="obj_preprint_details">

Expand Down Expand Up @@ -108,13 +109,23 @@
<span class="separator">{translate key="navigation.breadcrumbSeparator"}</span>
<span class="preprint_version">{translate key="publication.version" version=$publication->getData('version')}</span>

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

{if $publication->getLocalizedData('subtitle')}
<h2 class="subtitle">
{$publication->getLocalizedSubTitle(null, 'html')|strip_unsafe_html}
{if !empty(count($pubLocData.titles.text))}
<section class="page_locale_titles">
{foreach from=$pubLocData.titles.text key=locale item=title}
<p lang="{$locale|replace:"_":"-"}">
{$title|strip_unsafe_html}
</p>
{/foreach}
</section>
{/if}

{if $publication->getData('subtitle', $pubLocData.primaryLocale)}
<h2 class="subtitle" lang="{$pubLocData.primaryLocale|replace:"_":"-"}">
{$publication->getData('subtitle', $pubLocData.primaryLocale)|strip_unsafe_html}
</h2>
{/if}

Expand Down Expand Up @@ -168,25 +179,35 @@
{/if}

{* Keywords *}
{if !empty($publication->getLocalizedData('keywords'))}
<section class="item keywords">
<h2 class="label">
{capture assign=translatedKeywords}{translate key="preprint.subject"}{/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}{translate key="common.commaListSeparator"}{/if}
{/foreach}
</span>
</section>
{if !empty(count($pubLocData.keywords.text))}
<section class="item keywords">
{foreach from=$pubLocData.keywords.text key=locale item=keywords}
<section>
<h2 class="label" lang="{$pubLocData.keywords.header[$locale]|replace:"_":"-"}">
{capture assign=translatedKeywords}{translate key="preprint.subject" 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>
</section>
{/foreach}
</section>
{/if}

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

Expand Down

0 comments on commit f6308d1

Please sign in to comment.