Pretty much what the title says. I want to add the google map in a cms block and then call it in a form.phtml
for Contact Page. How do I do it?
Pretty much what the title says. I want to add the google map in a cms block and then call it in a form.phtml
for Contact Page. How do I do it?
2
Answers
To render an enabled CMS block in a phtml file in Magento 2, you can use the following code:
Here’s a breakdown of the code:
Define the identifier of the CMS block you want to render. This can be found in the CMS block list in the Magento admin panel.
Create an instance of the CMS block using the ObjectManager
Check if the block is active (enabled)
If the block is active, render the block’s HTML using the toHtml() method.
Note that using the ObjectManager directly is not best practice, and it is recommended to use dependency injection instead. Additionally, you should make sure to sanitize the $blockId input to avoid security vulnerabilities.
Override form.phtml file in your child theme and then Call your static block in that override form.phtml file: