M
M
matveyboyko2022-02-08 08:06:10
Magento
matveyboyko, 2022-02-08 08:06:10

How to hide/show a field depending on the value of another in Magento 2?

Hello!
Created three product attributes:
6201f927095ea503717069.png

1. app/code/Ns/My_module/Setup/UpgradeSchema.php:

<?php
 namespace NS\My_module\Setup;

 use Magento\Customer\Setup\CustomerSetupFactory;
 use Magento\Customer\Model\Customer;
 use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
 use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
 use Magento\Framework\Setup\UpgradeDataInterface;
 use Magento\Framework\Setup\ModuleContextInterface;
 use Magento\Framework\Setup\ModuleDataSetupInterface;

 class UpgradeData implements UpgradeDataInterface {
     protected $customerSetupFactory;
     private $attributeSetFactory;

     const IS_PRESALE = 'is_presale_product';
     const PRESALE_START = 'presale_product_from';
     const PRESALE_FINISH = 'presale_product_to';

     public function __construct(
         CustomerSetupFactory $customerSetupFactory,
         AttributeSetFactory $attributeSetFactory
     ) {
         $this->customerSetupFactory = $customerSetupFactory;
         $this->attributeSetFactory = $attributeSetFactory;
     }

     public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
     {
         $setup->startSetup();
         $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

         $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
         $attributeSetId = $customerEntity->getDefaultAttributeSetId();

         $attributeSet = $this->attributeSetFactory->create();
         $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
         if (version_compare($context->getVersion(), '1.0.1') < 0) {

             $customerSetup->addAttribute(
                 \Magento\Catalog\Model\Product::ENTITY,
                 self::IS_PRESALE,
                 [
                     'group' => "",
                     'label' => "Is PreSale?",
                     'is_html_allowed_on_front' => true,
                     'default' => '1',
                     'note' => '',
                     'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
                     'visible' => true,
                     'required' => false,
                     'user_defined' => false,
                     'searchable' => false,
                     'filterable' => false,
                     'comparable' => false,
                     'visible_on_front' => true,
                     'visible_in_advanced_search' => false,
                     'unique' => false,
                     "frontend_class" => "",
                     "used_in_product_listing" => true,
                     "input" => "select",
                     "type" => "varchar",
                     "source" => "NS\My_module\Model\Config\Source\PresaleSchedule",
                     'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend'
                 ]
             );

             $customerSetup->addAttribute(
                 \Magento\Catalog\Model\Product::ENTITY,
                 self::PRESALE_START,
                 [
                     'group' => '',
                     'label' => 'Presale From',
                     'type' => 'datetime',
                     'is_html_allowed_on_front' => true,
                     'default' => '',
                     'note' => '',
                     'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
                     'visible' => true,
                     'required' => false,
                     'user_defined' => false,
                     'searchable' => false,
                     'filterable' => false,
                     'comparable' => false,
                     'visible_on_front' => true,
                     'visible_in_advanced_search' => false,
                     'unique' => false,
                     'frontend_class' => '',
                     'used_in_product_listing' => true,
                     'input' => 'date',
                     'source' => '',
                     'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                 ]
             );

             $customerSetup->addAttribute(
                 \Magento\Catalog\Model\Product::ENTITY,
                 self::PRESALE_FINISH,
                 [
                     'group' => '',
                     'label' => 'Presale To',
                     'type' => 'datetime',
                     'is_html_allowed_on_front' => true,
                     'default' => '',
                     'note' => '',
                     'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
                     'visible' => true,
                     'required' => false,
                     'user_defined' => false,
                     'searchable' => false,
                     'filterable' => false,
                     'comparable' => false,
                     'visible_on_front' => true,
                     'visible_in_advanced_search' => false,
                     'unique' => false,
                     'frontend_class' => '',
                     'used_in_product_listing' => true,
                     'input' => 'date',
                     'source' => '',
                     'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                 ]
             );
         }

         $setup->endSetup();
     }
 }


2. app/code/NS/My_module/Model/Config/Source/PresaleSchedule.php:

<?php
 namespace NS\My_module\Model\Config\Source;

 class PresaleSchedule extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource {

     protected $_optionsData;

     public function getAllOptions()
     {
         if ($this->_options === null) {
             $this->_options = [
                 ['value' => '0', 'label' => __('No')],
                 ['value' => '1', 'label' => __('Yes')]
             ];
         }
         return $this->_options;
     }
 }


3. app/code/NS/My_module/Ui/DataProvider/Product/Form/Modifier/PreSellDesignUpdate.php:

<?php
 namespace NS\My_module\Ui\DataProvider\Product\Form\Modifier;

 use Magento\Framework\Stdlib\ArrayManager;
 use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;

 class PreSellDesignUpdate extends AbstractModifier {

     const PRESALE_START = 'presale_product_from';
     const PRESALE_END = 'presale_product_to';
     protected $arrayManager;

         public function __construct(ArrayManager $arrayManager)
         {
             $this->arrayManager = $arrayManager;
         }

         public function modifyMeta(array $meta)
         {
             return $this->customizeDateRangeField($meta);
         }

         public function modifyData(array $data)
         {
             return $data;
         }

         protected function customizeDateRangeField(array $meta)
         {
             if ($this->getGroupCodeByField($meta, self::PRESALE_START)
                 !== $this->getGroupCodeByField($meta, self::PRESALE_END)
             ) {
                 return $meta;
             }

             $fromFieldPath = $this->arrayManager->findPath(self::PRESALE_START, $meta, null, 'children');
             $toFieldPath = $this->arrayManager->findPath(self::PRESALE_END, $meta, null, 'children');
             $fromContainerPath = $this->arrayManager->slicePath($fromFieldPath, 0, -2);
             $toContainerPath = $this->arrayManager->slicePath($toFieldPath, 0, -2);

             $meta = $this->arrayManager->merge(
                 $fromFieldPath . self::META_CONFIG_PATH,
                 $meta,
                 [
                     'label' => __('Presell Product From'),
                     'additionalClasses' => 'admin__field-date',
                 ]
             );
             $meta = $this->arrayManager->merge(
                 $toFieldPath . self::META_CONFIG_PATH,
                 $meta,
                 [
                     'label' => __('To'),
                     'scopeLabel' => null,
                     'additionalClasses' => 'admin__field-date',
                 ]
             );
             $meta = $this->arrayManager->merge(
                 $fromContainerPath . self::META_CONFIG_PATH,
                 $meta,
                 [
                     'label' => false,
                     'required' => false,
                     'additionalClasses' => 'admin__control-grouped-date',
                     'breakLine' => false,
                     'component' => 'Magento_Ui/js/form/components/group',
                 ]
             );
             $meta = $this->arrayManager->set(
                 $fromContainerPath . '/children/' . self::PRESALE_END,
                 $meta,
                 $this->arrayManager->get($toFieldPath, $meta)
             );

             return $this->arrayManager->remove($toContainerPath, $meta);
         }


It is necessary to hide/show the "From" and "To" fields depending on the value of the "Is PreSale?" .
How can I do that?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question