Skip to content

Commit 169b59c

Browse files
authored
ENGCOM-3844: Sitemap filename can't exceed 32 characters #13937 #20044
2 parents 71d7346 + 2da1a26 commit 169b59c

File tree

3 files changed

+231
-63
lines changed

3 files changed

+231
-63
lines changed

app/code/Magento/Sitemap/Block/Adminhtml/Edit/Form.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ protected function _construct()
4848
}
4949

5050
/**
51+
* Configure form for sitemap.
52+
*
5153
* @return $this
5254
*/
5355
protected function _prepareForm()
@@ -73,7 +75,8 @@ protected function _prepareForm()
7375
'name' => 'sitemap_filename',
7476
'required' => true,
7577
'note' => __('example: sitemap.xml'),
76-
'value' => $model->getSitemapFilename()
78+
'value' => $model->getSitemapFilename(),
79+
'class' => 'validate-length maximum-length-32'
7780
]
7881
);
7982

app/code/Magento/Sitemap/Controller/Adminhtml/Sitemap/Save.php

Lines changed: 85 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,74 @@
55
*/
66
namespace Magento\Sitemap\Controller\Adminhtml\Sitemap;
77

8-
use Magento\Backend\App\Action;
8+
use Magento\Backend\App\Action\Context;
9+
use Magento\Framework\App\Action\HttpPostActionInterface;
910
use Magento\Framework\App\Filesystem\DirectoryList;
1011
use Magento\Framework\Controller;
12+
use Magento\Framework\Validator\StringLength;
13+
use Magento\MediaStorage\Model\File\Validator\AvailablePath;
14+
use Magento\Sitemap\Model\SitemapFactory;
1115

