I am trying to add category selector in my block page without UI component.
This is my block file.
<?php
namespace UedakanamonoBannerSliderBlockAdminhtmlBannerEditTab;
use MagentoBackendBlockTemplateContext;
use MagentoBackendBlockWidgetButton;
use MagentoBackendBlockWidgetFormElementDependence;
use MagentoBackendBlockWidgetFormGeneric;
use MagentoBackendBlockWidgetTabTabInterface;
use MagentoCmsModelWysiwygConfig as WysiwygConfig;
use MagentoConfigModelConfigSourceEnabledisable;
use MagentoConfigModelConfigStructureElementDependencyFieldFactory;
use MagentoFrameworkConvertDataObject;
use MagentoFrameworkDataFormFactory;
use MagentoFrameworkExceptionLocalizedException;
use MagentoFrameworkRegistry;
use UedakanamonoBannerSliderBlockAdminhtmlBannerEditTabRenderImage as BannerImage;
use UedakanamonoBannerSliderBlockAdminhtmlBannerEditTabRenderSlider;
use UedakanamonoBannerSliderBlockAdminhtmlBannerChooser;
use UedakanamonoBannerSliderHelperData;
use UedakanamonoBannerSliderHelperImage as HelperImage;
use UedakanamonoBannerSliderModelConfigSourceTemplate;
use UedakanamonoBannerSliderModelConfigSourceType;
use MagentoFrameworkStdlibDateTime;
/**
* Class Banner
* @package UedakanamonoBannerSliderBlockAdminhtmlBannerEditTab
*/
class Banner extends Generic implements TabInterface
{
/**
* Type options
*
* @var Type
*/
protected $typeOptions;
/**
* Template options
*
* @var Template
*/
protected $template;
/**
* Status options
*
* @var Enabledisable
*/
protected $statusOptions;
/**
* @var HelperImage
*/
protected $imageHelper;
/**
* @var FieldFactory
*/
protected $_fieldFactory;
/**
* @var DataObject
*/
protected $_objectConverter;
/**
* @var WysiwygConfig
*/
protected $_wysiwygConfig;
/**
* Banner constructor.
*
* @param Type $typeOptions
* @param Template $template
* @param Enabledisable $statusOptions
* @param Context $context
* @param Registry $registry
* @param FormFactory $formFactory
* @param HelperImage $imageHelper
* @param FieldFactory $fieldFactory
* @param DataObject $objectConverter
* @param WysiwygConfig $wysiwygConfig
* @param array $data
*/
public function __construct(
Type $typeOptions,
Template $template,
Enabledisable $statusOptions,
Context $context,
Registry $registry,
FormFactory $formFactory,
HelperImage $imageHelper,
FieldFactory $fieldFactory,
DataObject $objectConverter,
WysiwygConfig $wysiwygConfig,
array $data = []
) {
$this->typeOptions = $typeOptions;
$this->template = $template;
$this->statusOptions = $statusOptions;
$this->imageHelper = $imageHelper;
$this->_fieldFactory = $fieldFactory;
$this->_objectConverter = $objectConverter;
$this->_wysiwygConfig = $wysiwygConfig;
parent::__construct($context, $registry, $formFactory, $data);
}
/**
* Prepare title for tab
*
* @return string
*/
public function getTabTitle()
{
return $this->getTabLabel();
}
/**
* Prepare label for tab
*
* @return string
*/
public function getTabLabel()
{
return __('General');
}
/**
* Can show tab in tabs
*
* @return boolean
*/
public function canShowTab()
{
return true;
}
/**
* Tab is hidden
*
* @return boolean
*/
public function isHidden()
{
return false;
}
/**
* @return Generic
* @throws LocalizedException
*/
protected function _prepareForm()
{
/** @var UedakanamonoBannerSliderModelBanner $banner */
$banner = $this->_coreRegistry->registry('mpbannerslider_banner');
$form = $this->_formFactory->create();
$form->setHtmlIdPrefix('banner_');
$form->setFieldNameSuffix('banner');
$fieldset = $form->addFieldset('base_fieldset', [
'legend' => __('Banner Information'),
'class' => 'fieldset-wide'
]);
if ($banner->getId()) {
$fieldset->addField(
'banner_id',
'hidden',
['name' => 'banner_id']
);
}
$fieldset->addField('name', 'text', [
'name' => 'name',
'label' => __('Banner Title'),
'title' => __('Banner Title'),
'required' => true,
]);
$fieldset->addField('status', 'select', [
'name' => 'status',
'label' => __('Status'),
'title' => __('Status'),
'values' => $this->statusOptions->toOptionArray(),
]);
$fieldset->addField(
'category_ids',
'UedakanamonoBannerSliderBlockAdminhtmlBannerChooser',
[
'name' => 'category_ids',
'label' => __('Categories'),
'title' => __('Categories'),
'required' => true
]
);
$uploadBanner = $fieldset->addField('image', BannerImage::class, [
'name' => 'image',
'label' => __('Upload Image'),
'title' => __('Upload Image'),
'path' => $this->imageHelper->getBaseMediaPath(HelperImage::TEMPLATE_MEDIA_TYPE_BANNER)
]);
$titleBanner = $fieldset->addField('title', 'text', [
'name' => 'title',
'label' => __('Banner SubTitle'),
'title' => __('Banner SubTitle'),
]);
$urlBanner = $fieldset->addField('url_banner', 'text', [
'name' => 'url_banner',
'label' => __('Url'),
'title' => __('Url'),
'class' => 'validate-url validate-no-html-tags'
]);
$newTab = $fieldset->addField('newtab', 'select', [
'name' => 'newtab',
'label' => __('Open new tab after click'),
'title' => __('Open new tab after click'),
'values' => $this->statusOptions->toOptionArray(),
'note' => __('Automatically open new tab after clicking on the banner')
]);
$orderBanner = $fieldset->addField('vieworder', 'text', [
'name' => 'vieworder',
'label' => __('Display Order'),
'title' => __('Display Order'),
]);
$fieldset->addField('banner_from_date', 'date', [
'name' => 'banner_from_date',
'label' => __('Banner Start Date'),
'title' => __('Banner Start Date'),
'date_format' => 'yyyy/MM/dd',
'input_format' => DateTime::DATE_INTERNAL_FORMAT,
'timezone' => false
]);
$fieldset->addField('banner_to_date', 'date', [
'name' => 'banner_to_date',
'label' => __('Banner End Date'),
'title' => __('Banner End Date'),
'date_format' => 'yyyy/MM/dd',
'input_format' => DateTime::DATE_INTERNAL_FORMAT,
'timezone' => false
]);
$fieldset->addField('sliders_ids', Slider::class, [
'name' => 'sliders_ids',
'label' => __('Sliders'),
'title' => __('Sliders'),
]);
if (!$banner->getSlidersIds()) {
$banner->setSlidersIds($banner->getSliderIds());
}
$bannerData = $this->_session->getData('mpbannerslider_banner_data', true);
if ($bannerData) {
$banner->addData($bannerData);
} else {
if (!$banner->getId()) {
$banner->addData($banner->getDefaultValues());
}
}
$form->addValues($banner->getData());
$this->setForm($form);
return parent::_prepareForm();
}
}
As you can see, I add fieldset name of category_ids.
And then this is my chooser.php
<?php
namespace UedakanamonoBannerSliderBlockAdminhtmlBanner;
use MagentoCatalogModelCategory as CategoryModel;
use MagentoFrameworkAuthorizationInterface;
use MagentoFrameworkDataFormElementCollectionFactory;
use MagentoFrameworkDataFormElementFactory;
use MagentoFrameworkDataFormElementMultiselect;
use MagentoFrameworkEscaper;
use MagentoFrameworkUrlInterface;
class Chooser extends Multiselect
{
public $collectionFactory;
public $authorization;
protected $_urlBuilder;
public function __construct(
Factory $factoryElement,
CollectionFactory $factoryCollection,
Escaper $escaper,
MagentoCatalogModelResourceModelCategoryCollectionFactory $collectionFactory,
AuthorizationInterface $authorization,
UrlInterface $urlBuilder,
array $data = []
) {
$this->collectionFactory = $collectionFactory;
$this->authorization = $authorization;
$this->_urlBuilder = $urlBuilder;
parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
}
public function getElementHtml()
{
$html = '<div class="admin__field-control admin__control-grouped">';
$html .= '<div id="topbanner-category-select" class="admin__field" data-bind="scope:'topbannerCategory'" data-index="index">';
$html .= '<!-- ko foreach: elems() -->';
$html .= '<input name="category_ids" data-bind="value: value" style="display: none"/>';
$html .= '<!-- ko template: elementTmpl --><!-- /ko -->';
$html .= '<!-- /ko -->';
$html .= '</div></div>';
$html .= $this->getAfterElementHtml();
return $html;
}
public function getCategoriesTree()
{
/* @var $collection MagentoCatalogModelResourceModelCategoryCollection */
$collection = $this->collectionFactory->create()->addAttributeToSelect('name')->addAttributeToSort('position','asc');
$categoryById = [
CategoryModel::TREE_ROOT_ID => [
'value' => CategoryModel::TREE_ROOT_ID,
'optgroup' => null,
],
];
foreach ($collection as $category) {
foreach ([$category->getId(), $category->getParentId()] as $categoryId) {
if (!isset($categoryById[$categoryId])) {
$categoryById[$categoryId] = ['value' => $categoryId];
}
}
$categoryById[$category->getId()]['is_active'] = 1;
$categoryById[$category->getId()]['label'] = $category->getName();
$categoryById[$category->getParentId()]['optgroup'][] = &$categoryById[$category->getId()];
}
return $categoryById[CategoryModel::TREE_ROOT_ID]['optgroup'];
}
public function getValues()
{
$values = $this->getValue();
if (!is_array($values)) {
$values = explode(',', $values);
}
if (!sizeof($values)) {
return [];
}
$collection = $this->collectionFactory->create()
->addIdFilter($values);
$options = [];
foreach ($collection as $category) {
$options[] = $category->getId();
}
return $options;
}
public function getAfterElementHtml()
{
$html = '<script type="text/x-magento-init">
{
"*": {
"Magento_Ui/js/core/app": {
"components": {
"topbannerCategory": {
"component": "uiComponent",
"children": {
"select_category": {
"component": "Vendor_Module/js/components/new-category",
"config": {
"filterOptions": true,
"disableLabel": true,
"chipsEnabled": true,
"levelsVisibility": "1",
"elementTmpl": "ui/grid/filters/elements/ui-select",
"options": ' . json_encode($this->getCategoriesTree()) . ',
"value": ' . json_encode($this->getValues()) . ',
"listens": {
"index=create_category:responseData": "setParsed",
"newOption": "toggleOptionSelected"
},
"config": {
"dataScope": "select_category",
"sortOrder": 10
}
}
}
}
}
}
}
}
}
</script>';
return $html;
}
}
After run, error is occured.
[2023-07-03T09:42:33.892733+00:00] main.CRITICAL: Exception: Deprecated Functionality: explode(): Passing null to parameter #2 ($string) of type string is deprecated in F:xampphtdocsmagento2appcodeUedakanamonoBannerSliderBlockAdminhtmlBannerChooser.php on line 83 in F:xampphtdocsmagento2libinternalMagentoFrameworkAppErrorHandler.php:62
Stack trace:
#0 [internal function]: MagentoFrameworkAppErrorHandler->handler(8192, 'explode(): Pass...', 'F:\xampp\htdocs...', 83)
#1 F:xampphtdocsmagento2appcodeUedakanamonoBannerSliderBlockAdminhtmlBannerChooser.php(83): explode(',', NULL)
#2 F:xampphtdocsmagento2appcodeUedakanamonoBannerSliderBlockAdminhtmlBannerChooser.php(120): UedakanamonoBannerSliderBlockAdminhtmlBannerChooser->getValues()
#3 F:xampphtdocsmagento2appcodeMagentoBackendviewadminhtmltemplateswidgetformrendererfieldsetelement.phtml(12): UedakanamonoBannerSliderBlockAdminhtmlBannerChooser->getAfterElementHtml()
#4 F:xampphtdocsmagento2libinternalMagentoFrameworkViewTemplateEnginePhp.php(71): include('F:\xampp\htdocs...')
#5 F:xampphtdocsmagento2libinternalMagentoFrameworkViewElementTemplate.php(263): MagentoFrameworkViewTemplateEnginePhp->render(Object(MagentoBackendBlockWidgetFormRendererFieldsetElement), 'F:/xampp/htdocs...', Array)
#6 F:xampphtdocsmagento2libinternalMagentoFrameworkViewElementTemplate.php(293): MagentoFrameworkViewElementTemplate->fetchView('F:/xampp/htdocs...')
#7 F:xampphtdocsmagento2appcodeMagentoBackendBlockTemplate.php(141): MagentoFrameworkViewElementTemplate->_toHtml()
#8 F:xampphtdocsmagento2libinternalMagentoFrameworkViewElementAbstractBlock.php(1095): MagentoBackendBlockTemplate->_toHtml()
#9 F:xampphtdocsmagento2libinternalMagentoFrameworkViewElementAbstractBlock.php(1099): MagentoFrameworkViewElementAbstractBlock->MagentoFrameworkViewElement{closure}()
#10 F:xampphtdocsmagento2libinternalMagentoFrameworkViewElementAbstractBlock.php(660): MagentoFrameworkViewElementAbstractBlock->_loadCache()
#11 F:xampphtdocsmagento2appcodeMagentoBackendBlockWidgetFormRendererFieldsetElement.php(46): MagentoFrameworkViewElementAbstractBlock->toHtml()
#12 F:xampphtdocsmagento2libinternalMagentoFrameworkDataFormElementAbstractElement.php(541): MagentoBackendBlockWidgetFormRendererFieldsetElement->render(Object(UedakanamonoBannerSliderBlockAdminhtmlBannerChooser))
#13 F:xampphtdocsmagento2libinternalMagentoFrameworkDataFormElementAbstractElement.php(555): MagentoFrameworkDataFormElementAbstractElement->getHtml()
#14 F:xampphtdocsmagento2libinternalMagentoFrameworkDataFormElementFieldset.php(232): MagentoFrameworkDataFormElementAbstractElement->toHtml()
#15 F:xampphtdocsmagento2libinternalMagentoFrameworkDataFormElementFieldset.php(109): MagentoFrameworkDataFormElementFieldset->_elementsToHtml(Array)
#16 F:xampphtdocsmagento2generatedcodeMagentoFrameworkDataFormElementFieldsetInterceptor.php(59): MagentoFrameworkDataFormElementFieldset->getBasicChildrenHtml()
#17 F:xampphtdocsmagento2appcodeMagentoBackendviewadminhtmltemplateswidgetformrendererfieldset.phtml(84): MagentoFrameworkDataFormElementFieldsetInterceptor->getBasicChildrenHtml()
#18 F:xampphtdocsmagento2libinternalMagentoFrameworkViewTemplateEnginePhp.php(71): include('F:\xampp\htdocs...')
#19 F:xampphtdocsmagento2libinternalMagentoFrameworkViewElementTemplate.php(263): MagentoFrameworkViewTemplateEnginePhp->render(Object(MagentoBackendBlockWidgetFormRendererFieldset), 'F:/xampp/htdocs...', Array)
#20 F:xampphtdocsmagento2libinternalMagentoFrameworkViewElementTemplate.php(293): MagentoFrameworkViewElementTemplate->fetchView('F:/xampp/htdocs...')
#21 F:xampphtdocsmagento2appcodeMagentoBackendBlockTemplate.php(141): MagentoFrameworkViewElementTemplate->_toHtml()
#22 F:xampphtdocsmagento2libinternalMagentoFrameworkViewElementAbstractBlock.php(1095): MagentoBackendBlockTemplate->_toHtml()
#23 F:xampphtdocsmagento2libinternalMagentoFrameworkViewElementAbstractBlock.php(1099): MagentoFrameworkViewElementAbstractBlock->MagentoFrameworkViewElement{closure}()
#24 F:xampphtdocsmagento2libinternalMagentoFrameworkViewElementAbstractBlock.php(660): MagentoFrameworkViewElementAbstractBlock->_loadCache()
#25 F:xampphtdocsmagento2appcodeMagentoBackendBlockWidgetFormRendererFieldset.php(50): MagentoFrameworkViewElementAbstractBlock->toHtml()
#26 F:xampphtdocsmagento2libinternalMagentoFrameworkDataFormElementAbstractElement.php(541): MagentoBackendBlockWidgetFormRendererFieldset->render(Object(MagentoFrameworkDataFormElementFieldsetInterceptor))
#27 F:xampphtdocsmagento2generatedcodeMagentoFrameworkDataFormElementFieldsetInterceptor.php(338): MagentoFrameworkDataFormElementAbstractElement->getHtml()
#28 F:xampphtdocsmagento2libinternalMagentoFrameworkDataFormElementAbstractElement.php(555): MagentoFrameworkDataFormElementFieldsetInterceptor->getHtml()
#29 F:xampphtdocsmagento2generatedcodeMagentoFrameworkDataFormElementFieldsetInterceptor.php(347): MagentoFrameworkDataFormElementAbstractElement->toHtml()
#30 F:xampphtdocsmagento2libinternalMagentoFrameworkDataForm.php(373): MagentoFrameworkDataFormElementFieldsetInterceptor->toHtml()
#31 F:xampphtdocsmagento2libinternalMagentoFrameworkDataForm.php(390): MagentoFrameworkDataForm->toHtml()
#32 F:xampphtdocsmagento2appcodeMagentoBackendBlockWidgetForm.php(113): MagentoFrameworkDataForm->getHtml()
#33 F:xampphtdocsmagento2generatedcodeUedakanamonoBannerSliderBlockAdminhtmlBannerEditTabBannerInterceptor.php(68): MagentoBackendBlockWidgetForm->getFormHtml()
#34 F:xampphtdocsmagento2appcodeMagentoBackendviewadminhtmltemplateswidgetform.phtml(13): UedakanamonoBannerSliderBlockAdminhtmlBannerEditTabBannerInterceptor->getFormHtml()
#35 F:xampphtdocsmagento2libinternalMagentoFrameworkViewTemplateEnginePhp.php(71): include('F:\xampp\htdocs...')
#36 F:xampphtdocsmagento2libinternalMagentoFrameworkViewElementTemplate.php(263): MagentoFrameworkViewTemplateEnginePhp->render(Object(UedakanamonoBannerSliderBlockAdminhtmlBannerEditTabBannerInterceptor), 'F:/xampp/htdocs...', Array)
#37 F:xampphtdocsmagento2generatedcodeUedakanamonoBannerSliderBlockAdminhtmlBannerEditTabBannerInterceptor.php(221): MagentoFrameworkViewElementTemplate->fetchView('F:/xampp/htdocs...')
#38 F:xampphtdocsmagento2libinternalMagentoFrameworkViewElementTemplate.php(293): UedakanamonoBannerSliderBlockAdminhtmlBannerEditTabBannerInterceptor->fetchView('F:/xampp/htdocs...')
#39 F:xampphtdocsmagento2appcodeMagentoBackendBlockTemplate.php(141): MagentoFrameworkViewElementTemplate->_toHtml()
#40 F:xampphtdocsmagento2libinternalMagentoFrameworkViewElementAbstractBlock.php(1095): MagentoBackendBlockTemplate->_toHtml()
#41 F:xampphtdocsmagento2libinternalMagentoFrameworkViewElementAbstractBlock.php(1099): MagentoFrameworkViewElementAbstractBlock->MagentoFrameworkViewElement{closure}()
#42 F:xampphtdocsmagento2libinternalMagentoFrameworkViewElementAbstractBlock.php(660): MagentoFrameworkViewElementAbstractBlock->_loadCache()
#43 F:xampphtdocsmagento2generatedcodeUedakanamonoBannerSliderBlockAdminhtmlBannerEditTabBannerInterceptor.php(446): MagentoFrameworkViewElementAbstractBlock->toHtml()
#44 F:xampphtdocsmagento2appcodeMagentoBackendBlockWidgetTabs.php(492): UedakanamonoBannerSliderBlockAdminhtmlBannerEditTabBannerInterceptor->toHtml()
#45 F:xampphtdocsmagento2appcodeMagentoBackendviewadminhtmltemplateswidgettabs.phtml(73): MagentoBackendBlockWidgetTabs->getTabContent(Object(UedakanamonoBannerSliderBlockAdminhtmlBannerEditTabBannerInterceptor))
#46 F:xampphtdocsmagento2libinternalMagentoFrameworkViewTemplateEnginePhp.php(71): include('F:\xampp\htdocs...')
#47 F:xampphtdocsmagento2libinternalMagentoFrameworkViewElementTemplate.php(263): MagentoFrameworkViewTemplateEnginePhp->render(Object(UedakanamonoBannerSliderBlockAdminhtmlBannerEditTabs), 'F:/xampp/htdocs...', Array)
#48 F:xampphtdocsmagento2libinternalMagentoFrameworkViewElementTemplate.php(293): MagentoFrameworkViewElementTemplate->fetchView('F:/xampp/htdocs...')
#49 F:xampphtdocsmagento2appcodeMagentoBackendBlockTemplate.php(141): MagentoFrameworkViewElementTemplate->_toHtml()
#50 F:xampphtdocsmagento2libinternalMagentoFrameworkViewElementAbstractBlock.php(1095): MagentoBackendBlockTemplate->_toHtml()
#51 F:xampphtdocsmagento2libinternalMagentoFrameworkViewElementAbstractBlock.php(1099): MagentoFrameworkViewElementAbstractBlock->MagentoFrameworkViewElement{closure}()
#52 F:xampphtdocsmagento2libinternalMagentoFrameworkViewElementAbstractBlock.php(660): MagentoFrameworkViewElementAbstractBlock->_loadCache()
#53 F:xampphtdocsmagento2libinternalMagentoFrameworkViewLayout.php(578): MagentoFrameworkViewElementAbstractBlock->toHtml()
#54 F:xampphtdocsmagento2libinternalMagentoFrameworkViewLayout.php(555): MagentoFrameworkViewLayout->_renderBlock('Uedakanamono_ba...')
#55 F:xampphtdocsmagento2generatedcodeMagentoFrameworkViewLayoutInterceptor.php(149): MagentoFrameworkViewLayout->renderNonCachedElement('Uedakanamono_ba...')
#56 F:xampphtdocsmagento2libinternalMagentoFrameworkViewLayout.php(510): MagentoFrameworkViewLayoutInterceptor->renderNonCachedElement('Uedakanamono_ba...')
#57 F:xampphtdocsmagento2generatedcodeMagentoFrameworkViewLayoutInterceptor.php(140): MagentoFrameworkViewLayout->renderElement('Uedakanamono_ba...', false)
#58 F:xampphtdocsmagento2libinternalMagentoFrameworkViewLayout.php(606): MagentoFrameworkViewLayoutInterceptor->renderElement('Uedakanamono_ba...', false)
#59 F:xampphtdocsmagento2libinternalMagentoFrameworkViewLayout.php(557): MagentoFrameworkViewLayout->_renderContainer('left', false)
#60 F:xampphtdocsmagento2generatedcodeMagentoFrameworkViewLayoutInterceptor.php(149): MagentoFrameworkViewLayout->renderNonCachedElement('left')
#61 F:xampphtdocsmagento2libinternalMagentoFrameworkViewLayout.php(510): MagentoFrameworkViewLayoutInterceptor->renderNonCachedElement('left')
#62 F:xampphtdocsmagento2generatedcodeMagentoFrameworkViewLayoutInterceptor.php(140): MagentoFrameworkViewLayout->renderElement('left', false)
#63 F:xampphtdocsmagento2libinternalMagentoFrameworkViewLayout.php(606): MagentoFrameworkViewLayoutInterceptor->renderElement('left', false)
#64 F:xampphtdocsmagento2libinternalMagentoFrameworkViewLayout.php(557): MagentoFrameworkViewLayout->_renderContainer('side.col', false)
#65 F:xampphtdocsmagento2generatedcodeMagentoFrameworkViewLayoutInterceptor.php(149): MagentoFrameworkViewLayout->renderNonCachedElement('side.col')
#66 F:xampphtdocsmagento2libinternalMagentoFrameworkViewLayout.php(510): MagentoFrameworkViewLayoutInterceptor->renderNonCachedElement('side.col')
#67 F:xampphtdocsmagento2generatedcodeMagentoFrameworkViewLayoutInterceptor.php(140): MagentoFrameworkViewLayout->renderElement('side.col', false)
....
#102 {main} [] []
Who can answer this issue?