/** * Get content for the widget. * * @param Widget $widget * * @throws \Exception * * @return array */ public function getWidgetContent(Widget $widget) { //the mode of display of the widget $mode = $widget->getMode(); //the widget must have a mode if ($mode === null) { throw new \Exception('The widget [' . $widget->getId() . '] has no mode.'); } $resolver = $this->widgetContentResolverChain->getResolverForWidget($widget); switch ($mode) { case Widget::MODE_STATIC: $parameters = $resolver->getWidgetStaticContent($widget); break; case Widget::MODE_ENTITY: //get the content of the widget with its entity $parameters = $resolver->getWidgetEntityContent($widget); break; case Widget::MODE_BUSINESS_ENTITY: //get the content of the widget with its entity $parameters = $resolver->getWidgetBusinessEntityContent($widget); break; case Widget::MODE_QUERY: $parameters = $resolver->getWidgetQueryContent($widget); break; default: throw new \Exception('The mode [' . $mode . '] is not supported by the widget manager. Widget ID:[' . $widget->getId() . ']'); } return $parameters; }
/** * render the Widget. * * @param Widget $widget * @param View $view * * @return widget show */ public function render(Widget $widget, View $view) { //the mode of display of the widget $mode = $widget->getMode(); //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'); if ($mock = $this->bepHelper->getEntitiesAllowedQueryBuilder($view, $entityManager)->setMaxResults(1)->getQuery()->getOneOrNullResult()) { $widget->setEntity($mock); } } //the templating service $templating = $this->container->get('templating'); //the content of the widget $parameters = $this->container->get('victoire_widget.widget_content_resolver')->getWidgetContent($widget); /* * In some cases, for example, WidgetRender in BusinessEntity mode with magic variables {{entity.id}} transformed * into the real business entity id, then if in the rendered action, we need to flush, it would persist the * modified widget which really uncomfortable ;) */ $this->container->get('doctrine.orm.entity_manager')->refresh($widget); //the template displayed is in the widget bundle (with the potential theme) $showView = 'show' . ucfirst($widget->getTheme()); $templateName = $this->container->get('victoire_widget.widget_helper')->getTemplateName($showView, $widget); return $templating->render($templateName, $parameters); }
/** * @param Widget $widget * * @return string */ protected function getHash(Widget $widget) { $hash = null; if (!$widget instanceof WidgetSlotInterface) { if ($widget->getMode() == Widget::MODE_BUSINESS_ENTITY && ($entity = $widget->getEntity()) && method_exists($widget->getEntity(), 'getUpdatedAt')) { $hash = $this->generateBusinessEntityHash($widget); } elseif ($widget->getMode() == Widget::MODE_STATIC) { $hash = $this->generateHash($widget); } } return $hash; }