Registering Custom Blocks for Magento 2 Page Builder
Magento’s Page Builder is an incredibly powerful tool that allows you to create dynamic content blocks without touching any code. However, to use your custom blocks in the Page Builder interface, you need to register them correctly within your module.
In your case, you’ve created a custom block Display.php under app/code/Goteam/HelloWorld/Block, and registered it via registration.php. While this enables you to use the block within your custom theme’s layout XML, it doesn’t make the block visible in the Page Builder’s component selection menu.
Understanding Magento 2’s Block Registration for Page Builder
To appear in the Page Builder interface, a block needs to be registered with the Page Builder Component Manager. This is done via a separate register function that hooks into the pagebuilder_cms_block_types event.
Here’s how you can modify your module to achieve this:
Step 1: Add a Dependency to the PageBuilder
First, ensure your module has a dependency on the Magento Page Builder module by adding it to your composer.json:
"require": {
...
"magento/page-builder": "2.4.3",
...
}
Step 2: Create an Event Observer for Block Registration
Create a new file called Goteam/HelloWorld/etc/frontend/di.xml with the following content:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager etc:config.xsd">
<type name="Magento\PageBuilder\Model\Component\ContainerProvider">
<arguments>
<argument name="componentTypes" xsi:type="array">
<item name="goteam-helloworld" xsi:type="string">Goteam\HelloWorld\Block\Component\Display</item>
</argument>
</arguments>
</type>
</config>
In this file, we’re adding an observer for the pagebuilder_cms_block_types event. We’re defining our custom block as a new component type called goteam-helloworld.
Step 3: Create Your Custom Component
Create a new file Goteam/HelloWorld/Block/Component/Display.php:
namespace Goteam\HelloWorld\Block\Component;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
class Display extends Template implements \Magento\PageBuilder\Model\ComponentInterface
{
public function __construct(Context $context)
{
parent::__construct($context);
}
public function getComponentName()
{
return 'goteam-helloworld';
}
public function sayHello()
{
return __('Hello World');
}
}
In this file, we’re creating a new component that implements the Magento\PageBuilder\Model\ComponentInterface. We need to provide the unique component name (getComponentName) and any other functionality required for your block.
Step 4: Enable Page Builder
Ensure that the Page Builder is enabled in your Magento admin. This can be done under Content > Configuration > Page Builder Configuration.
Conclusion
With these steps, you should now see your custom block available in the Page Builder component selection menu. Remember to clear your cache after making any changes.
This approach allows you to leverage the full power of Magento’s Page Builder while still maintaining a clean separation between your business logic and content creation.
For more detailed guides on Magento 2 development, I recommend checking out LaravelCompany’s blog for in-depth tutorials and best practices.