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 cbf7490 commit 92297bd
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 29 deletions.
41 changes: 41 additions & 0 deletions pages/article/ArticleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ public function view($args, $request)
$templateMgr->assign('purchaseArticleEnabled', true);
}

$templateMgr->assign('pubLocData', $this->getPublicationLocaleData($publication, $context->getPrimaryLocale(), $article->getData('locale'), $templateMgr->getTemplateVars('activeTheme')->getOption('showMetadata') ?: []));

if (!Hook::call('ArticleHandler::view', [&$request, &$issue, &$article, $publication])) {
$templateMgr->display('frontend/pages/article.tpl');
event(new UsageEvent(Application::ASSOC_TYPE_SUBMISSION, $context, $article, null, null, $this->issue));
Expand Down Expand Up @@ -610,4 +612,43 @@ public function userCanViewGalley($request, $articleId, $galleyId = null)
}
return true;
}

/**
* For article details, format display data.
*/
protected function getPublicationLocaleData(\APP\publication\Publication $publication, string $contextPrimaryLocale, string $submissionLocale, array $metadataOpts): array
{
$titles = $publication->getTitles('html');
$subtitles = $publication->getSubtitles('html');
$primaryLocale = isset($titles[$contextPrimaryLocale]) ? $contextPrimaryLocale : $submissionLocale;
$uiLocale = $contextPrimaryLocale;
$getOtherTitles = fn ($tt) => in_array('titles', $metadataOpts) ? array_filter($tt, fn ($locale) => $locale !== $primaryLocale, ARRAY_FILTER_USE_KEY) : [];
$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 = [
'title' => ['text' => $getOtherTitles($titles)],
'subtitle' => ['text' => $getOtherTitles($subtitles)],
'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['primarySubtitle'] = $subtitles[$primaryLocale] ?? null;
$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 @@ -124,6 +124,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' => __('article.subject'),
],
[
'value' => 'abstract',
'label' => __('article.abstract'),
],
],
'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 @@ -98,3 +98,18 @@ msgstr "Next slide"

msgid "plugins.themes.default.prevSlide"
msgstr "Previous slide"

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

msgid "plugins.themes.default.option.metadata.description"
msgstr "Select the article metadata to show in other languages."

msgid "plugins.themes.default.option.metadata.titleAndSubtitle"
msgstr "Title and subtitle"

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

msgid "plugins.themes.default.titleSubtitleSeparator"
msgstr " &mdash; "
43 changes: 37 additions & 6 deletions plugins/themes/default/styles/objects/article_details.less
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,30 @@
*/
.obj_article_details {

> .page_title {
.page_titles {
margin: 0;

> .subtitle {
font-size: @font-base;
line-height: @line-lead;
font-weight: @normal;
}
}

> .subtitle {
margin: 0;
font-size: @font-base;
line-height: @line-lead;
font-weight: @normal;
.page_locale_titles {
margin: @quadruple 0 @triple 0;

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

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

.row {
Expand Down Expand Up @@ -66,6 +81,22 @@
display: inline;
font-size: @font-base;
}

&.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
69 changes: 46 additions & 23 deletions templates/frontend/objects/article_details.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,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,
*}
{if !$heading}
{assign var="heading" value="h3"}
Expand All @@ -87,14 +88,26 @@
</div>
{/if}

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

{if $publication->getLocalizedData('subtitle')}
<h2 class="subtitle">
{$publication->getLocalizedSubTitle(null, 'html')|strip_unsafe_html}
</h2>
{if !empty(count($pubLocData.title.text))}
<hgroup class="page_locale_titles" aria-label="{translate key="plugins.themes.default.aria.multilingualSubmissionTitles"}">
{foreach from=$pubLocData.title.text key=locale item=title}
<h2 lang="{$locale|replace:"_":"-"}">
{$title|strip_unsafe_html}
{if isset($pubLocData.subtitle.text[$locale])}{translate key="plugins.themes.default.titleSubtitleSeparator"}{$pubLocData.subtitle.text[$locale]|strip_unsafe_html}{/if}
</h2>
{/foreach}
</hgroup>
{/if}

<div class="row">
Expand Down Expand Up @@ -158,25 +171,35 @@


{* Keywords *}
{if !empty($publication->getLocalizedData('keywords'))}
<section class="item keywords">
<h2 class="label">
{capture assign=translatedKeywords}{translate key="article.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.heading[$locale]|replace:"_":"-"}">
{capture assign=translatedKeywords}{translate key="article.subject" 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>
</section>
{/foreach}
</section>
{/if}

{* Abstract *}
{if $publication->getLocalizedData('abstract')}
<section class="item abstract">
<h2 class="label">{translate key="article.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.heading[$locale]|replace:"_":"-"}">
{translate key="article.abstract" locale=$pubLocData.abstract.heading[$locale]}
</h2>
<span lang="{$locale|replace:"_":"-"}">{$abstract|strip_unsafe_html}</span>
</section>
{/foreach}
</section>
{/if}

Expand Down

0 comments on commit 92297bd

Please sign in to comment.