0

Could we actually configure our installed module fields via Magento 2 CLI?

For example:

# typical way to enable a Magento 2 module, via Magento 2 CLI
bin/magento module:enable Acme_Acme

# can we do something like this?
bin/magento module:enable Acme_Acme --config=[password=1234]

Goal: The goal is to make bin/magento module:enable a little dummy-proof, if some more than one 'required' configuration field is still null or empty.

When the module is enabled, with the empty 'required' configuration fields, the plugin causes Magento 2 site errors and so on.

Alternative solutions: If going about the Magento 2 CLI is the wrong path towards a solution, what other solutions are available? Using the di.xml file? Programmatically "catch" for empty 'required' configuration fields?

FYI, Magento 2 casual here. Please feel free to share your thoughts, thank you.

3
  • You can set the depedency and validation on that fields on the configuration it self. Jul 28 at 4:49
  • @DhirenVasoya I see, I believe that's what I'm missing. I did a quick search on Magento 2 docs, with your suggestion and keywords. Are you referring to this example (I understand this is v2.4 docs)? (devdocs.magento.com/guides/v2.4/config-guide/prod/…)
    – Brian Ng
    Jul 28 at 4:58
  • Yes, let me post one example so you get idea. Jul 28 at 5:10
0

You can do this using your system.xml file like this one.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
       <section id="yoursectionid" sortOrder="150" type="text" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>YOUR LABLE</label>
            <tab>yourtab</tab>
            <resource>Vendor_Extension::yourresourcename</resource>
            <group id="general" translate="label" sortOrder="10" type="text" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Configuration</label>
                <field id="enable" translate="label" sortOrder="10" type="select" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Your Extension Enable</label>
                    <source_model>Magento\Config\Model\Config\Source\Enabledisable</source_model>
                </field>
                <field id="yourcustomfield" translate="label" sortOrder="20" type="text" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Your Custom Field Title</label>
                    <validate>required-entry</validate>
                    <depends>
                        <field id="*/*/enable">1</field>
                    </depends>
                </field>
             </group>
         </section>
    </system>
</config>
3
  • Thank you @DhirenVasoya for the example! Before your example, I was trying it out myself, trial-and-error. Yes, the <validate> element is all I needed actually, and you've also shown the <depends> element too, which gave me new ideas for the other fields' validations. Cheers, much appreciated!
    – Brian Ng
    Jul 28 at 5:46
  • Great, if answer upvote and mark as solutions, so it will help other in community as well. Jul 28 at 6:06
  • Thanks for the reminder, no problem. Your example above is "Accepted Answer". To upvote, I can't at this time, cause I need at least 15 reputation to cast a vote, but my feedback has been recorded.
    – Brian Ng
    Jul 28 at 6:23

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.