0

I created a new EAV attribute using this code :

'information',
    [
        'group' => 'Content',
        'type' => 'int',
        'default' => null,
        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
        'visible' => false
    ]

PS: I added this attribute to the product form

this attribute is by default hidden, but I want to visible it after a condition, is there any method to do that?

1

You can easily do that using a plugin. Try following way to doing this. Following example will hide product name. For your custom attribute you need to modify plugin code.

app/code/SR/MagentoCommunity/etc/adminhtml/di.xml

<?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\Catalog\Ui\DataProvider\Product\Form\Modifier\General">
        <plugin name="sr_attribute_visibility"
                type="SR\MagentoCommunity\Plugin\Catalog\Ui\DataProvider\Product\Form\Modifier\General" sortOrder="1"/>
    </type>
</config>

app/code/SR/MagentoCommunity/Plugin/Catalog/Ui/DataProvider/Product/Form/Modifier/General.php

<?php
namespace SR\MagentoCommunity\Plugin\Catalog\Ui\DataProvider\Product\Form\Modifier;


class General
{
    public function afterModifyMeta(
        \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\General $subject,
        $meta
    ) {
        // add your condition
        $meta['product-details']['children']['container_name']['children']['name']['arguments']['data']['config']['visible'] = 0;
        // print $meta and check your custom attribute index.
        return $meta;
    }
}

[Update]

Suppose information is text attribute. Now modify following way:

<?php
namespace SR\MagentoCommunity\Plugin\Catalog\Ui\DataProvider\Product\Form\Modifier;


class General
{
    public function afterModifyMeta(
        \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\General $subject,
        $meta
    ) {
        // add your condition
        if (isset($meta['product-details']['children']['container_information'])) {
            $meta['product-details']['children']['container_information']['children']['information']['arguments']['data']['config']['visible'] = 0;
        }

        return $meta;
    }
}

PS: the attribute visibility should be by default: true (false don't work)

  • Thanks for your reply sir, That I need to change this ['name'] with ['information'] ? name of my EAV attribute – sayou May 2 at 17:13
  • I think you need to change container_name too. In your case should container_information. – Sohel Rana May 2 at 17:14
  • I get this error The "componentType" configuration parameter is required for the "container_information" component – sayou May 2 at 17:16
  • Try using: $meta['product-details']['children']['container_information']['children']['information']['arguments']['data']['config']['visible'] = 0; – Sohel Rana May 2 at 17:22
  • Check updated answer – Sohel Rana May 2 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.