Skip to content

Commit

Permalink
Add blog post summary
Browse files Browse the repository at this point in the history
  • Loading branch information
AchillesKal committed Apr 7, 2024
1 parent da5ee09 commit 9d629bd
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 6 deletions.
26 changes: 26 additions & 0 deletions migrations/Version20240407081339.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20240407081339 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE blog_post ADD summary VARCHAR(1000) DEFAULT NULL');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE blog_post DROP summary');
}
}
15 changes: 15 additions & 0 deletions src/Entity/BlogPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class BlogPost
#[ORM\Column(length: 255, nullable: true)]
private ?string $banner = null;

#[ORM\Column(length: 1000, nullable: true)]
private ?string $summary = null;

public function __construct()
{
$this->tags = new ArrayCollection();
Expand Down Expand Up @@ -135,4 +138,16 @@ public function setBanner(?string $banner): static

return $this;
}

public function getSummary(): ?string
{
return $this->summary;
}

public function setSummary(?string $summary): static
{
$this->summary = $summary;

return $this;
}
}
3 changes: 2 additions & 1 deletion src/Factory/BlogPostFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ protected function getDefaults(): array
$file = new UploadedFile($temporaryImagePath, $filename);
return [
'title' => ucfirst(self::faker()->words(5, true)),
'content' => self::faker()->paragraphs(10, true),
'summary' => self::faker()->text,
'content' => self::faker()->randomHtml(3, 6),
'banner' => $file->getFilename(),
];
}
Expand Down
5 changes: 5 additions & 0 deletions src/Form/BlogPostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
])
],
])
->add('summary', TextareaType::class, [
'required' => false,
'attr' => ['class' => 'tinymce'],
'purify_html' => true,
])
->add('content', TextareaType::class, [
'required' => false,
'attr' => ['class' => 'tinymce'],
Expand Down
5 changes: 5 additions & 0 deletions templates/blog_post/_form.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
{{ form_widget(form.banner) }}
</div>

<div>
<label for="blog_post_summary" class="text-sm font-medium">Summary</label>
{{ form_widget(form.summary, {'attr': {'class': 'shadow-inner w-full bg-gray-100 hover:border-gray-900 transition-all border text-gray-900 mt-2 p-4 rounded-xl focus:outline-none focus:shadow-outline', 'rows': '20', 'placeholder': 'Add summary'}}) }}
</div>

<div>
<label for="blog_post_content" class="text-sm font-medium">Content</label>
{{ form_widget(form.content, {'attr': {'class': 'shadow-inner w-full bg-gray-100 hover:border-gray-900 transition-all border text-gray-900 mt-2 p-4 rounded-xl focus:outline-none focus:shadow-outline', 'rows': '20', 'placeholder': 'Add content'}}) }}
Expand Down
5 changes: 5 additions & 0 deletions templates/blog_post/edit.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
.catch( error => {
console.error( error );
} );
ClassicEditor
.create( document.querySelector( '#blog_post_summary' ) )
.catch( error => {
console.error( error );
} );
});
</script>
Expand Down
2 changes: 1 addition & 1 deletion templates/blog_post/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<h2 class="text-balance @xl:leading-tight text-2xl font-bold @xl:text-5xl text-black">{{ blog_post.title }}</h2>
</a>
<p class="@xl:text-2xl text-md font-light text-black max-w-4xl line-clamp-3 @xl:leading-9 leading-relaxed">
{{ blog_post.content|purify }}
{{ blog_post.summary|purify }}
</p>
</div>

Expand Down
13 changes: 9 additions & 4 deletions templates/blog_post/new.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
<script>
document.addEventListener('DOMContentLoaded', function() {
ClassicEditor
.create( document.querySelector( '#blog_post_content' ) )
.catch( error => {
console.error( error );
} );
.create(document.querySelector('#blog_post_content'))
.catch(error => {
console.error(error);
});
ClassicEditor
.create(document.querySelector('#blog_post_summary'))
.catch(error => {
console.error(error);
});
});
</script>
{% endblock %}

0 comments on commit 9d629bd

Please sign in to comment.