How can I retrieve a list of available pages such as Contact Us, About Us, Home etc... in Magento 2, my purpose to get a list of pages is to build a custom menu on left side bar. Also is there any way to build a custom menu for selected pages in Magento 2?
-
1In Helper <?php namespace Vendor\Module\Helper; class Data extends \Magento\Framework\App\Helper\AbstractHelper { protected $_cmsPages; public function __construct(\Magento\Cms\Model\ResourceModel\Page\Collection $cmsPages) { $this->_cmsPages = $cmsPages; } public function getCMSPages(){ return $this->_cmsPages; } } In phtml template file <?php foreach($this->helper('Vendor\Module\Helper\Data')->getCMSPages() as $cms_page) { echo $cms_page->getTitle()."<br/>"; } ?> – Mike May 21 '16 at 3:55
For your information, even though the answers posted look like valid answer, it is recommended to use service contracts in Magento 2.
In your case a valid code would be to first inject the right service contract class in your constructor:
protected $_pageRepositoryInterface;
public function __construct(
...
\Magento\Cms\Api\PageRepositoryInterface $pageRepositoryInterface
) {
$this->_pageRepositoryInterface = $pageRepositoryInterface;
...
}
Then you can do:
$this->_pageRepositoryInterface->getList($searchCriteria);
Where $searchCriteria
is an instance of \Magento\Framework\Api\SearchCriteriaInterface
-
As far as I can see the getList method returns an array of data, not a page object with its ID right? – koosa Sep 15 '16 at 6:57
-
Yep, that data array returned by PageRepository bizarrely does not include the object id itself... – koosa Sep 15 '16 at 7:10
-
@koosa I updated my answer because the service contract class was wrong. You should be able to call
getItems
on the result of thegetList()
method – Raphael at Digital Pianism Sep 15 '16 at 7:38 -
1@RaphaelatDigitalPianism I am trying to do a similar thing with blocks however it is returning an array of arrays instead of an array of objects when I use
Magento\Cms\Api\BlockRepositoryInterface
. Using M2.1.1. – Raj Sep 22 '16 at 7:31 -
2
you can get it from object Manager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$collection = $objectManager->get('\Magento\Cms\Model\ResourceModel\Page\CollectionFactory')->create();
// add Filter if you want
$collection->addFieldToFilter('is_active' , \Magento\Cms\Model\Page::STATUS_ENABLED);
$pages = array('default' => '(Default) Customer Login Page');
foreach($collection as $page){
$pages[$page->getId()] = $page->getTitle();
}
This code will fetch you all enabled CMS pages:
public function __construct(
\Magento\Cms\Api\PageRepositoryInterface $pageRepository,
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->_cmsPage = $pageRepository;
$this->_search = $searchCriteriaBuilder;
}
public function toOptionArray()
{
$pages = [];
foreach($this->_cmsPage->getList($this->_getSearchCriteria())->getItems() as $page) {
$pages[] = [
'value' => $page->getIdentifier(),
'label' => $page->getTitle()
];
}
return $pages;
}
protected function _getSearchCriteria()
{
return $this->_search->addFilter('is_active', '1')->create();
}
Here is example :
#File: app/code/Pulsestorm/TutorialInstanceObjects/Command/Testbed.php
public function __construct(
\Magento\Cms\Model\PageFactory $pageFactory =
)
{
$this->pageFactory = $pageFactory;
return parent::__construct();
}
//...
public function execute(InputInterface $input, OutputInterface $output)
{
$page = $this->pageFactory->create();
foreach($page->getCollection() as $item)
{
$output->writeln($item->getId() . '::' . $item->getTitle());
}
$page = $this->pageFactory->create()->load(1);
var_dump($page->getData());
}
-
Thank you for your help, I got it working with help of your example and few customization to my code. – nuwaus May 14 '16 at 6:04
You could use the method getList() from vendor/magento/module-cms/Model/PageRepository.php class.
You have to pass a SearchCriteria Object, here there is some example of its usage: Filtering a Magento 2 Object Repository
So pass PageRepository in the constructor of your block or create object with ObjectManager or pass it with registry from a controller and make a try.
I haven't try this solution, but it should be work.