Hi,
I developed a custom widget that needs to adjust its duration dynamically based on the max duration of the regions in the layout. Initially, I used the LayoutBuildEvent event, but it doesn’t seem to work for my specific case.
Searching through the code, I found other events that would suit my needs better. Unfortunately, I haven’t been able to catch them in my code.
public function init()
{
parent::init();
$dispatcher = $this->getDispatcher();
$dispatcher->addListener(WidgetAddEvent::$NAME, [$this, 'addedWidgetListener']);
}
public function addedWidgetListener(WidgetAddEvent $event): void
{
$module = $event->getModule();
if($module->region->duration > $this->getDuration())
{
try
{
$this->setUseDuration(1);
$this->setDuration($module->region->duration);
}
catch (InvalidArgumentException $e)
{
$this->getLog()->error('Error setting duration for widget: ' . $this->widget->getId());
}
}
}
Is there some problem with my code, or is this event just not catchable inside a widget?
EDIT:
Looking at the code of the CMS I found that the WidgetAddEvent is never dispatched. Basically, it is saved inside the module as a reference to be called by the method saveWidget(). The problem is that it will be 100% overridden by WidgetEditEvent. It happens because saveWidget() is called only by the widget’s method edit() which in turn is called from the module’s editWidget().