Skip to content

32913 set url key for page before validation #33043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CmsUrlRewrite\Plugin\Cms\Model\PageRepository;

use Magento\Cms\Api\Data\PageInterface;
use Magento\Cms\Model\PageRepository\ValidationComposite;
use Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator;

/**
* Generate url_key if the merchant didn't fill this field
*/
class ValidationCompositePlugin
{
/**
* @var CmsPageUrlPathGenerator
*/
private $cmsPageUrlPathGenerator;

/**
* @param CmsPageUrlPathGenerator $cmsPageUrlPathGenerator
*/
public function __construct(
CmsPageUrlPathGenerator $cmsPageUrlPathGenerator
) {
$this->cmsPageUrlPathGenerator = $cmsPageUrlPathGenerator;
}

/**
* Before save handler
*
* @param ValidationComposite $subject
* @param PageInterface $page
*/
public function beforeSave(
ValidationComposite $subject,
PageInterface $page
) {
$urlKey = $page->getData('identifier');
if ($urlKey === '' || $urlKey === null) {
$page->setData('identifier', $this->cmsPageUrlPathGenerator->generateUrlKey($page));
}
}
}
4 changes: 4 additions & 0 deletions app/code/Magento/CmsUrlRewrite/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<type name="Magento\Cms\Model\ResourceModel\Page">
<plugin name="cms_url_rewrite_plugin" type="Magento\CmsUrlRewrite\Plugin\Cms\Model\ResourceModel\Page"/>
</type>
<type name="Magento\Cms\Model\PageRepository\ValidationComposite">
<plugin name="cms_validate_url_plugin"
type="Magento\CmsUrlRewrite\Plugin\Cms\Model\PageRepository\ValidationCompositePlugin" sortOrder="10"/>
</type>
<type name="Magento\UrlRewrite\Model\UrlRewrite">
<arguments>
<argument name="entityToCacheTagMap" xsi:type="array">
Expand Down