public static function onBeforeSave(Event $event)
    {
        $layout = $event->getTarget();
        if (!is_a($layout, 'BackBee\Site\Layout')) {
            return;
        }

        $dispatcher = $event->getDispatcher();
        if (null !== $dispatcher->getApplication()) {
            if (is_a($event->getEventArgs(), 'Doctrine\ORM\Event\PreUpdateEventArgs')) {
                if (!$event->getEventArgs()->hasChangedField('_data')) {
                    return;
                }
            }

            // Update the layout thumbnail - Beware of generate thumbnail before any other operation
            $thumb = $dispatcher->getApplication()->getEntityManager()
                    ->getRepository('BackBee\Site\Layout')
                    ->generateThumbnail($layout, $dispatcher->getApplication());

            // Update the layout file
            try {
                $dispatcher->getApplication()->getRenderer()->updateLayout($layout);
            } catch (\BackBee\Renderer\Exception\RendererException $e) {
                $dispatcher->getApplication()->warning($e->getMessage());
            }

            if (is_a($event->getEventArgs(), 'Doctrine\ORM\Event\PreUpdateEventArgs')) {
                if ($event->getEventArgs()->hasChangedField('_picpath')) {
                    $event->getEventArgs()->setNewValue('_picpath', $thumb);
                }
            }
        }
    }