cancel
Showing results for 
Search instead for 
Did you mean: 

REST call to cmsPage/search does not return page id's

SOLVED

REST call to cmsPage/search does not return page id's

The REST API for for cmsPage and cmsBlock depends on the user knowing the id of the page or block item you want to work with. According to the swagger documentation the id will be returned by the search operation as follows:

 

{
	"items":[
		{
			"id":0,
			"identifier":"string",
			"title":"string",
			"pageLayout":"string",
			...
		}
	]
}

Unfortunately, in reality, the output I get is identical to the example, except that the id field is omitted. As such any successive calls to other entry points are impossible to make.

 

I'm using the latest devbox distribution (Magento CE 2.1), running on up-to-date Docker on macOS Sierra 10.12.3.

 

I've been going through the code (in particular the relevant interfaces and implementations in the module-cms code), but I can't spot any obvious points where in the API the id entry is being lost. 

 

Are there any suggestions on how to fix the output of this REST call?

1 ACCEPTED SOLUTION

Accepted Solutions

Re: REST call to cmsPage/search does not return page id's

It's not possible at the moment because of the way how Magento2 maps data objects and interfaces. In order to fix it, you'll have to add following lines into vendor/magento/module-cms/Model/Page.php:

    /**
     * Get ID
     *
     * @return int
     */
    public function getPageId()
    {
        return parent::getData(self::PAGE_ID);
    }

    /**
     * Set ID
     *
     * @param int $id
     * @return \Magento\Cms\Api\Data\PageInterface
     */
    public function setPageId($id)
    {
        return $this->setData(self::PAGE_ID, $id);
    }

And add following lines into vendor/magento/module-cms/Model/Page.php:

    /**
     * Retrieve block id
     *
     * @return int
     */
    public function getBlockId()
    {
        return $this->getData(self::BLOCK_ID);
    }

    /**
     * Set ID
     *
     * @param int $id
     * @return BlockInterface
     */
    public function setBlockId($id)
    {
        return $this->setData(self::BLOCK_ID, $id);
    }

And at the end just compile DI from command line:

php bin/magento setup:di:compile

 

If this response was helpful to you, consider giving kudos to this post.
If this response solved your problem, click accept as solution to help others solve this issue

View solution in original post

2 REPLIES 2

Re: REST call to cmsPage/search does not return page id's

It's not possible at the moment because of the way how Magento2 maps data objects and interfaces. In order to fix it, you'll have to add following lines into vendor/magento/module-cms/Model/Page.php:

    /**
     * Get ID
     *
     * @return int
     */
    public function getPageId()
    {
        return parent::getData(self::PAGE_ID);
    }

    /**
     * Set ID
     *
     * @param int $id
     * @return \Magento\Cms\Api\Data\PageInterface
     */
    public function setPageId($id)
    {
        return $this->setData(self::PAGE_ID, $id);
    }

And add following lines into vendor/magento/module-cms/Model/Page.php:

    /**
     * Retrieve block id
     *
     * @return int
     */
    public function getBlockId()
    {
        return $this->getData(self::BLOCK_ID);
    }

    /**
     * Set ID
     *
     * @param int $id
     * @return BlockInterface
     */
    public function setBlockId($id)
    {
        return $this->setData(self::BLOCK_ID, $id);
    }

And at the end just compile DI from command line:

php bin/magento setup:di:compile

 

If this response was helpful to you, consider giving kudos to this post.
If this response solved your problem, click accept as solution to help others solve this issue

Re: REST call to cmsPage/search does not return page id's

Thank you for the great feedback. Accepted this as solution.

 

Considering the importance of the PageID field to the API, I am however a little surprised that this simple fix has not been picked up by the dev-team yet.