Skip to content
This repository was archived by the owner on Nov 21, 2019. It is now read-only.

Commit 36f98fe

Browse files
Add article type (#55)
* Add article type * Fix name * Add display channel support * Fix merge * Update
1 parent 65fcea2 commit 36f98fe

File tree

14 files changed

+896
-3
lines changed

14 files changed

+896
-3
lines changed

.docker/api/data/scholarly-articles/article1/1.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
</meta>
1010

11-
<jats:article>
11+
<jats:article article-type="research-article">
1212

1313
<jats:front>
1414

@@ -24,9 +24,15 @@
2424
</jats:title-group>
2525

2626
<jats:article-categories>
27+
2728
<jats:subj-group subj-group-type="heading">
2829
<jats:subject>Evolutionary Biology</jats:subject>
2930
</jats:subj-group>
31+
32+
<jats:subj-group subj-group-type="display-channel">
33+
<jats:subject>Research Article</jats:subject>
34+
</jats:subj-group>
35+
3036
</jats:article-categories>
3137

3238
<jats:kwd-group kwd-group-type="author-keywords">
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Libero\JatsContentBundle\EventListener\BuildView;
6+
7+
use FluentDOM\DOM\Element;
8+
use Libero\ViewsBundle\Views\ContextAwareTranslation;
9+
use Libero\ViewsBundle\Views\SimplifiedViewConverterListener;
10+
use Libero\ViewsBundle\Views\View;
11+
use Symfony\Contracts\Translation\TranslatorInterface;
12+
use function sprintf;
13+
14+
final class FrontArticleTypeContentMetaListener
15+
{
16+
use ContextAwareTranslation;
17+
use SimplifiedViewConverterListener;
18+
19+
private $translationKeys;
20+
21+
public function __construct(TranslatorInterface $translator, array $translationKeys = [])
22+
{
23+
$this->translator = $translator;
24+
$this->translationKeys = $translationKeys;
25+
}
26+
27+
protected function handle(Element $object, View $view) : View
28+
{
29+
$items = $view->getArgument('items') ?? [];
30+
31+
if (isset($items['type'])) {
32+
return $view;
33+
}
34+
35+
$article = $object->parentNode;
36+
37+
if (!$article instanceof Element
38+
||
39+
'{http://jats.nlm.nih.gov}article' !== sprintf('{%s}%s', $article->namespaceURI, $article->localName)
40+
) {
41+
return $view;
42+
}
43+
44+
$type = $article->getAttribute('article-type');
45+
46+
if (!isset($this->translationKeys[$type])) {
47+
return $view;
48+
}
49+
50+
$items['type'] = [
51+
'attributes' => [
52+
'aria-label' => $this->translate('libero.patterns.content_header.meta.type.label', $view->getContext()),
53+
],
54+
'content' => [
55+
'text' => $this->translate($this->translationKeys[$type], $view->getContext()),
56+
],
57+
];
58+
59+
return $view->withArgument('items', $items);
60+
}
61+
62+
protected function canHandleTemplate(?string $template) : bool
63+
{
64+
return '@LiberoPatterns/content-meta.html.twig' === $template;
65+
}
66+
67+
protected function canHandleElement(string $element) : bool
68+
{
69+
return '{http://jats.nlm.nih.gov}front' === $element;
70+
}
71+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Libero\JatsContentBundle\EventListener\BuildView;
6+
7+
use FluentDOM\DOM\Element;
8+
use Libero\ViewsBundle\Views\ContextAwareTranslation;
9+
use Libero\ViewsBundle\Views\SimplifiedViewConverterListener;
10+
use Libero\ViewsBundle\Views\View;
11+
use Libero\ViewsBundle\Views\ViewConverter;
12+
use Symfony\Contracts\Translation\TranslatorInterface;
13+
use function Libero\ViewsBundle\array_has_key;
14+
15+
final class FrontContentHeaderMetaListener
16+
{
17+
use ContextAwareTranslation;
18+
use SimplifiedViewConverterListener;
19+
20+
private $converter;
21+
22+
public function __construct(ViewConverter $converter, TranslatorInterface $translator)
23+
{
24+
$this->converter = $converter;
25+
$this->translator = $translator;
26+
}
27+
28+
protected function handle(Element $object, View $view) : View
29+
{
30+
$meta = $this->converter
31+
->convert($object, '@LiberoPatterns/content-meta.html.twig', $view->getContext())
32+
->getArguments();
33+
34+
if (empty($meta)) {
35+
return $view;
36+
}
37+
38+
if (!isset($meta['attributes']['aria-label'])) {
39+
$meta['attributes']['aria-label'] = $this->translate(
40+
'libero.patterns.content_header.meta.label',
41+
$view->getContext()
42+
);
43+
}
44+
45+
return $view->withArgument('meta', $meta);
46+
}
47+
48+
protected function canHandleTemplate(?string $template) : bool
49+
{
50+
return '@LiberoPatterns/content-header.html.twig' === $template;
51+
}
52+
53+
protected function canHandleElement(string $element) : bool
54+
{
55+
return '{http://jats.nlm.nih.gov}front' === $element;
56+
}
57+
58+
protected function canHandleArguments(array $arguments) : bool
59+
{
60+
return !array_has_key($arguments, 'meta');
61+
}
62+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Libero\JatsContentBundle\EventListener\BuildView;
6+
7+
use FluentDOM\DOM\Element;
8+
use Libero\ViewsBundle\Views\ContextAwareTranslation;
9+
use Libero\ViewsBundle\Views\SimplifiedViewConverterListener;
10+
use Libero\ViewsBundle\Views\View;
11+
use Libero\ViewsBundle\Views\ViewConverter;
12+
use Symfony\Contracts\Translation\TranslatorInterface;
13+
14+
final class FrontDisplayChannelContentMetaListener
15+
{
16+
use ContextAwareTranslation;
17+
use SimplifiedViewConverterListener;
18+
19+
private const CATEGORIES_PATH = 'jats:article-meta/jats:article-categories';
20+
private const GROUP_PATH = self::CATEGORIES_PATH.'/jats:subj-group[@subj-group-type="display-channel"]';
21+
private const SUBJECT_PATH = self::GROUP_PATH.'/jats:subject';
22+
23+
private $converter;
24+
25+
public function __construct(ViewConverter $converter, TranslatorInterface $translator)
26+
{
27+
$this->converter = $converter;
28+
$this->translator = $translator;
29+
}
30+
31+
protected function handle(Element $object, View $view) : View
32+
{
33+
$items = $view->getArgument('items') ?? [];
34+
35+
if (isset($items['type'])) {
36+
return $view;
37+
}
38+
39+
$displayChannel = $object->ownerDocument->xpath()->firstOf(self::SUBJECT_PATH, $object);
40+
41+
if (!$displayChannel instanceof Element) {
42+
return $view;
43+
}
44+
45+
$items['type'] = [
46+
'attributes' => [
47+
'aria-label' => $this->translate('libero.patterns.content_header.meta.type.label', $view->getContext()),
48+
],
49+
'content' => $this->converter->convert(
50+
$displayChannel,
51+
'@LiberoPatterns/link.html.twig',
52+
$view->getContext()
53+
)->getArguments(),
54+
];
55+
56+
return $view->withArgument('items', $items);
57+
}
58+
59+
protected function canHandleTemplate(?string $template) : bool
60+
{
61+
return '@LiberoPatterns/content-meta.html.twig' === $template;
62+
}
63+
64+
protected function canHandleElement(string $element) : bool
65+
{
66+
return '{http://jats.nlm.nih.gov}front' === $element;
67+
}
68+
}

vendor-extra/JatsContentBundle/src/Resources/config/services.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,30 @@
7575
<tag name="kernel.event_listener" event="libero.view.build" method="onBuildView"/>
7676
</service>
7777

78+
<service id="Libero\JatsContentBundle\EventListener\BuildView\FrontContentHeaderMetaListener"
79+
class="Libero\JatsContentBundle\EventListener\BuildView\FrontContentHeaderMetaListener">
80+
<argument type="service" id="Libero\ViewsBundle\Views\ViewConverter"/>
81+
<argument type="service" id="translator"/>
82+
<tag name="kernel.event_listener" event="libero.view.build" method="onBuildView"/>
83+
</service>
84+
85+
<service id="Libero\JatsContentBundle\EventListener\BuildView\FrontArticleTypeContentMetaListener"
86+
class="Libero\JatsContentBundle\EventListener\BuildView\FrontArticleTypeContentMetaListener">
87+
<argument type="service" id="translator"/>
88+
<argument type="collection">
89+
<!-- Map of @article-type values to translation keys -->
90+
<argument key="research-article">libero.jats.article_type.research_article</argument>
91+
</argument>
92+
<tag name="kernel.event_listener" event="libero.view.build" method="onBuildView" priority="-100"/>
93+
</service>
94+
95+
<service id="Libero\JatsContentBundle\EventListener\BuildView\FrontDisplayChannelContentMetaListener"
96+
class="Libero\JatsContentBundle\EventListener\BuildView\FrontDisplayChannelContentMetaListener">
97+
<argument type="service" id="Libero\ViewsBundle\Views\ViewConverter"/>
98+
<argument type="service" id="translator"/>
99+
<tag name="kernel.event_listener" event="libero.view.build" method="onBuildView"/>
100+
</service>
101+
78102
<service id="Libero\JatsContentBundle\EventListener\BuildView\FrontSubjectGroupContentHeaderListener"
79103
class="Libero\JatsContentBundle\EventListener\BuildView\FrontSubjectGroupContentHeaderListener">
80104
<argument type="service" id="Libero\ViewsBundle\Views\ViewConverter"/>

vendor-extra/JatsContentBundle/src/Resources/translations/messages+intl-icu.en.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
<source>Research organism</source>
1717
</trans-unit>
1818

19+
<trans-unit id="3" resname="libero.jats.article_type.research_article">
20+
<source>Research article</source>
21+
</trans-unit>
22+
1923
</body>
2024

2125
</file>

0 commit comments

Comments
 (0)