BUG: Event listener doesn't catch WidgetAddEvent event

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().

Hi,

I found other events that would suit my needs better.

The reason we haven’t published them is because they may be removed in a later version - they are internal events, so use them at your own risk :smile:

ModuleWidget::saveWidget() is called during ModuleWidget::add(), which is called automatically when the widget is first added to the layout/playlist, so I would expect it to work as it stands now.

I see your :bug:

We will take a look at that for the next release and make sure the event is being fired as expected.

I developed a custom widget that needs to adjust its duration dynamically based on the max duration of the regions in the layout.

If your widget sits in a region on its own, then that is the default behaviour - it will always be as long as the longest region.