Skip to content

Commit 3cf1757

Browse files
ENGCOM-3844: Sitemap filename can't exceed 32 characters #13937 #20044
- Merge Pull Request #20044 from irajneeshgupta/magento2:irajneeshgupta-patch-13937 - Merged commits: 1. 55cf2dc 2. 035e8d7
2 parents 470fd45 + 035e8d7 commit 3cf1757

File tree

3 files changed

+217
-61
lines changed

3 files changed

+217
-61
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ protected function _prepareForm()
7373
'name' => 'sitemap_filename',
7474
'required' => true,
7575
'note' => __('example: sitemap.xml'),
76-
'value' => $model->getSitemapFilename()
76+
'value' => $model->getSitemapFilename(),
77+
'class' => 'validate-length maximum-length-32'
7778
]
7879
);
7980

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

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,61 @@
1111

1212
class Save extends \Magento\Sitemap\Controller\Adminhtml\Sitemap
1313
{
14+
/**
15+
* Maximum length of sitemap filename
16+
*/
17+
const MAX_FILENAME_LENGTH = 32;
18+
19+
/**
20+
* @var \Magento\Framework\Validator\StringLength
21+
*/
22+
private $stringValidator;
23+
24+
/**
25+
* @var \Magento\MediaStorage\Model\File\Validator\AvailablePath
26+
*/
27+
private $pathValidator;
28+
29+
/**
30+
* @var \Magento\Sitemap\Helper\Data
31+
*/
32+
private $sitemapHelper;
33+
34+
/**
35+
* @var \Magento\Framework\Filesystem
36+
*/
37+
private $filesystem;
38+
39+
/**
40+
* @var \Magento\Sitemap\Model\SitemapFactory
41+
*/
42+
private $sitemapFactory;
43+
44+
/**
45+
* Save constructor.
46+
* @param Action\Context $context
47+
* @param \Magento\Framework\Validator\StringLength $stringValidator
48+
* @param \Magento\MediaStorage\Model\File\Validator\AvailablePath $pathValidator
49+
* @param \Magento\Sitemap\Helper\Data $sitemapHelper
50+
* @param \Magento\Framework\Filesystem $filesystem
51+
* @param \Magento\Sitemap\Model\SitemapFactory $sitemapFactory
52+
*/
53+
public function __construct(
54+
\Magento\Backend\App\Action\Context $context,
55+
\Magento\Framework\Validator\StringLength $stringValidator = null,
56+
\Magento\MediaStorage\Model\File\Validator\AvailablePath $pathValidator = null,
57+
\Magento\Sitemap\Helper\Data $sitemapHelper = null,
58+
\Magento\Framework\Filesystem $filesystem = null,
59+
\Magento\Sitemap\Model\SitemapFactory $sitemapFactory = null
60+
) {
61+
parent::__construct($context);
62+
$this->stringValidator = $stringValidator ?: $this->_objectManager->get(\Magento\Framework\Validator\StringLength::class);
63+
$this->pathValidator = $pathValidator ?: $this->_objectManager->get(\Magento\MediaStorage\Model\File\Validator\AvailablePath::class);
64+
$this->sitemapHelper = $sitemapHelper ?: $this->_objectManager->get(\Magento\Sitemap\Helper\Data::class);
65+
$this->filesystem = $filesystem ?: $this->_objectManager->get(\Magento\Framework\Filesystem::class);
66+
$this->sitemapFactory = $sitemapFactory ?: $this->_objectManager->get(\Magento\Sitemap\Model\SitemapFactory::class);
67+
}
68+
1469
/**
1570
* Validate path for generation
1671
*
@@ -23,17 +78,25 @@ protected function validatePath(array $data)
2378
if (!empty($data['sitemap_filename']) && !empty($data['sitemap_path'])) {
2479
$data['sitemap_path'] = '/' . ltrim($data['sitemap_path'], '/');
2580
$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) {
81+
$this->pathValidator->setPaths($this->sitemapHelper->getValidPaths());
82+
if (!$this->pathValidator->isValid($path)) {
83+
foreach ($this->pathValidator->getMessages() as $message) {
84+
$this->messageManager->addErrorMessage($message);
85+
}
86+
// save data in session
87+
$this->_session->setFormData($data);
88+
// redirect to edit form
89+
return false;
90+
}
91+
92+
$filename = rtrim($data['sitemap_filename']);
93+
$this->stringValidator->setMax(self::MAX_FILENAME_LENGTH);
94+
if (!$this->stringValidator->isValid($filename)) {
95+
foreach ($this->stringValidator->getMessages() as $message) {
3396
$this->messageManager->addErrorMessage($message);
3497
}
3598
// save data in session
36-
$this->_objectManager->get(\Magento\Backend\Model\Session::class)->setFormData($data);
99+
$this->_session->setFormData($data);
37100
// redirect to edit form
38101
return false;
39102
}
@@ -49,9 +112,8 @@ protected function validatePath(array $data)
49112
*/
50113
protected function clearSiteMap(\Magento\Sitemap\Model\Sitemap $model)
51114
{
52-
/** @var \Magento\Framework\Filesystem\Directory\Write $directory */
53-
$directory = $this->_objectManager->get(\Magento\Framework\Filesystem::class)
54-
->getDirectoryWrite(DirectoryList::ROOT);
115+
/** @var \Magento\Framework\Filesystem $directory */
116+
$directory = $this->filesystem->getDirectoryWrite(DirectoryList::ROOT);
55117

56118
if ($this->getRequest()->getParam('sitemap_id')) {
57119
$model->load($this->getRequest()->getParam('sitemap_id'));
@@ -74,7 +136,7 @@ protected function saveData($data)
74136
{
75137
// init model and set data
76138
/** @var \Magento\Sitemap\Model\Sitemap $model */
77-
$model = $this->_objectManager->create(\Magento\Sitemap\Model\Sitemap::class);
139+
$model = $this->sitemapFactory->create();
78140
$this->clearSiteMap($model);
79141
$model->setData($data);
80142

@@ -85,13 +147,13 @@ protected function saveData($data)
85147
// display success message
86148
$this->messageManager->addSuccessMessage(__('You saved the sitemap.'));
87149
// clear previously saved data from session
88-
$this->_objectManager->get(\Magento\Backend\Model\Session::class)->setFormData(false);
150+
$this->_session->setFormData(false);
89151
return $model->getId();
90152
} catch (\Exception $e) {
91153
// display error message
92154
$this->messageManager->addErrorMessage($e->getMessage());
93155
// save data in session
94-
$this->_objectManager->get(\Magento\Backend\Model\Session::class)->setFormData($data);
156+
$this->_session->setFormData($data);
95157
}
96158
return false;
97159
}

0 commit comments

Comments
 (0)