Question

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?

Was it helpful?

Solution

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)

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top