Skip to content

Commit

Permalink
feat(Meta): add meta tags and open-graph properties to blog articles
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne-monsieurbiz committed Aug 20, 2024
1 parent cd19041 commit 36700ed
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ COMPOSER=symfony composer
CONSOLE=${SYMFONY} console
export COMPOSE_PROJECT_NAME=sylius_blog_plugin
PLUGIN_NAME=sylius-blog-plugin
COMPOSE=docker-compose
COMPOSE=docker compose
YARN=yarn
DOCTRINE_MIGRATIONS_NAMESPACE=MonsieurBiz\SyliusBlogPlugin\Migrations

Expand Down
30 changes: 30 additions & 0 deletions src/Entity/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,36 @@ public function getAuthors(): Collection
return $this->authors;
}

public function getMetaTitle(): ?string
{
return $this->getTranslation()->getMetaTitle();
}

public function setMetaTitle(?string $metaTitle): void
{
$this->getTranslation()->setMetaTitle($metaTitle);
}

public function getMetaDescription(): ?string
{
return $this->getTranslation()->getMetaDescription();
}

public function setMetaDescription(?string $metaDescription): void
{
$this->getTranslation()->setMetaDescription($metaDescription);
}

public function getMetaKeywords(): ?string
{
return $this->getTranslation()->getMetaKeywords();
}

public function setMetaKeywords(?string $metaKeywords): void
{
$this->getTranslation()->setMetaKeywords($metaKeywords);
}

public function publish(): void
{
$this->publishedAt = new DateTime();
Expand Down
12 changes: 12 additions & 0 deletions src/Entity/ArticleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,16 @@ public function hasAuthor(AuthorInterface $author): bool;
public function getAuthors(): Collection;

public function publish(): void;

public function getMetaTitle(): ?string;

public function setMetaTitle(?string $metaTitle): void;

public function getMetaDescription(): ?string;

public function setMetaDescription(?string $metaDescription): void;

public function getMetaKeywords(): ?string;

public function setMetaKeywords(?string $metaKeywords): void;
}
36 changes: 36 additions & 0 deletions src/Entity/ArticleTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class ArticleTranslation extends AbstractTranslation implements ArticleTranslati

protected ?string $content;

protected ?string $metaTitle = null;

protected ?string $metaDescription = null;

protected ?string $metaKeywords = null;

public function getId(): ?int
{
return $this->id;
Expand Down Expand Up @@ -72,4 +78,34 @@ public function setContent(?string $content): void
{
$this->content = $content;
}

public function getMetaTitle(): ?string
{
return $this->metaTitle;
}

public function setMetaTitle(?string $metaTitle): void
{
$this->metaTitle = $metaTitle;
}

public function getMetaDescription(): ?string
{
return $this->metaDescription;
}

public function setMetaDescription(?string $metaDescription): void
{
$this->metaDescription = $metaDescription;
}

public function getMetaKeywords(): ?string
{
return $this->metaKeywords;
}

public function setMetaKeywords(?string $metaKeywords): void
{
$this->metaKeywords = $metaKeywords;
}
}
12 changes: 12 additions & 0 deletions src/Entity/ArticleTranslationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,16 @@ public function setDescription(?string $description): void;
public function getContent(): ?string;

public function setContent(string $content): void;

public function getMetaTitle(): ?string;

public function setMetaTitle(?string $metaTitle): void;

public function getMetaDescription(): ?string;

public function setMetaDescription(?string $metaDescription): void;

public function getMetaKeywords(): ?string;

