1

I am trying to override the Resource model for the cms page file in the sitemap module

my code is

<?php
namespace MyModule\SiteMap\Model\ResourceModel\Cms;

use Magento\Cms\Api\Data\PageInterface;
use Magento\Cms\Api\GetUtilityPageIdentifiersInterface;
use Magento\Cms\Model\Page as CmsPage;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DB\Select;
use Magento\Framework\EntityManager\EntityManager;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
use Magento\Framework\Model\ResourceModel\Db\Context;

/**
 * Sitemap cms page collection model
 *
 * @api
 * @since 100.0.2
 */
class Page extends \Magento\Sitemap\Model\ResourceModel\Cms\Page
{
    /**
     * @var MetadataPool
     * @since 100.1.0
     */
    protected $metadataPool;

    /**
     * @var EntityManager
     * @since 100.1.0
     */
    protected $entityManager;

    /**
     * @var GetUtilityPageIdentifiersInterface
     */
    private $getUtilityPageIdentifiers;

    /**
     * @param Context                            $context
     * @param MetadataPool                       $metadataPool
     * @param EntityManager                      $entityManager
     * @param string                             $connectionName
     * @param GetUtilityPageIdentifiersInterface $getUtilityPageIdentifiers
     */
    public function __construct(
        Context $context,
        MetadataPool $metadataPool,
        EntityManager $entityManager,
        $connectionName = null,
        GetUtilityPageIdentifiersInterface $getUtilityPageIdentifiers = null
    ) {
        $this->metadataPool      = $metadataPool;
        $this->entityManager     = $entityManager;
        $this->getUtilityPageIdentifiers = $getUtilityPageIdentifiers ?:
            ObjectManager::getInstance()->get(GetUtilityPageIdentifiersInterface::class);
        parent::__construct($context,$connectionName);
    }

    /**
     * Retrieve cms page collection array
     *
     * @param int $storeId
     * @return array
     */
    public function getCollection($storeId)
    {
        $entityMetadata = $this->metadataPool->getMetadata(PageInterface::class);
        $linkField = $entityMetadata->getLinkField();

        $select = $this->getConnection()->select()->from(
            ['main_table' => $this->getMainTable()],
            [$this->getIdFieldName(), 'url' => 'identifier', 'updated_at' => 'update_time', 'title' => 'title',]
        )->join(
            ['store_table' => $this->getTable('cms_page_store')],
            "main_table.{$linkField} = store_table.$linkField",
            []
        )->where(
            'main_table.is_active = 1'
        )->where(
            'main_table.identifier NOT IN (?)',
            $this->getUtilityPageIdentifiers->execute()
        )->where(
            'store_table.store_id IN(?)',
            [0, $storeId]
        );

        $pages = [];
        $query = $this->getConnection()->query($select);
        while ($row = $query->fetch()) {
            $page = $this->_prepareObject($row);
            $pages[$page->getId()] = $page;
        }

        return $pages;
    }

    /**
     * Prepare page object
     *
     * @param array $data
     * @return \Magento\Framework\DataObject
     */
    protected function _prepareObject(array $data)
    {
        $object = new \Magento\Framework\DataObject();
        $object->setId($data[$this->getIdFieldName()]);
        $object->setUrl($data['url']);
        $object->setUpdatedAt($data['updated_at']);
        $object->setTitle($data['title']);

        return $object;
    }
}

but when I run setup:di:compile it throws an error as below:

bin/magento setup:di:compile
Compilation was started.
Interception cache generation... 6/9 [==================>---------]  66% 3 mins 558.0 MiBErrors during compilation:
        MyModule\SiteMap\Model\ResourceModel\Cms\Page
                Incompatible argument type: Required type: \Magento\Framework\EntityManager\EntityManager. Actual type: string; File: 
/var/www/html/wp11243/app/code/Calculus/SiteMap/Model/ResourceModel/Cms/Page.php

Total Errors Count: 1

In Log.php line 92:
                            
  Error during compilation  

Does anyone know how to fix this?

2
  • Hi there can you please share the PHP file again with the line numbers , especially in the line no = 92 , because i want to check that.Thanks Oct 5, 2022 at 18:03
  • No need , I worked out the solution. Just needed to pass all arguments into the parent in construct like so: parent::__construct($context,$metadataPool,$entityManager,$connectionName);
    – nickcobb
    Oct 6, 2022 at 9:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.