I need to customize the function _getStoreBaseUrl() from the core file magento\module-sitemap\Model\Sitemap.php.

I have tried to override the Model file using preference method and also tried to implement Plugin, but it's throwing error when I write a constructor method in it.

Fatal error: Uncaught Error: Call to a member function isExist() on null in \vendor\magento\module-sitemap\Model\Sitemap.php:351 Stack trace: #0 \vendor\magento\framework\Model\ResourceModel\Db\AbstractDb.php(406): Magento\Sitemap\Model\Sitemap->beforeSave() #1 \vendor\magento\framework\Model\AbstractModel.php(647): Magento\Framework\Model\ResourceModel\Db\AbstractDb->save(Object(Vendor\Sitemap\Model\Sitemap)) #2   \vendor\magento\module-sitemap\Controller\Adminhtml\Sitemap\Save.php(84): Magento\Framework\Model\AbstractModel->save() #3   \vendor\magento\module-sitemap\Controller\Adminhtml\Sitemap\Save.php(150): Magento\Sitemap\Controller\Adminhtml\Sitemap\Save->saveData(Array) #4   \generated\code\Magento\Sitemap\Controller\Adminhtml\Sitemap\Save\Interceptor.php(24): Magento\Sitemap\Controller\Adminhtml\Sitemap\Save->execute() #5   \vendor\magento\framework\App\Action\Action.php(107): Magent in   \vendor\magento\module-sitemap\Model\Sitemap.php on line 351

I need to use the dependency injection way in this overrided file to get some custom values from the ScopeConfig.

My Model file is,

enter image description here

My di.xml is,

enter image description here

Can anyone tell me how to fix this ?

  • Can you please update your di.xml and model code in question? – Rutvee Sojitra Aug 29 '18 at 10:46
  • yes, I have updated the post. – MGento Aug 29 '18 at 11:00
  • actually you have used constructor so please use this too \Magento\Framework\Filesystem $filesystem, \Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot $documentRoot = null and $this->_directory = $filesystem->getDirectoryWrite($documentRoot->getPath()); inside your constructor – Ansar Husain Aug 29 '18 at 11:03

Your construct should be like below:

 public function __construct(
    \Magento\Framework\Model\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Framework\Escaper $escaper,
    \Magento\Sitemap\Helper\Data $sitemapData,
    \Magento\Framework\Filesystem $filesystem,
    \Magento\Sitemap\Model\ResourceModel\Catalog\CategoryFactory $categoryFactory,
    \Magento\Sitemap\Model\ResourceModel\Catalog\ProductFactory $productFactory,
    \Magento\Sitemap\Model\ResourceModel\Cms\PageFactory $cmsFactory,
    \Magento\Framework\Stdlib\DateTime\DateTime $modelDate,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Framework\App\RequestInterface $request,
    \Magento\Framework\Stdlib\DateTime $dateTime,
    \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
    \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    array $data = [],
    \Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot $documentRoot = null
) {
    $this->_escaper = $escaper;
    $this->_sitemapData = $sitemapData;
    $documentRoot = $documentRoot ?: ObjectManager::getInstance()->get(DocumentRoot::class);
    $this->_directory = $filesystem->getDirectoryWrite($documentRoot->getPath());
    $this->_categoryFactory = $categoryFactory;
    $this->_productFactory = $productFactory;
    $this->_cmsFactory = $cmsFactory;
    $this->_dateModel = $modelDate;
    $this->_storeManager = $storeManager;
    $this->_request = $request;
    $this->dateTime = $dateTime;
    $this->_scopeConfig = $scopeConfig;

    parent::__construct($context, $registry,$escaper, $sitemapData,$filesystem,$categoryFactory,$productFactory,$cmsFactory,$modelDate,$storeManager,$request,$dateTime,$resource,$resourceCollection,$data,$documentRoot);
| improve this answer | |
  • Then it's throwing error as Fatal error: Uncaught Error: Cannot instantiate abstract class Magento\Framework\Data\Collection\AbstractDb in \vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php:111 – MGento Aug 29 '18 at 11:23
  • I have tried all these ways, but not working – MGento Aug 29 '18 at 11:23
  • Check removing from parent::constuct resource and resourceConnection and check @MGento – Rutvee Sojitra Aug 29 '18 at 11:25
  • still, same error. – MGento Aug 29 '18 at 11:28
  • still same issue facing if any one know reply me – Rv Singh Jul 16 '19 at 14:37

Add in your constructor below code too

use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot;
use Magento\Framework\App\ObjectManager;
use Magento\Robots\Model\Config\Value;
use Magento\Framework\DataObject;

     public function __construct(

    \Magento\Framework\Filesystem $filesystem,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot $documentRoot = null

) {
    $documentRoot = $documentRoot ?: ObjectManager::getInstance()->get(DocumentRoot::class);
    $this->_directory = $filesystem->getDirectoryWrite($documentRoot->getPath());
| improve this answer | |
  • Sorry, but it won't work even if I add those in my constructor . – MGento Aug 29 '18 at 11:16
  • what is error now ? i think you should copy complete constructor and add your new dependency also on that constructor. – Ansar Husain Aug 29 '18 at 11:17
  • I have updated code – Ansar Husain Aug 29 '18 at 11:23
  • Tried all these ways already but not working. I also tried the plugin way too. The above way gives error like Fatal error: Uncaught Error: Cannot instantiate abstract class Magento\Framework\Data\Collection\AbstractDb in \vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php:111 – MGento Aug 29 '18 at 11:24
  • can you use constructor like that only and check ? – Ansar Husain Aug 29 '18 at 11:47

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.