1

Can't create the page in the Store View programmatically.

I have two stores:

  1. English
  2. French

    $pageModel = \Magento\Framework\App\ObjectManager::getInstance()
        ->create(\Magento\Cms\Model\PageFactory::class)->create();
    $pageModel->load($submission['content_id']);
    $pageModel->setId(null);
    $pageModel->setContent($content);
    $pageModel->setStoreId($storeId);
    \Magento\Framework\App\ObjectManager::getInstance()
        ->create(\Magento\Cms\Api\PageRepositoryInterface::class)
        ->save($pageModel);
    

Result:

$origialStoreId = 1;

$storeId = 2;

Could not save the page: URL key for specified store already exists.

0

This is because URL rewrite rule - url_rewrite table:

UNIQUE KEY `URL_REWRITE_REQUEST_PATH_STORE_ID` (`request_path`,`store_id`),

Make two test cases:

1) Create a CMS page: Page in Websites > All Store Views

Take a look at url_rewrite. A record will be generated for each store.

enter image description here

So, in this case, your code above will have an error. Because Magento will try to insert the same row to database.

2) Create a CMS page: Page in Websites > Choose a specific store

Now we can use your code above.

Should take a look module cms and url-rewrite for more details.

Suggestion:

-Should use $pageModel->setStores($storesArray) to set stores for the cms page.

For example:

$pageModel->load($storeId);
......
$pageModel->setStores([1,2]);
$pageModel->save();
  • I've used setStore method )) – RDK Dec 13 '17 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.