1 | <?php |
||
21 | class WidgetRenderer |
||
22 | { |
||
23 | private $container; |
||
24 | /** |
||
25 | * @var WidgetCache |
||
26 | */ |
||
27 | private $widgetCache; |
||
28 | /** |
||
29 | * @var WidgetHelper |
||
30 | */ |
||
31 | private $widgetHelper; |
||
32 | /** |
||
33 | * @var VictoireCollector |
||
34 | */ |
||
35 | private $victoireCollector; |
||
36 | /** |
||
37 | * @var BusinessPageHelper |
||
38 | */ |
||
39 | private $bepHelper; |
||
40 | |||
41 | /** |
||
42 | * WidgetRenderer constructor. |
||
43 | * |
||
44 | * @param Container $container |
||
45 | * @param WidgetCache $widgetCache |
||
46 | * @param WidgetHelper $widgetHelper |
||
47 | * @param VictoireCollector $victoireCollector |
||
48 | * @param BusinessPageHelper $bepHelper |
||
49 | * |
||
50 | * @internal param Client $redis |
||
51 | */ |
||
52 | public function __construct(Container $container, WidgetCache $widgetCache, WidgetHelper $widgetHelper, VictoireCollector $victoireCollector, BusinessPageHelper $bepHelper) |
||
60 | |||
61 | /** |
||
62 | * render the Widget. |
||
63 | * |
||
64 | * @param Widget $widget |
||
65 | * @param View $view |
||
66 | * |
||
67 | * @return widget show |
||
68 | */ |
||
69 | public function render(Widget $widget, View $view) |
||
70 | { |
||
71 | //the mode of display of the widget |
||
72 | $mode = $widget->getMode(); |
||
73 | |||
74 | //if entity is given and it's not the object, retrieve it and set the entity for the widget |
||
75 | if ($mode == Widget::MODE_BUSINESS_ENTITY && $view instanceof BusinessPage) { |
||
76 | $widget->setEntity($view->getEntity()); |
||
77 | } elseif ($view instanceof BusinessTemplate) { |
||
78 | //We'll try to find a sample entity to mock the widget behavior |
||
79 | /** @var EntityManager $entityManager */ |
||
80 | $entityManager = $this->container->get('doctrine.orm.entity_manager'); |
||
81 | if ($view->getBusinessEntity()->getType() === ORMBusinessEntity::TYPE) { |
||
82 | if ($mock = $this->bepHelper->getEntitiesAllowedQueryBuilder($view, $entityManager)->setMaxResults(1)->getQuery()->getOneOrNullResult()) { |
||
83 | $widget->setEntity($mock); |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 | |||
88 | //the templating service |
||
89 | $templating = $this->container->get('templating'); |
||
90 | |||
91 | //the content of the widget |
||
92 | |||
93 | $parameters = $this->container->get('victoire_widget.widget_content_resolver')->getWidgetContent($widget); |
||
94 | /* |
||
95 | * In some cases, for example, WidgetRender in BusinessEntity mode with magic variables {{entity.id}} transformed |
||
96 | * into the real business entity id, then if in the rendered action, we need to flush, it would persist the |
||
97 | * modified widget which really uncomfortable ;) |
||
98 | */ |
||
99 | if ($widget->getMode() == Widget::MODE_BUSINESS_ENTITY) { |
||
100 | $this->container->get('doctrine.orm.entity_manager')->refresh($widget); |
||
101 | } |
||
102 | |||
103 | //the template displayed is in the widget bundle (with the potential theme) |
||
104 | $showView = 'show'.ucfirst($widget->getTheme()); |
||
105 | $templateName = $this->container->get('victoire_widget.widget_helper')->getTemplateName($showView, $widget); |
||
106 | |||
107 | return $templating->render($templateName, $parameters); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * render a widget. |
||
112 | * |
||
113 | * @param Widget $widget |
||
114 | * @param View $view |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | public function renderContainer(Widget $widget, View $view) |
||
153 | |||
154 | /** |
||
155 | * prepare a widget to be rendered asynchronously. |
||
156 | * |
||
157 | * @param int $widgetId |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | public function prepareAsynchronousRender($widgetId) |
||
169 | |||
170 | /** |
||
171 | * render widget unlink action. |
||
172 | * |
||
173 | * @param int $widgetId |
||
174 | * @param View $view |
||
175 | * |
||
176 | * @return string |
||
177 | */ |
||
178 | public function renderUnlinkActionByWidgetId($widgetId, $view) |
||
188 | |||
189 | /** |
||
190 | * Compute slot options. |
||
191 | * |
||
192 | * @param int $slotId |
||
193 | * @param array $options |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | public function computeOptions($slotId, $options = []) |
||
226 | |||
227 | /** |
||
228 | * Get the extra classes for the css. |
||
229 | * |
||
230 | * @return string The classes |
||
231 | */ |
||
232 | public function getExtraCssClass(Widget $widget) |
||
238 | } |
||
239 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: