0

I am attempting to load a CMS Page from my controller. The community here was kind enough to help me with a CMS Static Block - and now I need a pointer in the right direction for a cms Page.

What is the proper syntax to load a CMS Page by identifier or block id and what factory supports this type of loading?

4

Found the solution. Documenting for posterity.

...
use Magento\Cms\Model\PageFactory;

protected $pageFactory;

public function __construct(...,PageFactory $pageFactory,...) {
...
$this->pageFactory = $pageFactory;
...
}

public function loadCMSPage() {
...
$page = $this->pageFactory->create()->load(<PAGE URL OR ID HERE>);
...
}
5
  • 1
    Please change $pagefactory to $pageFactory in construct argument, it will not work in Linux server. – Niranjan Gondaliya Oct 23 '18 at 7:53
  • 1
    @Kevin Hill Awesome. How can I page information per store view I've tried $this->_pageFactory->create()->setStoreId(1)->load(51) but it doesn't work? – Juliano Vargas Mar 1 '19 at 16:38
  • @Juliano Vargas Have you find the solution to load cms page by store id? I am also looking for the same thing.Kindly share your experience – aravind May 9 '19 at 11:17
  • @aravind I haven't worked on it yet. But I have something that I would like to try: Which is create a link between the cms pages E.G : I have 3 store view therefore I need to create 3 help.html page one for each store then add new field in the admin cms form to display all the cms pages from the main store that way I can then select help.html in store 2 and 3 so they are linked when I hit the url:../help.html I get id fetch new column that has link_id and load all cms data by that column if that make sense it's just a theory but idea is to link pages with one another. – Juliano Vargas May 9 '19 at 13:28
  • @Juliano Vargas thanks for your idea.But i have managed to solve the issue by adding filter to a function which extends searchbuilder. It gets me only the stores where the cms page is active – aravind May 9 '19 at 13:40
0

There is a special model for this: GetPageByIdentifierInterface
It is available since 2.3

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.