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 27, 2023
1 parent 0027b66 commit a7ddb59
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 35 deletions.
48 changes: 46 additions & 2 deletions pages/article/ArticleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use APP\observers\events\UsageEvent;
use APP\payment\ojs\OJSCompletedPaymentDAO;
use APP\payment\ojs\OJSPaymentManager;
use APP\publication\Publication;
use APP\security\authorization\OjsJournalMustPublishPolicy;
use APP\submission\Submission;
use APP\template\TemplateManager;
Expand All @@ -38,6 +39,7 @@
use PKP\plugins\PluginRegistry;
use PKP\security\authorization\ContextRequiredPolicy;
use PKP\security\Validation;
use PKP\services\PKPSchemaService;
use PKP\submission\Genre;
use PKP\submission\GenreDAO;
use PKP\submission\PKPSubmission;
Expand Down Expand Up @@ -167,8 +169,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 +359,13 @@ public function view($args, $request)
$templateMgr->assign('purchaseArticleEnabled', true);
}

$templateMgr->assign('pubLocaleData', $this->getPublicationLocaleData(
$publication,
$context->getPrimaryLocale(),
$templateMgr->getTemplateVars('activeTheme')->getOption('showMultilingualMetadata') ?: [],
$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 +625,39 @@ public function userCanViewGalley($request, $articleId, $galleyId = null)
}
return true;
}

/**
* Format multilingual publication data for template:
* Default data at least includes in one language: full title, title, subtitle, keywords, abstract
* showMultilingualMetadataOpts adds multilingual metadata: title (by default includes fullTitle and subtitle), keywords, abstract, etc.
* showMetadataOpts: additional metadata
*/
protected function getPublicationLocaleData(Publication $publication, string $contextPrimaryLocale, array $showMultilingualMetadataOpts, array $showMetadataOpts): array
{
$titles = collect([
'title' => $publication->getTitles('html'),
'subtitle' => $publication->getSubtitles('html'),
'fullTitle' => $publication->getFullTitles('html'),
]);
$metadataOpts = collect($showMetadataOpts)->filter(fn ($opt) => in_array($opt, Services::get('schema')->getMultilingualProps(PKPSchemaService::SCHEMA_PUBLICATION)))
->concat(['keywords', 'abstract'])->unique()->diff($titles->keys())->values();
$multilingualOpts = collect($showMultilingualMetadataOpts)
->when(in_array('title', $showMultilingualMetadataOpts), fn ($m) => $m->concat(['subtitle', 'fullTitle'])->unique()->values());
$primaryLocale = isset($titles->get('title')[$contextPrimaryLocale]) ? $contextPrimaryLocale : $publication->getData('locale');

$getText = fn (array $item, string $opt): array => [
$opt => [
'text' => ($text = array_filter($item, fn (string $locale) => $multilingualOpts->contains($opt) || $locale === $primaryLocale, ARRAY_FILTER_USE_KEY)),
'headingLang' => collect($text)->map(fn ($_, string $locale): string => $locale === $primaryLocale ? $contextPrimaryLocale : $locale)->toArray()
],
];

$pubLocaleData = $titles->mapWithKeys($getText)
->union($metadataOpts->mapWithKeys(fn (string $opt): array => $getText($publication->getData($opt) ?? [], $opt)));
return $pubLocaleData
->put('languages', $pubLocaleData->map(fn (array $item): array => array_keys($item['text']))
->flatten()->sort()->prepend($primaryLocale)->unique()->values())
->put('primaryLocale', $primaryLocale)
->toArray();
}
}
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('showMultilingualMetadata', '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' => __('common.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.submissionMetadataInLanguage"
msgstr "Article Metadata in English"

msgid "plugins.themes.default.titleSubtitleSeparator"
msgstr " — "
15 changes: 8 additions & 7 deletions plugins/themes/default/styles/objects/article_details.less
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
margin-bottom: 0;
}

> h2 + p {
> h2 + p, h3 + p {
margin-top: 0;
}
}
Expand All @@ -52,6 +52,10 @@

.main_entry {

.metadata > .page_metadata_title {
margin: 0;
}

.item {

.label {
Expand All @@ -61,8 +65,7 @@
font-weight: @bold;
}

&.doi .label,
&.keywords .label {
&.doi .label {
display: inline;
font-size: @font-base;
}
Expand Down Expand Up @@ -127,8 +130,7 @@
}
}

.item.doi,
.item.keywords {
.item.doi {
padding-top: 0;
}

Expand Down Expand Up @@ -315,8 +317,7 @@
font-weight: @bold;
}

&.doi .label,
&.keywords .label {
&.doi .label {
display: inline;
font-size: @font-base;
}
Expand Down
83 changes: 57 additions & 26 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.title.text[$pubLocaleData.primaryLocale]|strip_unsafe_html}
</h1>

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

Expand Down Expand Up @@ -160,29 +160,60 @@
</section>
{/if}

{*
* Show article keywords and abstract in ui, or submission, language by default.
* Show optional multilingual metadata: titles, keywords, abstracts.
*}
{foreach from=$pubLocaleData.languages item=lang}
<section class="metadata">
{assign "hLvl" "2"}
{* Multilingual metadata title *}
{if $lang !== $pubLocaleData.primaryLocale}
{assign "hLvl" "3"}
<h2 class="item label page_metadata_title" lang={$lang|replace:"_":"-"}>
{translate key="plugins.themes.default.submissionMetadataInLanguage" locale=$lang}
</h2>
{* Title in other language *}
{if isset($pubLocaleData.title.text[$lang])}
<section class="item page_locale_title">
<h{$hLvl} class="label" lang="{$pubLocaleData.title.headingLang[$lang]|replace:"_":"-"}">
{translate key="submission.title" locale=$pubLocaleData.title.headingLang[$lang]}
</h{$hLvl}>
<p lang="{$lang|replace:"_":"-"}">
{$pubLocaleData.title.text[$lang]|strip_tags}
{if isset($pubLocaleData.subtitle.text[$lang])}
{translate key="plugins.themes.default.titleSubtitleSeparator" locale=$pubLocaleData.title.headingLang[$lang]}{$pubLocaleData.subtitle.text[$lang]|strip_tags}
{/if}
</p>
</section>
{/if}
{/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}
{* Keywords *}
{if isset($pubLocaleData.keywords.text[$lang])}
<section class="item keywords">
<h{$hLvl} class="label" lang="{$pubLocaleData.keywords.headingLang[$lang]|replace:"_":"-"}">
{translate key="common.keywords" locale=$pubLocaleData.keywords.headingLang[$lang]}
</h{$hLvl}>
<p class="value" lang="{$lang|replace:"_":"-"}">
{foreach from=$pubLocaleData.keywords.text[$lang] item="keyword"}
{$keyword|escape}{if !$keyword@last}{translate key="common.commaListSeparator" locale=$pubLocaleData.keywords.headingLang[$lang]}{/if}
{/foreach}
</p>
</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}
{* Abstract *}
{if isset($pubLocaleData.abstract.text[$lang])}
<section class="item abstract">
<h{$hLvl} class="label" lang="{$pubLocaleData.abstract.headingLang[$lang]|replace:"_":"-"}">
{translate key="common.abstract" locale=$pubLocaleData.abstract.headingLang[$lang]}
</h{$hLvl}>
<p lang="{$lang|replace:"_":"-"}">{$pubLocaleData.abstract.text[$lang]|strip_tags}</p>
</section>
{/if}
</section>
{/if}
{/foreach}

{call_hook name="Templates::Article::Main"}

Expand Down

0 comments on commit a7ddb59

Please sign in to comment.