From 24da51cf1be4ca91459f296ddc3523b99df05f3a Mon Sep 17 00:00:00 2001 From: Leny BERNARD Date: Sat, 16 Apr 2016 04:16:05 +0200 Subject: [PATCH] try to find a sample entity to mock the widget behavior or throw an exception to ask for at least one instance Conflicts: Bundle/WidgetBundle/Renderer/WidgetRenderer.php --- ...MissingBusinessEntityInstanceException.php | 23 +++++++++++++++++++ Bundle/WidgetBundle/Entity/Widget.php | 2 +- .../WidgetBundle/Renderer/WidgetRenderer.php | 18 ++++++++++++++- 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 Bundle/BusinessEntityBundle/Exception/MissingBusinessEntityInstanceException.php diff --git a/Bundle/BusinessEntityBundle/Exception/MissingBusinessEntityInstanceException.php b/Bundle/BusinessEntityBundle/Exception/MissingBusinessEntityInstanceException.php new file mode 100644 index 000000000..51bfe6cbb --- /dev/null +++ b/Bundle/BusinessEntityBundle/Exception/MissingBusinessEntityInstanceException.php @@ -0,0 +1,23 @@ +message = sprintf( + 'There isn\'t any instance of %s but at least one is required. Please create one and retry.', + $className + ); + } +} \ No newline at end of file diff --git a/Bundle/WidgetBundle/Entity/Widget.php b/Bundle/WidgetBundle/Entity/Widget.php index bc03bbaf5..f2c99d425 100644 --- a/Bundle/WidgetBundle/Entity/Widget.php +++ b/Bundle/WidgetBundle/Entity/Widget.php @@ -391,7 +391,7 @@ public function removeWidgetMap(WidgetMap $widgetMap) /** * Set the entity. * - * @param unknown $entity + * @param object $entity */ public function setEntity($entity) { diff --git a/Bundle/WidgetBundle/Renderer/WidgetRenderer.php b/Bundle/WidgetBundle/Renderer/WidgetRenderer.php index f0aa42694..5b64d4900 100644 --- a/Bundle/WidgetBundle/Renderer/WidgetRenderer.php +++ b/Bundle/WidgetBundle/Renderer/WidgetRenderer.php @@ -2,8 +2,12 @@ namespace Victoire\Bundle\WidgetBundle\Renderer; +use Doctrine\ORM\EntityManager; use Symfony\Component\DependencyInjection\Container; +use Victoire\Bundle\BusinessEntityBundle\Exception\MissingBusinessEntityInstanceException; +use Victoire\Bundle\BusinessEntityBundle\Helper\BusinessEntityHelper; use Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage; +use Victoire\Bundle\BusinessPageBundle\Entity\BusinessTemplate; use Victoire\Bundle\CoreBundle\Entity\View; use Victoire\Bundle\CoreBundle\Event\WidgetRenderEvent; use Victoire\Bundle\CoreBundle\VictoireCmsEvents; @@ -35,9 +39,21 @@ public function render(Widget $widget, View $view) //the mode of display of the widget $mode = $widget->getMode(); - //if entty is given and it's not the object, retrive it and set the entity for the widget + //if entity is given and it's not the object, retrieve it and set the entity for the widget if ($mode == Widget::MODE_BUSINESS_ENTITY && $view instanceof BusinessPage) { $widget->setEntity($view->getBusinessEntity()); + } elseif ($view instanceof BusinessTemplate) { + //We'll try to find a sample entity to mock the widget behavior + /** @var EntityManager $entityManager */ + $entityManager = $this->container->get('doctrine.orm.entity_manager'); + /** @var BusinessEntityHelper $businessEntityHelper */ + $businessEntityHelper = $this->container->get('victoire_core.helper.business_entity_helper'); + $businessEntity = $businessEntityHelper->findById($view->getBusinessEntityId()); + $queryBuilder = $entityManager->getRepository($businessEntity->getClass())->createQueryBuilder('c'); + if (null === $mock = $queryBuilder->setMaxResults(1)->getQuery()->getOneOrNullResult()) { + throw new MissingBusinessEntityInstanceException($businessEntity->getClass()); + } + $widget->setEntity($mock); } //the templating service