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

try to find a sample entity to mock the widget behavior or throw an e… #384

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Victoire\Bundle\BusinessEntityBundle\Exception;

/**
* Triggered when at least one Business entity instance is needed
*/
class MissingBusinessEntityInstanceException extends \Exception
{

/**
* MissingBusinessEntityException constructor.
*
* @param string $className
*/
public function __construct($className)
{
$this->message = sprintf(
'There isn\'t any instance of %s but at least one is required. Please create one and retry.',
$className
);
}
}
2 changes: 1 addition & 1 deletion Bundle/WidgetBundle/Entity/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ public function removeWidgetMap(WidgetMap $widgetMap)
/**
* Set the entity.
*
* @param unknown $entity
* @param object $entity
*/
public function setEntity($entity)
{
Expand Down
18 changes: 17 additions & 1 deletion Bundle/WidgetBundle/Renderer/WidgetRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down