MetaModels\DcGeneral\Events\MetaModel\CreateVariantButton::handleCreateVariantAction PHP Method

handleCreateVariantAction() public method

Handle the "create variant" event.
public handleCreateVariantAction ( ContaoCommunityAlliance\DcGeneral\Event\ActionEvent $event ) : void
$event ContaoCommunityAlliance\DcGeneral\Event\ActionEvent The action Event being executed.
return void
    public function handleCreateVariantAction(ActionEvent $event)
    {
        if ($event->getAction()->getName() != 'createvariant') {
            return;
        }
        $environment = $event->getEnvironment();
        $view = $environment->getView();
        $dataProvider = $environment->getDataProvider();
        $inputProvider = $environment->getInputProvider();
        $modelId = $inputProvider->hasParameter('id') ? ModelId::fromSerialized($inputProvider->getParameter('id')) : null;
        /** @var \MetaModels\DcGeneral\Data\Driver $dataProvider */
        $model = $dataProvider->createVariant($dataProvider->getEmptyConfig()->setId($modelId->getId()));
        if ($model == null) {
            throw new \RuntimeException(sprintf('Could not find model with id %s for creating a variant.', $modelId));
        }
        $metaModel = $this->getServiceContainer()->getFactory()->getMetaModel($model->getProviderName());
        if (!$metaModel || !$metaModel->hasVariants()) {
            return;
        }
        $preFunction = function ($environment, $model) {
            /** @var EnvironmentInterface $environment */
            $copyEvent = new PreCreateModelEvent($environment, $model);
            $environment->getEventDispatcher()->dispatch($copyEvent::NAME, $copyEvent);
        };
        $postFunction = function ($environment, $model) {
            /** @var EnvironmentInterface $environment */
            $copyEvent = new PostCreateModelEvent($environment, $model);
            $environment->getEventDispatcher()->dispatch($copyEvent::NAME, $copyEvent);
        };
        if (!$view instanceof BackendViewInterface) {
            throw new \InvalidArgumentException('Invalid view registered in environment.');
        }
        $editMask = new EditMask($view, $model, null, $preFunction, $postFunction, $this->breadcrumb($environment));
        $event->setResponse($editMask->execute());
    }