Skip to content

Commit

Permalink
Add new event to make sure node info can be altered before it is pers…
Browse files Browse the repository at this point in the history
…isted to the database.
  • Loading branch information
ozmodiar committed Apr 11, 2015
1 parent 7c22266 commit 8ebcd8b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Clastic/NodeBundle/Controller/NodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Clastic\BackofficeBundle\Controller\AbstractModuleController;
use Clastic\NodeBundle\Entity\Node;
use Clastic\NodeBundle\Event\NodeFormPrePersistEvent;
use Clastic\NodeBundle\Event\NodeFormPersistEvent;
use Clastic\NodeBundle\Node\NodeManager;
use Clastic\NodeBundle\Node\NodeReferenceInterface;
Expand Down Expand Up @@ -147,6 +148,9 @@ protected function persistData(Form $form)
$node = $form->getData()->getNode();
$node->setChanged(new \DateTime());

$this->get('event_dispatcher')
->dispatch(NodeFormPrePersistEvent::NODE_FORM_PREPERSIST, new NodeFormPrePersistEvent($node, $form));

$objectManager = $this->getDoctrine()->getManager();
$objectManager->persist($form->getData());
$objectManager->flush();
Expand Down
58 changes: 58 additions & 0 deletions src/Clastic/NodeBundle/Event/NodeFormPrePersistEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* This file is part of the Clastic package.
*
* (c) Dries De Peuter <dries@nousefreak.be>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Clastic\NodeBundle\Event;

use Clastic\NodeBundle\Entity\Node;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\Form\Form;

/**
* @author Dries De Peuter <dries@nousefreak.be>
*/
class NodeFormPrePersistEvent extends Event
{
const NODE_FORM_PREPERSIST = 'clastic.node.form.prepersist';

/**
* @var Node
*/
private $node;

/**
* @var Form
*/
private $form;

/**
* @param Node $node
* @param Form $form
*/
public function __construct(Node $node, Form $form)
{
$this->node = $node;
$this->form = $form;
}

/**
* @return Node
*/
public function getNode()
{
return $this->node;
}

/**
* @return Form
*/
public function getForm()
{
return $this->form;
}
}

0 comments on commit 8ebcd8b

Please sign in to comment.