Skip to content

Commit ee97099

Browse files
committed
Update post slug to Autologin using Cookie post and and posibility to update/add post_slug using fixtures
1 parent 367d367 commit ee97099

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

‎src/App/src/Fixture/PostLoader.php‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,20 @@ public function load(ObjectManager $manager): void
5555
}
5656

5757
/** @var Author $author */
58-
$author = $this->getReference('author_' . $authorSlug, Author::class);
59-
$title = html_entity_decode($articleData['post_title'], ENT_QUOTES, 'UTF-8');
60-
$slug = $this->slugify($title);
58+
$author = $this->getReference('author_' . $authorSlug, Author::class);
59+
$title = html_entity_decode($articleData['post_title'], ENT_QUOTES, 'UTF-8');
60+
$autoSlug = $this->slugify($title);
6161

62-
if (isset($usedSlugs[$slug])) {
63-
$usedSlugs[$slug]++;
64-
$slug .= '-' . $usedSlugs[$slug];
62+
if (isset($usedSlugs[$autoSlug])) {
63+
$usedSlugs[$autoSlug]++;
64+
$autoSlug .= '-' . $usedSlugs[$autoSlug];
6565
} else {
66-
$usedSlugs[$slug] = 1;
66+
$usedSlugs[$autoSlug] = 1;
6767
}
6868

69+
$customSlug = trim((string) ($articleData['post_slug'] ?? ''));
70+
$slug = $customSlug !== '' ? $customSlug : $autoSlug;
71+
6972
$status = match ($articleData['post_status']) {
7073
'published' => PostStatusEnum::Published,
7174
'private' => PostStatusEnum::Private,
@@ -85,6 +88,10 @@ public function load(ObjectManager $manager): void
8588
$openGraphImg = $articleData['opengraph_img'] ?? null;
8689

8790
$article = $repository->findOneBy(['slug' => $slug]);
91+
if ($article === null && $slug !== $autoSlug) {
92+
// post was imported before post_slug was set: find it by the title-based slug
93+
$article = $repository->findOneBy(['slug' => $autoSlug]);
94+
}
8895

8996
if ($article === null) {
9097
$article = new Post();
@@ -105,6 +112,10 @@ public function load(ObjectManager $manager): void
105112
} else {
106113
$changed = false;
107114

115+
if ($article->getSlug() !== $slug) {
116+
$article->setSlug($slug);
117+
$changed = true;
118+
}
108119
if ($article->getTitle() !== $title) {
109120
$article->setTitle($title);
110121
$changed = true;

‎src/App/src/Fixture/articles_cleaned.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,7 @@
936936
},
937937
{
938938
"post_title": "Autologin using Cookie / Remember Me in Dotkernel",
939+
"post_slug": "autologin-cookie-remember-me-feature",
939940
"post_date": "2022-07-18 09:12:59",
940941
"post_status": "published",
941942
"author": {

0 commit comments

Comments
 (0)