I need to change the functionality of the generating sitemap xml. For that, I used around plugin for the generateXml() method in the Magento\Sitemap\Model\Sitemap class. When I try to generate the file, the filename to be generated is coming as empty. So it produces exceptional error. How can we resolve it?
my di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Sitemap\Model\Sitemap">
<plugin name="SitemapSplitter" type="Company\SitemapSplitter\Model\Sitemap" sortOrder="1"/>
</type>
</config>
Company\SitemapSplitter\Model\Sitemap.php
<?php
namespace Company\SitemapSplitter\Model;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Model\AbstractModel;
class Sitemap extends \Magento\Sitemap\Model\Sitemap {
public function aroundGenerateXml(\Magento\Sitemap\Model\Sitemap $subject, \Closure $proceed, $requestInfo = null)
{
$this->_initSitemapItems();
/** @var $sitemapItem \Magento\Framework\DataObject */
foreach ($this->_sitemapItems as $sitemapItem) {
$changefreq = $sitemapItem->getChangefreq();
$priority = $sitemapItem->getPriority();
foreach ($sitemapItem->getCollection() as $item) {
$xml = $this->_getSitemapRow(
$item->getUrl(),
$item->getUpdatedAt(),
$changefreq,
$priority,
$item->getImages()
);
if ($this->_isSplitRequired($xml) && $this->_sitemapIncrement > 0) {
$this->_finalizeSitemap();
}
if (!$this->_fileSize) {
$this->_createSitemap();
}
$this->_writeSitemapRow($xml);
// Increase counters
$this->_lineCount++;
$this->_fileSize += strlen($xml);
}
}
$this->_finalizeSitemap();
if ($this->_sitemapIncrement == 1) {
// In case when only one increment file was created use it as default sitemap
$path = rtrim(
$this->getSitemapPath(),
'/'
) . '/' . $this->_getCurrentSitemapFilename(
$this->_sitemapIncrement
);
$destination = rtrim($this->getSitemapPath(), '/') . '/' . $this->getSitemapFilename();
$this->_directory->renameFile($path, $destination);
} else {
// Otherwise create index file with list of generated sitemaps
$this->_createSitemapIndex();
}
// Push sitemap to robots.txt
if ($this->_isEnabledSubmissionRobots()) {
$this->_addSitemapToRobotsTxt($this->getSitemapFilename());
}
$this->setSitemapTime($this->_dateModel->gmtDate('Y-m-d H:i:s'));
$this->save();
return $this;
}
}
?>