diff --git a/src/Seo/SeoPage.php b/src/Seo/SeoPage.php
index b04527ab..212fc899 100644
--- a/src/Seo/SeoPage.php
+++ b/src/Seo/SeoPage.php
@@ -20,6 +20,12 @@
*/
class SeoPage implements SeoPageInterface
{
+ /**
+ * @var string[]
+ */
+ private const ALLOW_MULTIPLE_TAGS = [
+ 'og:image',
+ ];
/**
* @var string
*/
@@ -157,7 +163,20 @@ public function addMeta($type, $name, /* string */ $value, array $extras = [])
$this->metas[$type] = [];
}
- $this->metas[$type][$name] = [$value, $extras];
+ $arrayValue = [$value, $extras];
+
+ if (\in_array($name, self::ALLOW_MULTIPLE_TAGS, true)) {
+ if (isset($this->metas[$type][$name])) {
+ [$oldValue, $oldExtras] = $this->metas[$type][$name];
+ if (!\is_array($oldValue)) {
+ $oldValue = [$oldValue];
+ $oldExtras = [$oldExtras];
+ }
+ $arrayValue = [array_merge($oldValue, [$value]), array_merge($oldExtras, [$extras])];
+ }
+ }
+
+ $this->metas[$type][$name] = $arrayValue;
return $this;
}
diff --git a/src/Twig/Extension/SeoExtension.php b/src/Twig/Extension/SeoExtension.php
index b150f880..b36755ba 100644
--- a/src/Twig/Extension/SeoExtension.php
+++ b/src/Twig/Extension/SeoExtension.php
@@ -121,18 +121,27 @@ public function getMetadatas()
[$content, $extras] = $meta;
if (!empty($content)) {
- $html .= sprintf(
- "\n",
- $type,
- $this->normalize($name),
- $this->normalize($content)
- );
+ if (\is_array($content)) {
+ foreach ($content as $contentKey => $contentItem) {
+ $html .= $this->generateMetaTag($type, $name, $contentItem);
+ if (isset($extras[$contentKey]) && \is_array($extras[$contentKey])) {
+ foreach ($extras[$contentKey] as $extraKey => $extra) {
+ if (':' === substr($extraKey, 0, 1)) {
+ $html .= $this->generateMetaTag($type, $name.$extraKey, $extra);
+ }
+ }
+ }
+ }
+ } else {
+ $html .= $this->generateMetaTag($type, $name, $content);
+ foreach ($extras as $extraKey => $extra) {
+ if (':' === substr($extraKey, 0, 1)) {
+ $html .= $this->generateMetaTag($type, $name.$extraKey, $extra);
+ }
+ }
+ }
} else {
- $html .= sprintf(
- "\n",
- $type,
- $this->normalize($name)
- );
+ $html .= $this->generateMetaTag($type, $name);
}
}
}
@@ -276,6 +285,27 @@ public function renderBreadcrumb(Environment $environment, ?string $currentUri =
]);
}
+ /**
+ * @return string
+ */
+ private function generateMetaTag($type, $name, $content = null)
+ {
+ if (null === $content) {
+ return sprintf(
+ "\n",
+ $type,
+ $this->normalize($name)
+ );
+ }
+
+ return sprintf(
+ "\n",
+ $type,
+ $this->normalize($name),
+ $this->normalize($content)
+ );
+ }
+
/**
* @param string $string
*
diff --git a/tests/Seo/SeoPageTest.php b/tests/Seo/SeoPageTest.php
index a1b93cf8..b2bd5ed8 100644
--- a/tests/Seo/SeoPageTest.php
+++ b/tests/Seo/SeoPageTest.php
@@ -34,6 +34,29 @@ public function testAddMeta()
static::assertSame($expected, $page->getMetas());
}
+ public function testAddArrayMeta()
+ {
+ $page = new SeoPage();
+
+ //Allow multiple tags
+ $page->addMeta('property', 'og:image', 'http://example1.com/');
+ $page->addMeta('property', 'og:image', 'http://example2.com/');
+
+ //Override tag
+ $page->addMeta('property', 'foo', 'foo', ['foo' => 'foo']);
+ $page->addMeta('property', 'foo', 'bar', ['foo' => 'bar']);
+
+ $expected = [
+ 'http-equiv' => [],
+ 'name' => [],
+ 'schema' => [],
+ 'charset' => [],
+ 'property' => ['og:image' => [['http://example1.com/', 'http://example2.com/'], [[], []]], 'foo' => ['bar', ['foo' => 'bar']]],
+ ];
+
+ static::assertSame($expected, $page->getMetas());
+ }
+
public function testOverrideMetas()
{
$page = new SeoPage();
diff --git a/tests/Twig/Extension/SeoExtensionTest.php b/tests/Twig/Extension/SeoExtensionTest.php
index 2f8471e2..02f64967 100644
--- a/tests/Twig/Extension/SeoExtensionTest.php
+++ b/tests/Twig/Extension/SeoExtensionTest.php
@@ -96,7 +96,8 @@ public function testMetadatas()
'schema' => [],
'charset' => ['UTF-8' => ['', []]],
'property' => [
- 'og:image:width' => [848, []],
+ 'og:image' => ['image1', [':width' => 848]],
+ 'og:image:height' => [646, []],
'og:type' => [new MetaTest(), []],
],
]);
@@ -104,7 +105,28 @@ public function testMetadatas()
$extension = new SeoExtension($page, 'UTF-8');
static::assertSame(
- "\n\n\n\n",
+ "\n\n\n\n\n\n",
+ $extension->getMetadatas()
+ );
+ }
+
+ public function testArrayMetadatas()
+ {
+ $page = $this->createMock(SeoPageInterface::class);
+ $page->expects(static::once())->method('getMetas')->willReturn([
+ 'http-equiv' => [],
+ 'name' => [],
+ 'schema' => [],
+ 'charset' => [],
+ 'property' => [
+ 'og:image' => [['image1', 'image2'], [['foo' => 'bar', ':width' => '640'], [':width' => '640', ':height' => '480']]],
+ ],
+ ]);
+
+ $extension = new SeoExtension($page, 'UTF-8');
+
+ static::assertSame(
+ "\n\n\n\n\n",
$extension->getMetadatas()
);
}