public function setMetaKeywords(?string $metaKeywords): void;
}
11 changes: 11 additions & 0 deletions src/Form/Type/ArticleTranslationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
->add('content', RichEditorType::class, [
'label' => 'monsieurbiz_blog.form.article.content',
])
->add('metaTitle', TextType::class, [
'label' => 'monsieurbiz_blog.form.article.meta_title',
'help' => 'monsieurbiz_blog.form.article.help.meta_title',
])
->add('metaDescription', TextType::class, [
'label' => 'monsieurbiz_blog.form.article.meta_description',
'help' => 'monsieurbiz_blog.form.article.help.meta_description',
])
->add('metaKeywords', TextType::class, [
'label' => 'monsieurbiz_blog.form.article.meta_keywords',
])
;
}
}
38 changes: 38 additions & 0 deletions src/Migrations/Version20240820090632.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of Monsieur Biz's Blog plugin for Sylius.
* (c) Monsieur Biz <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusBlogPlugin\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240820090632 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE monsieurbiz_blog_article_translation ADD metaTitle VARCHAR(255) DEFAULT NULL, ADD metaDescription LONGTEXT DEFAULT NULL, ADD metaKeywords VARCHAR(255) DEFAULT NULL');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE monsieurbiz_blog_article_translation DROP metaTitle, DROP metaDescription, DROP metaKeywords');
}
}
4 changes: 4 additions & 0 deletions src/Resources/config/doctrine/ArticleTranslation.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<field name="description" type="text" />
<field name="content" type="text"/>

<field name="metaTitle" nullable="true"/>
<field name="metaDescription" type="text" nullable="true"/>
<field name="metaKeywords" nullable="true"/>

<field name="createdAt" column="created_at" type="datetime_immutable">
<gedmo:timestampable on="create"/>
</field>
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ liip_imagine:
monsieurbiz_blog_image_thumbnail:
filters:
thumbnail: { size: [260, 260], mode: outbound }
monsieurbiz_blog_image_og_image: ~
6 changes: 6 additions & 0 deletions src/Resources/translations/messages.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ monsieurbiz_blog:
image: Image
description: Description
authors: Authors
meta_title: Meta title
meta_description: Meta description
meta_keywords: Meta keywords
help:
meta_title: If empty, the title will be used.
meta_description: If empty, the description will be used.
author:
name: Name
ui:
Expand Down
6 changes: 6 additions & 0 deletions src/Resources/translations/messages.fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ monsieurbiz_blog:
image: Image
description: Description
authors: Auteurs
meta_title: Meta title
meta_description: Meta description
meta_keywords: Meta keywords
help:
meta_title: Si vide, le titre sera utilisé.
meta_description: Si vide, la description sera utilisée.
author:
name: Nom
ui:
Expand Down
3 changes: 3 additions & 0 deletions src/Resources/views/Shop/Article/Metatags/_image.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% if article.image is not empty %}
<meta property="og:image" content="{{ article.image|imagine_filter('monsieurbiz_blog_image_og_image') }}" />
{% endif %}
13 changes: 13 additions & 0 deletions src/Resources/views/Shop/Article/_metadata.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% set metaTitle = article.metaTitle|default(article.title) %}
{% set metaDescription = article.metaDescription|default(article.title) %}

<meta name="title" property="og:title" content="{{ metaTitle }}" />
<meta name="description" property="og:description" content="{{ metaDescription }}" />
<meta property="og:type" content="article" />
<meta property="og:url" content="{{ absolute_url(path('monsieurbiz_blog_article_show', {'slug': article.slug})) }}" />

{% include '@MonsieurBizSyliusBlogPlugin/Shop/Article/Metatags/_image.html.twig' %}

{% if article.metaKeywords|default('') is not empty %}
<meta name="keywords" content="{{ article.metaKeywords }}"/>
{% endif %}
1 change: 1 addition & 0 deletions src/Resources/views/Shop/Article/show.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{% endblock %}

{% block metatags %}
{% include '@MonsieurBizSyliusBlogPlugin/Shop/Article/_metadata.html.twig' %}
{% include '@MonsieurBizSyliusBlogPlugin/Shop/Article/_structured_data.html.twig' %}
{% endblock %}

Expand Down

0 comments on commit 36700ed

Please sign in to comment.