Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow array values for meta tags #600

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/Seo/SeoPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
*/
class SeoPage implements SeoPageInterface
{
/**
* @var string[]
*/
private const ALLOW_MULTIPLE_TAGS = [
'og:image',
];
/**
* @var string
*/
Expand Down Expand Up @@ -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;
}
Expand Down
52 changes: 41 additions & 11 deletions src/Twig/Extension/SeoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,27 @@ public function getMetadatas()
[$content, $extras] = $meta;

if (!empty($content)) {
$html .= sprintf(
"<meta %s=\"%s\" content=\"%s\" />\n",
$type,
$this->normalize($name),
$this->normalize($content)
);
if (\is_array($content)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method has a large code complexity and is hard to understand. Can you try to extra a few methods.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can pass an $extra variable to generateMetaTag() method as a new property, then the foreach would not be duplicated. The structured properties (additional tags) would be generated from the inside as a recursive function.
Or should I simply introduce a new generateMetaTagExtra() method and call it?

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(
"<meta %s=\"%s\" />\n",
$type,
$this->normalize($name)
);
$html .= $this->generateMetaTag($type, $name);
}
}
}
Expand Down Expand Up @@ -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(
"<meta %s=\"%s\" />\n",
$type,
$this->normalize($name)
);
}

return sprintf(
"<meta %s=\"%s\" content=\"%s\" />\n",
$type,
$this->normalize($name),
$this->normalize($content)
);
}

/**
* @param string $string
*
Expand Down
23 changes: 23 additions & 0 deletions tests/Seo/SeoPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
26 changes: 24 additions & 2 deletions tests/Twig/Extension/SeoExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,37 @@ 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(), []],
],
]);

$extension = new SeoExtension($page, 'UTF-8');

static::assertSame(
"<meta name=\"foo\" content=\"bar &quot;'&quot;\" />\n<meta charset=\"UTF-8\" />\n<meta property=\"og:image:width\" content=\"848\" />\n<meta property=\"og:type\" content=\"article\" />\n",
"<meta name=\"foo\" content=\"bar &quot;'&quot;\" />\n<meta charset=\"UTF-8\" />\n<meta property=\"og:image\" content=\"image1\" />\n<meta property=\"og:image:width\" content=\"848\" />\n<meta property=\"og:image:height\" content=\"646\" />\n<meta property=\"og:type\" content=\"article\" />\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(
"<meta property=\"og:image\" content=\"image1\" />\n<meta property=\"og:image:width\" content=\"640\" />\n<meta property=\"og:image\" content=\"image2\" />\n<meta property=\"og:image:width\" content=\"640\" />\n<meta property=\"og:image:height\" content=\"480\" />\n",
$extension->getMetadatas()
);
}
Expand Down