Skip to content

MagentoCatalogBlockAdminhtmlProductWidgetChooser is not working , button is not appearing

this is my code:

namespace vendormoduleBlockAdminhtmlProductEditTab;

use Exception;
use vendormoduleHelperData;
use vendormoduleModelResourceModelProductSourceAllProductsFormodule;
use MagentoBackendBlockTemplateContext;
use MagentoBackendBlockWidgetFormGeneric;
use MagentoBackendBlockWidgetTabTabInterface;
use MagentoCatalogModelProductFactory;
use MagentoFrameworkDataFormFactory;
use MagentoFrameworkRegistry;
use MagentoFrameworkStdlibDateTimeTimezoneInterface;
use MagentoStoreModelSystemStore;

/**
 * Class Main
 * @package vendormoduleBlockAdminhtmlProductEditTab
 */
class Main extends Generic implements TabInterface
{
    /**
     * @var Store
     */
    protected $_systemStore;

    /**
     * @var ProductFactory
     */
    protected $_productloader;
    /**
     * @var AllProductsFormodule
     */
    protected $_allProducts;
    /**
     * @var array configuration of module
     */

    protected $helper;
    /**
     * @var TimezoneInterface
     */
    private $timezone;

    /**
     * @param Context $context
     * @param Registry $registry
     * @param FormFactory $formFactory
     * @param Store $systemStore
     * @param ProductFactory $_productloader
     * @param AllProductsFormodule $allProducts
     * @param Data $helper
     * @param TimezoneInterface $timezone
     * @param array $data
     */
    public function __construct(
        Context $context,
        Registry $registry,
        FormFactory $formFactory,
        Store $systemStore,
        ProductFactory $_productloader,
        AllProductsFormodule $allProducts,
        Data $helper,
        TimezoneInterface $timezone,
        array $data = []
    ) {
        $this->helper = $helper;
        $this->_allProducts = $allProducts;
        $this->_systemStore = $systemStore;
        $this->_productloader = $_productloader;
        $this->timezone = $timezone;
        parent::__construct($context, $registry, $formFactory, $data);
    }


