The class \Magento\Backend\Block\Widget\Form
has a protected _getAdditionalElementTypes
method:
A descendant can override this method to support displaying custom controls.
The possibility is used by administrative product and categories screens.
\Magento\Catalog\Block\Adminhtml\Product\Edit\Tab\Attributes::_getAdditionalElementTypes()
:
As you can seem the administrative product form adds custom form element types: price
, weight
, gallery
, image
, boolean
, textarea
.
Then it fires the event adminhtml_catalog_product_edit_element_types
:
A custom extension can handle the event and add its custom controls (form element types):
<?xml version='1.0'?>
<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='../../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd'>
<event name='adminhtml_catalog_product_edit_element_types'>
<observer
name='Dfe\Markdown\Observer\ElementTypes'
instance='Dfe\Markdown\Observer\ElementTypes'
/>
</event>
</config>
<?php
namespace Dfe\Markdown\Observer;
use Magento\Framework\Event\ObserverInterface;
class ElementTypes implements ObserverInterface {
/**
* @override
* @see ObserverInterface::execute()
* @used-by \Magento\Framework\Event\Invoker\InvokerDefault::_callObserverMethod()
* @see \Magento\Catalog\Block\Adminhtml\Product\Edit\Tab\Attributes::_getAdditionalElementTypes()
* https://github.com/magento/magento2/blob/0be91a56d050791ac0b67a47185d79df31e79329/app/code/Magento/Catalog/Block/Adminhtml/Product/Edit/Tab/Attributes.php#L151
$response = new \Magento\Framework\DataObject();
$response->setTypes([]);
$this->_eventManager->dispatch(
'adminhtml_catalog_product_edit_element_types', ['response' => $response]
);
* https://3v4l.org/UidhW
* @param \Magento\Framework\Event\Observer $o
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $o) {
if (\Dfe\Markdown\Settings::s()->enable()) {
$o['response']['types'] = ['textarea' => 'Dfe\Markdown\FormElement'];
}
}
}
It works for administrative product page but there is a problem for administrative category page: the corresponding category edit form does not fire any events:
https://github.com/magento/magento2/issues/2165
While it is not fixed in the core I use a workaround solution: override the admin category form class and fire the event my myself:
<?xml version='1.0'?>
<config
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:noNamespaceSchemaLocation='../../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd'
>
<preference
for='Magento\Catalog\Block\Adminhtml\Category\Tab\Attributes'
type='Df\Catalog\Block\Adminhtml\Category\Tab\Attributes'
/>
</config>
<?php
namespace Df\Catalog\Block\Adminhtml\Category\Tab;
class Attributes extends \Magento\Catalog\Block\Adminhtml\Category\Tab\Attributes {
/**
* @override
* @see \Magento\Catalog\Block\Adminhtml\Category\Tab\Attributes::_getAdditionalElementTypes()
* @return string[]
*/
protected function _getAdditionalElementTypes() {
/** @var string[] $result */
$result = parent::_getAdditionalElementTypes();
$response = new \Magento\Framework\DataObject();
$response['types'] = [];
$this->_eventManager->dispatch(
'adminhtml_catalog_category_edit_element_types', ['response' => $response]
);
foreach ($response['types'] as $typeName => $typeClass) {
/** @var string $typeName */
/** @var string $typeClass */
$result[$typeName] = $typeClass;
}
return $result;
}
}
<?xml version='1.0'?>
<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='../../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd'>
<event name='adminhtml_catalog_category_edit_element_types'>
<observer
name='Dfe\Markdown\Observer\ElementTypes'
instance='Dfe\Markdown\Observer\ElementTypes'
/>
</event>
</config>