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?
2 Answers 2
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>);
...
}
-
1Please change $pagefactory to $pageFactory in construct argument, it will not work in Linux server.Niranjan Gondaliya– Niranjan Gondaliya2018年10月23日 07:53:33 +00:00Commented Oct 23, 2018 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– Juliano Vargas2019年03月01日 16:38:11 +00:00Commented Mar 1, 2019 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 experiencearavind– aravind2019年05月09日 11:17:04 +00:00Commented May 9, 2019 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.htmlpage 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.htmlI get id fetch new column that haslink_idand 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– Juliano Vargas2019年05月09日 13:28:16 +00:00Commented May 9, 2019 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 activearavind– aravind2019年05月09日 13:40:21 +00:00Commented May 9, 2019 at 13:40
There is a special model for this: GetPageByIdentifierInterface
It is available since 2.3