12-
class Save extends \Magento\Sitemap\Controller\Adminhtml\Sitemap
16+
/**
17+
* Save sitemap controller.
18+
*/
19+
class Save extends \Magento\Sitemap\Controller\Adminhtml\Sitemap implements HttpPostActionInterface
1320
{
21+
/**
22+
* Maximum length of sitemap filename
23+
*/
24+
const MAX_FILENAME_LENGTH = 32;
25+
26+
/**
27+
* @var StringLength
28+
*/
29+
private $stringValidator;
30+
31+
/**
32+
* @var AvailablePath
33+
*/
34+
private $pathValidator;
35+
36+
/**
37+
* @var \Magento\Sitemap\Helper\Data
38+
*/
39+
private $sitemapHelper;
40+
41+
/**
42+
* @var \Magento\Framework\Filesystem
43+
*/
44+
private $filesystem;
45+
46+
/**
47+
* @var SitemapFactory
48+
*/
49+
private $sitemapFactory;
50+
51+
/**
52+
* Save constructor.
53+
* @param Context $context
54+
* @param StringLength $stringValidator
55+
* @param AvailablePath $pathValidator
56+
* @param \Magento\Sitemap\Helper\Data $sitemapHelper
57+
* @param \Magento\Framework\Filesystem $filesystem
58+
* @param SitemapFactory $sitemapFactory
59+
*/
60+
public function __construct(
61+
Context $context,
62+
StringLength $stringValidator = null,
63+
AvailablePath $pathValidator = null,
64+
\Magento\Sitemap\Helper\Data $sitemapHelper = null,
65+
\Magento\Framework\Filesystem $filesystem = null,
66+
SitemapFactory $sitemapFactory = null
67+
) {
68+
parent::__construct($context);
69+
$this->stringValidator = $stringValidator ?: $this->_objectManager->get(StringLength::class);
70+
$this->pathValidator = $pathValidator ?: $this->_objectManager->get(AvailablePath::class);
71+
$this->sitemapHelper = $sitemapHelper ?: $this->_objectManager->get(\Magento\Sitemap\Helper\Data::class);
72+
$this->filesystem = $filesystem ?: $this->_objectManager->get(\Magento\Framework\Filesystem::class);
73+
$this->sitemapFactory = $sitemapFactory ?: $this->_objectManager->get(SitemapFactory::class);
74+
}
75+
1476
/**
1577
* Validate path for generation
1678
*
@@ -23,17 +85,25 @@ protected function validatePath(array $data)
2385
if (!empty($data['sitemap_filename']) && !empty($data['sitemap_path'])) {
2486
$data['sitemap_path'] = '/' . ltrim($data['sitemap_path'], '/');
2587
$path = rtrim($data['sitemap_path'], '\\/') . '/' . $data['sitemap_filename'];
26-
/** @var $validator \Magento\MediaStorage\Model\File\Validator\AvailablePath */
27-
$validator = $this->_objectManager->create(\Magento\MediaStorage\Model\File\Validator\AvailablePath::class);
28-
/** @var $helper \Magento\Sitemap\Helper\Data */
29-
$helper = $this->_objectManager->get(\Magento\Sitemap\Helper\Data::class);
30-
$validator->setPaths($helper->getValidPaths());
31-
if (!$validator->isValid($path)) {
32-
foreach ($validator->getMessages() as $message) {
88+
$this->pathValidator->setPaths($this->sitemapHelper->getValidPaths());
89+
if (!$this->pathValidator->isValid($path)) {
90+
foreach ($this->pathValidator->getMessages() as $message) {
91+
$this->messageManager->addErrorMessage($message);
92+
}
93+
// save data in session
94+
$this->_session->setFormData($data);
95+
// redirect to edit form
96+
return false;
97+
}
98+
99+
$filename = rtrim($data['sitemap_filename']);
100+
$this->stringValidator->setMax(self::MAX_FILENAME_LENGTH);
101+
if (!$this->stringValidator->isValid($filename)) {
102+
foreach ($this->stringValidator->getMessages() as $message) {
33103
$this->messageManager->addErrorMessage($message);
34104
}
35105
// save data in session
36-
$this->_objectManager->get(\Magento\Backend\Model\Session::class)->setFormData($data);
106+
$this->_session->setFormData($data);
37107
// redirect to edit form
38108
return false;
39109
}
@@ -49,9 +119,8 @@ protected function validatePath(array $data)
49119
*/
50120
protected function clearSiteMap(\Magento\Sitemap\Model\Sitemap $model)
51121
{
52-
/** @var \Magento\Framework\Filesystem\Directory\Write $directory */
53-
$directory = $this->_objectManager->get(\Magento\Framework\Filesystem::class)
54-
->getDirectoryWrite(DirectoryList::ROOT);
122+
/** @var \Magento\Framework\Filesystem $directory */
123+
$directory = $this->filesystem->getDirectoryWrite(DirectoryList::ROOT);
55124

56125
if ($this->getRequest()->getParam('sitemap_id')) {
57126
$model->load($this->getRequest()->getParam('sitemap_id'));
@@ -74,7 +143,7 @@ protected function saveData($data)
74143
{
75144
// init model and set data
76145
/** @var \Magento\Sitemap\Model\Sitemap $model */
77-
$model = $this->_objectManager->create(\Magento\Sitemap\Model\Sitemap::class);
146+
$model = $this->sitemapFactory->create();
78147
$this->clearSiteMap($model);
79148
$model->setData($data);
80149

@@ -85,13 +154,13 @@ protected function saveData($data)
85154
// display success message
86155
$this->messageManager->addSuccessMessage(__('You saved the sitemap.'));
87156
// clear previously saved data from session
88-
$this->_objectManager->get(\Magento\Backend\Model\Session::class)->setFormData(false);
157+
$this->_session->setFormData(false);
89158
return $model->getId();
90159
} catch (\Exception $e) {
91160
// display error message
92161
$this->messageManager->addErrorMessage($e->getMessage());
93162
// save data in session
94-
$this->_objectManager->get(\Magento\Backend\Model\Session::class)->setFormData($data);
163+
$this->_session->setFormData($data);
95164
}
96165
return false;
97166
}

0 commit comments

Comments
 (0)