    /**
     * @return Main
     * @throws MagentoFrameworkExceptionLocalizedException
     * @throws Exception
     */
    protected function _prepareForm()
    {
        $model = $this->_coreRegistry->registry('vendormodule_product');
        $dateFormat = $this->_localeDate->getDateFormat(IntlDateFormatter::SHORT);

        $form = $this->_formFactory->create();
        //$form->setUseContainer(true);

        $fieldset = $form->addFieldset(
            'options_fieldset',
            ['legend' => __('Product Information'), 'class' => 'fieldset-wide fieldset-widget-options']
        );
        if ($model->getId()) {
            $fieldset->addField('module_entity_id', 'hidden', ['name' => 'module_entity_id']);
        }
        if (!$model->getProductId()) {
            $chooserField = $fieldset->addField(
                'options_fieldset_entity_id',
                'label',
                [
                    'name' => 'product_id',
                    'label' => __('Product'),
                    'required' => true,
                    'class' => 'widget-option',
                    'note' => __("Select a product"),
                ]
            );
            /*Add chooser helper for the field*/
            $helperData = [
                'data' => [
                    'button' => ['open' => __("Select Product...")]
                ]
            ];
            // $chooserField->setValue('product/' . $model->getProductId());
            // Fixes Require product.
            $chooserField->setValue($model->getProductId());
            $helperBlock = $this->getLayout()->createBlock(
                'MagentoCatalogBlockAdminhtmlProductWidgetChooser',
                '',
                $helperData
            );

            $helperBlock->setConfig($helperData)
                ->setFieldsetId($fieldset->getId())
                ->prepareElementHtml($chooserField)
                ->setValue($model->getId());
        } else {
            $fieldset->addField(
                'product_id',
                'note',
                [
                    'name' => 'product_id',
                    'label' => __('Product'),
                    'title' => __('Product'),
                    'text' => $this->helper->getProductbyId($model->getProductId())->getName()
                ]
            );
        }
        $fieldset->addField(
            'starting_price',
            'text',
            [
                'name' => 'starting_price',
                'label' => __('Starting Price'),
                'id' => 'starting_price',
                'title' => __('Starting Price'),
                'type' => 'price',
                'class' => 'required-entry validate-zero-or-greater',
                'required' => true
            ]
        );
        $fieldset->addField(
            'max_amount',
            'text',
            [
                'name' => 'max_amount',
                'label' => __('Max Price'),
                'id' => 'max_price',
                'title' => __('Max Price'),
                'type' => 'price',
                'class' => 'validate-zero-or-greater',
                'required' => false,
            ]
        );
        $reqClass = "";
        $requiredOpt = false;
        if ($this->helper->getmoduleConfiguration()['reserve_enable']) {
            $reqClass = 'required-entry';
            $requiredOpt = true;
        }
        $fieldset->addField(
            'reserve_price',
            'text',
            [
                'name' => 'reserve_price',
                'label' => __('Reserve Price'),
                'id' => 'reserve_price',
                'title' => __('Reserve Price'),
                'values' => [],
                'class' => $reqClass . ' validate-zero-or-greater',
                'required' => $requiredOpt,
            ]
        );

        $fieldset->addField(
            'start_module_time',
            'date',
            [
                'name' => 'start_module_time',
                'label' => __('Start module Time'),
                'id' => 'start_module_time',
                'title' => __('Start module Time'),
                'date_format' => $dateFormat,
                'time_format' => 'HH:mm:ss',
                'class' => 'required-entry admin__control-text',
                'style' => 'width:210px',
                'required' => true,
            ]
        );
        $fieldset->addField(
            'stop_module_time',
            'date',
            [
                'name' => 'stop_module_time',
                'label' => __('Stop module Time'),
                'id' => 'stop_module_time',
                'title' => __('Stop module Time'),
                'date_format' => $dateFormat,
                'time_format' => 'HH:mm:ss',
                'class' => 'required-entry admin__control-text',
                'style' => 'width:210px',
                'required' => true,
            ]
        );

        $fieldset->addField(
            'days',
            'text',
            [
                'name' => 'days',
                'label' => __('Number of Days Till Winner Can Buy'),
                'id' => 'days',
                'title' => __('Number of Days Till Winner Can Buy'),
                'class' => 'required-entry integer validate-zero-or-greater',
                'required' => true,
            ]
        );
        $fieldset->addField(
            'min_qty',
            'text',
            [
                'name' => 'min_qty',
                'label' => __('Minimum Quantity'),
                'id' => 'min_qty',
                'title' => __('Minimum Quantity'),
                'class' => 'required-entry validate-zero-or-greater',
                'required' => true,
            ]
        );

        $fieldset->addField(
            'max_qty',
            'text',
            [
                'name' => 'max_qty',
                'label' => __('Maximum Quantity'),
                'id' => 'max_qty',
                'title' => __('Maximum Quantity'),
                'class' => 'required-entry validate-zero-or-greater',
                'required' => true,
            ]
        );

        if ($this->helper->getmoduleConfiguration()['increment_auc_enable']) {
            $fieldset->addField(
                'increment_opt',
                'select',
                [
                    'name' => 'increment_opt',
                    'label' => __('Increment Option'),
                    'options' => ['1' => __('Enabled'), '0' => __('Disabled')],
                    'id' => 'increment_opt',
                    'title' => __('Increment Option'),
                    'class' => 'required-entry',
                    'required' => true,
                ]
            );
        }

        $fieldset->addField(
            'auto_module_opt',
            'select',
            [
                'name' => 'auto_module_opt',
                'label' => __('Automatic Option'),
                'options' => ['1' => __('Enabled'), '0' => __('Disabled')],
                'id' => 'auto_module_opt',
                'title' => __('Automatic Option'),
                'class' => 'required-entry',
                'required' => true,
            ]
        );

        $fieldset->addField(
            'buy_it_now',
            'select',
            [
                'name' => 'buy_it_now',
                'label' => __('Buy It Now'),
                'options' => ['1' => __('Enabled'), '0' => __('Disabled')],
                'id' => 'buy_it_now',
                'title' => __('Buy It Now'),
                'class' => 'required-entry',
                'required' => true,
            ]
        );
        $fieldset->addField(
            'featured_module',
            'select',
            [
                'name' => 'featured_module',
                'label' => __('Featured Status'),
                'options' => ['1' => __('Enabled'), '0' => __('Disabled')],
                'id' => 'buy_it_now',
                'title' => __('Featured Status'),
                'class' => 'required-entry',
                'required' => true,
            ]
        );
        if (!$model->getProductId()) {
            // add dependence javascript block
            $dependenceBlock = $this->getLayout()->createBlock('MagentoBackendBlockWidgetFormElementDependence');
            $this->setChild('form_after', $dependenceBlock);
            $dependenceBlock->addFieldMap($chooserField->getId(), 'product_id');
        }

        $data = $model->getData();
        if($data) {
            $data['stop_module_time'] = $this->timezone->date(new DateTime($data['stop_module_time']))->format('m/d/y H:i:s');
            $data['start_module_time'] = $this->timezone->date(new DateTime($data['start_module_time']))->format('m/d/y H:i:s');

            if (isset($data['entity_id']) && $data['entity_id']) {
                $data['module_entity_id'] = $data['entity_id'];
            }
            $form->setValues($data);
        }
        $this->setForm($form);
        return parent::_prepareForm();
    }


    /**
     * Prepare label for tab
     *
     * @return MagentoFrameworkPhrase
     */
    public function getTabLabel()
    {
        return __('Product Data');
    }

    /**
     * Prepare title for tab
     *
     * @return MagentoFrameworkPhrase
     */
    public function getTabTitle()
    {
        return __('Product Data');
    }

    /**
     * {@inheritdoc}
     */
    public function canShowTab()
    {
        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function isHidden()
    {
        return false;
    }

    /**
     * Check permission for passed action
     *
     * @param string $resourceId
     * @return bool
     */
    protected function _isAllowedAction($resourceId)
    {
        return $this->_authorization->isAllowed($resourceId);
    }

    /**
     * convertToTz convert Datetime from one zone to another
     * @param string $dateTime which we want to convert
     * @param string $toTz timezone in which we want to convert
     * @param string $fromTz timezone from which we want to convert
     * @return string
     * @throws Exception
     */
    protected function convertToTz($dateTime = "", $toTz = '', $fromTz = '')
    {
        // timezone by php friendly values
        $date = new DateTime($dateTime, new DateTimeZone($fromTz));
        $date->setTimezone(new DateTimeZone($toTz));
        $dateTime = $date->format('m/d/Y H:i:s');
        return $dateTime;
    }
}

Please help me.
and please let me know the solution to it… or
explain to me the working flow “MagentoCatalogBlockAdminhtmlProductWidgetChooser”.