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 16, 2023
1 parent 715d3e7 commit 31520dd
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 24 deletions.
3 changes: 3 additions & 0 deletions locale/en/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ msgstr "Submission ID"
msgid "article.title"
msgstr "Title"

msgid "article.titlesLanguages"
msgstr "Article titles in other languages"

msgid "article.authors"
msgstr "Authors"

Expand Down
42 changes: 40 additions & 2 deletions pages/article/ArticleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ public function initialize($request, $args = [])
$this->galley = $galley;
break;

// In some cases, a URL to a galley may use the ID when it should use
// the urlPath. Redirect to the galley's correct URL.
// In some cases, a URL to a galley may use the ID when it should use
// the urlPath. Redirect to the galley's correct URL.
} elseif (ctype_digit($galleyId) && $galley->getId() == $galleyId) {
$request->redirect(null, $request->getRequestedPage(), $request->getRequestedOp(), [$submission->getBestId(), $galley->getBestGalleyId()]);
}
Expand Down Expand Up @@ -357,6 +357,8 @@ public function view($args, $request)
$templateMgr->assign('purchaseArticleEnabled', true);
}

$templateMgr->assign('pubLocaleData', $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 @@ -616,4 +618,40 @@ 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 (array $tt): array => in_array('titles', $metadataOpts) ? array_filter($tt, fn (string $locale) => $locale !== $primaryLocale, ARRAY_FILTER_USE_KEY) : [];
$getMdata = fn (string $opt): array => 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]]);

$pubLocaleData = [
'title' => ['text' => $getOtherTitles($titles)],
'subtitle' => ['text' => $getOtherTitles($subtitles)],
'keywords' => ['text' => $getMdata('keywords')],
'abstract' => ['text' => $getMdata('abstract')],
];

foreach($pubLocaleData as $opt => &$item) {
uksort($item['text'], fn (string $a, string $b) => $a === $primaryLocale ? -1 : ($b === $primaryLocale ? 1 : $a <=> $b));

$item['headingLang'] = collect($item['text'])
->map(fn ($_, string $locale) => in_array($opt, $metadataOpts) ? $locale : $uiLocale)
->toArray();
}

$pubLocaleData['primaryTitle'] = $titles[$primaryLocale];
$pubLocaleData['primarySubtitle'] = $subtitles[$primaryLocale] ?? null;
$pubLocaleData['primaryLocale'] = $primaryLocale;

return $pubLocaleData;
}
}
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
12 changes: 12 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,15 @@ 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.titleSubtitleSeparator"
msgstr " &mdash; "
17 changes: 17 additions & 0 deletions plugins/themes/default/styles/objects/article_details.less
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@
display: inline;
font-size: @font-base;
}

&.keywords > section {
margin-bottom: @base;

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

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

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

.sub_item {
Expand Down
68 changes: 46 additions & 22 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 $pubLocaleData array Array of formatted publication locale metadata: titles, abstracts, keywords,
*
* @hook Templates::Article::Main []
* @hook Templates::Article::Details::Reference []
Expand Down Expand Up @@ -91,13 +92,12 @@
</div>
{/if}

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

{if $publication->getLocalizedData('subtitle')}
<h2 class="subtitle">
{$publication->getLocalizedSubTitle(null, 'html')|strip_unsafe_html}
{if $pubLocaleData.primarySubtitle}
<h2 class="subtitle" lang="{$pubLocaleData.primaryLocale|replace:"_":"-"}">
{$pubLocaleData.primarySubtitle|strip_unsafe_html}
</h2>
{/if}

Expand Down Expand Up @@ -160,27 +160,51 @@
</section>
{/if}

{* Titles in other languages *}
{if !empty(count($pubLocaleData.title.text))}
<section class="item page_locale_titles">
<h2 class="label">
{translate key="article.titlesLanguages" locale=$pubLocaleData.primaryLocale}
</h2>
{foreach from=$pubLocaleData.title.text key=locale item=title}
<p lang="{$locale|replace:"_":"-"}">
{$title|strip_tags}
{if isset($pubLocaleData.subtitle.text[$locale])}{translate key="plugins.themes.default.titleSubtitleSeparator"}{$pubLocaleData.subtitle.text[$locale]|strip_tags}{/if}
</p>
{/foreach}
</section>
{/if}

{* 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($pubLocaleData.keywords.text))}
<section class="item keywords">
{foreach from=$pubLocaleData.keywords.text key=locale item=keywords}
<section>
<h2 class="label" lang="{$pubLocaleData.keywords.headingLang[$locale]|replace:"_":"-"}">
{capture assign=translatedKeywords}{translate key="article.subject" locale=$pubLocaleData.keywords.headingLang[$locale]}{/capture}
{translate key="semicolon" label=$translatedKeywords locale=$pubLocaleData.keywords.headingLang[$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=$pubLocaleData.keywords.headingLang[$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($pubLocaleData.abstract.text))}
<section class="item abstracts">
{foreach from=$pubLocaleData.abstract.text key=locale item=abstract}
<section class="abstract">
<h2 class="label" lang="{$pubLocaleData.abstract.headingLang[$locale]|replace:"_":"-"}">
{translate key="article.abstract" locale=$pubLocaleData.abstract.headingLang[$locale]}
</h2>
<p lang="{$locale|replace:"_":"-"}">{$abstract|strip_tags}</p>
</section>
{/foreach}
</section>
{/if}

Expand Down

0 comments on commit 31520dd

Please sign in to comment.