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

Make blog post titles unique #1062

Merged
merged 3 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added a form event to set the slug
  • Loading branch information
javiereguiluz committed Jan 8, 2020
commit 7486dc82016d73e86c63154cddadb462e1368608
8 changes: 2 additions & 6 deletions src/Controller/Admin/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\String\Slugger\SluggerInterface;

/**
* Controller used to manage blog contents in the backend.
Expand Down Expand Up @@ -70,7 +69,7 @@ public function index(PostRepository $posts): Response
* to constraint the HTTP methods each controller responds to (by default
* it responds to all methods).
*/
public function new(Request $request, SluggerInterface $slugger): Response
public function new(Request $request): Response
{
$post = new Post();
$post->setAuthor($this->getUser());
Expand All @@ -86,8 +85,6 @@ public function new(Request $request, SluggerInterface $slugger): Response
// However, we explicitly add it to improve code readability.
// See https://symfony.com/doc/current/forms.html#processing-forms
if ($form->isSubmitted() && $form->isValid()) {
$post->setSlug($slugger->slug($post->getTitle())->lower());

$em = $this->getDoctrine()->getManager();
$em->persist($post);
$em->flush();
Expand Down Expand Up @@ -133,13 +130,12 @@ public function show(Post $post): Response
* @Route("/{id<\d+>}/edit",methods={"GET", "POST"}, name="admin_post_edit")
* @IsGranted("edit", subject="post", message="Posts can only be edited by their authors.")
*/
public function edit(Request $request, Post $post, SluggerInterface $slugger): Response
public function edit(Request $request, Post $post): Response
{
$form = $this->createForm(PostType::class, $post);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$post->setSlug($slugger->slug($post->getTitle())->lower());
$this->getDoctrine()->getManager()->flush();

$this->addFlash('success', 'post.updated_successfully');
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* @ORM\Entity(repositoryClass="App\Repository\PostRepository")
* @ORM\Table(name="symfony_demo_post")
* @UniqueEntity(fields={"slug"}, errorPath="title")
* @UniqueEntity(fields={"slug"}, errorPath="title", message="This title was already used in another blog post, but they must be unique.")
*
* Defines the properties of the Post entity to represent the blog posts.
*
Expand Down
21 changes: 21 additions & 0 deletions src/Form/PostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\String\Slugger\SluggerInterface;

/**
* Defines the form used to create and manipulate blog posts.
Expand All @@ -28,6 +31,14 @@
*/
class PostType extends AbstractType
{
private $slugger;

// Form types are services, so you can inject other services in them if needed
public function __construct(SluggerInterface $slugger)
{
$this->slugger = $slugger;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -64,6 +75,16 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'label' => 'label.tags',
'required' => false,
])
// form events let you modify information or fields at different steps
// of the form handling process.
// See https://symfony.com/doc/current/form/events.html
->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) {
/** @var Post */
$post = $event->getData();
if (null !== $postTitle = $post->getTitle()) {
$post->setSlug($this->slugger->slug($postTitle)->lower());
}
})
;
}

Expand Down