diff --git a/Controller/Adminhtml/Route/Edit.php b/Controller/Adminhtml/Route/Edit.php
index 6ceb283..3eaf424 100644
--- a/Controller/Adminhtml/Route/Edit.php
+++ b/Controller/Adminhtml/Route/Edit.php
@@ -53,6 +53,7 @@ public function execute()
return $resultRedirect->setPath('*/*/');
}
}
+ $model->setData('store_id', implode(',', $model->getStoreIds()));
$this->dataPersistor->set('prismicio_route', $model->getData());
// 3. Build edit form
diff --git a/Controller/Adminhtml/Route/Save.php b/Controller/Adminhtml/Route/Save.php
index 6e508b6..faba874 100644
--- a/Controller/Adminhtml/Route/Save.php
+++ b/Controller/Adminhtml/Route/Save.php
@@ -7,11 +7,22 @@
namespace Elgentos\PrismicIO\Controller\Adminhtml\Route;
+use Elgentos\PrismicIO\Model\ResourceModel\Route\Store\Collection as RouteStoreCollection;
+use Elgentos\PrismicIO\Model\Route\Store;
use Magento\Framework\Exception\LocalizedException;
+use Elgentos\PrismicIO\Model\Route\StoreFactory as RouteStoreFactory;
class Save extends \Magento\Backend\App\Action
{
+ /**
+ * @var RouteStoreFactory
+ */
+ public $routeStoryFactory;
+ /**
+ * @var Collection
+ */
+ public $routeStoreCollection;
protected $dataPersistor;
/**
@@ -20,10 +31,14 @@ class Save extends \Magento\Backend\App\Action
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
- \Magento\Framework\App\Request\DataPersistorInterface $dataPersistor
+ \Magento\Framework\App\Request\DataPersistorInterface $dataPersistor,
+ RouteStoreCollection $routeStoreCollection,
+ RouteStoreFactory $routeStoreFactory
) {
$this->dataPersistor = $dataPersistor;
+ $this->routeStoryFactory = $routeStoreFactory;
parent::__construct($context);
+ $this->routeStoreCollection = $routeStoreCollection;
}
/**
@@ -49,6 +64,17 @@ public function execute()
try {
$model->save();
+
+ $this->routeStoreCollection->addFieldToFilter('route_id', $model->getId())->each(function ($routeStore) {
+ $routeStore->delete();
+ });
+ foreach ($model->getData('store_id') as $storeId) {
+ $this->routeStoryFactory->create()->setData([
+ 'route_id' => $model->getId(),
+ 'store_id' => $storeId
+ ])->save();
+ }
+
$this->messageManager->addSuccessMessage(__('You saved the Route.'));
$this->dataPersistor->clear('prismicio_route');