How to create Magento Config Yesno in custom phtml template in magento 2?












1














I want to create magento default Yesno option in custom phtml template.
This can be created in system.xml by using this code.



<field id="testID" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Custom Select Option</label>
<source_model>MagentoConfigModelConfigSourceYesno</source_model>
</field>


But how this can be created in my custom phtml template.










share|improve this question






















  • you can use magento select class from admin
    – magefms
    yesterday










  • Frontend or Backend ?
    – PЯINCƏ
    yesterday










  • i post my answer which works for me
    – iqbal malik
    yesterday
















1














I want to create magento default Yesno option in custom phtml template.
This can be created in system.xml by using this code.



<field id="testID" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Custom Select Option</label>
<source_model>MagentoConfigModelConfigSourceYesno</source_model>
</field>


But how this can be created in my custom phtml template.










share|improve this question






















  • you can use magento select class from admin
    – magefms
    yesterday










  • Frontend or Backend ?
    – PЯINCƏ
    yesterday










  • i post my answer which works for me
    – iqbal malik
    yesterday














1












1








1


1





I want to create magento default Yesno option in custom phtml template.
This can be created in system.xml by using this code.



<field id="testID" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Custom Select Option</label>
<source_model>MagentoConfigModelConfigSourceYesno</source_model>
</field>


But how this can be created in my custom phtml template.










share|improve this question













I want to create magento default Yesno option in custom phtml template.
This can be created in system.xml by using this code.



<field id="testID" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Custom Select Option</label>
<source_model>MagentoConfigModelConfigSourceYesno</source_model>
</field>


But how this can be created in my custom phtml template.







magento2






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









iqbal malikiqbal malik

7310




7310












  • you can use magento select class from admin
    – magefms
    yesterday










  • Frontend or Backend ?
    – PЯINCƏ
    yesterday










  • i post my answer which works for me
    – iqbal malik
    yesterday


















  • you can use magento select class from admin
    – magefms
    yesterday










  • Frontend or Backend ?
    – PЯINCƏ
    yesterday










  • i post my answer which works for me
    – iqbal malik
    yesterday
















you can use magento select class from admin
– magefms
yesterday




you can use magento select class from admin
– magefms
yesterday












Frontend or Backend ?
– PЯINCƏ
yesterday




Frontend or Backend ?
– PЯINCƏ
yesterday












i post my answer which works for me
– iqbal malik
yesterday




i post my answer which works for me
– iqbal malik
yesterday










2 Answers
2






active

oldest

votes


















1















You can use select class from admin




check and try this below code in your custom phtml:



<select name="yesno" class="select admin__control-select">
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>





share|improve this answer





















  • Save config not saving this option
    – iqbal malik
    yesterday










  • are you implementing it on a system.xml config file?
    – magefms
    yesterday










  • a block is called from system.xml and this block call that phtml file. If another module is enabled than this select option should display.
    – iqbal malik
    yesterday










  • I see, it is a little bit complicated. can you add your system config code in your post.
    – magefms
    yesterday










  • i done this by using save config hook. Thanks for your time
    – iqbal malik
    yesterday



















0














I have done this maybe this will help others.

My system.xml



<field id="testID" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Custom Select Option</label>
<source_model>VendorModuleBlockSystemConfigCustom</source_model>
</field><br>


My Block file



<?php
namespace VendorModuleBlockSystemConfig;

use MagentoFrameworkAppConfigScopeConfigInterface;

class RADforLiftgate extends MagentoConfigBlockSystemConfigFormField
{
const TEMPLATE = 'system/config/custom.phtml';

protected $_moduleManager;
public $enable = 'no';

/**
*
* @param MagentoBackendBlockTemplateContext $context
* @param ScopeConfigInterface $scopeConfig
* @param MagentoFrameworkModuleManager $moduleManager
* @param MagentoFrameworkObjectManagerInterface $objectmanager
* @param array $data
*/
public function __construct(
MagentoBackendBlockTemplateContext $context,
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
MagentoFrameworkModuleManager $moduleManager,
array $data =
)
{
$this->_scopeConfig = $scopeConfig;
$this->_moduleManager = $moduleManager;
$this->checkOtherModule();
parent::__construct($context, $data);
}

/**
*
* @return $this
*/
protected function _prepareLayout()
{
parent::_prepareLayout();
if (!$this->getTemplate()) {
$this->setTemplate(static::TEMPLATE);
}
return $this;
}

/**
* checkOtherModule
*/
protected function checkOtherModule()
{
if($this->_moduleManager->isEnabled('OtherVendor_OtherModule')){
$this->enable = 'yes';
}
}
/**
* Retrieve element HTML markup.
*
* @param MagentoFrameworkDataFormElementAbstractElement $element
*
* @return string
*/
protected function _getElementHtml(MagentoFrameworkDataFormElementAbstractElement $element)
{
$this->setNamePrefix($element->getName())
->setHtmlId($element->getHtmlId());

return $this->_toHtml();
}

/**
*
* @param $name
* @return boolean
*/
public function getIsChecked()
{
return $this->_scopeConfig->getValue("vendor/module/custom", MagentoStoreModelScopeInterface::SCOPE_STORE);
}
}<br>


My phtml file



<?php
if($this->enable == 'yes'){ ?>
<select name="custom" class=" select admin__control-select"
data-ui-id="select-groups-third-fields-custom-value">
<option value="no" <?php echo ($this->getIsChecked() == 'no')? 'selected="selected"':''; ?>>No</option>
<option value="yes" <?php echo ($this->getIsChecked() == 'yes')? 'selected="selected"':''; ?>>Yes</option>
</select>

<?php } else { ?>
<div class="rad">
<p>
Click <a href="#">here</a> to read paragraph.
</p>
</div>



Now to save this select value on Save Config button. Create a file di.xml



<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<preference for="MagentoCheckoutBlockCartLayoutProcessor" type="VendorModuleModelCheckoutBlockCartShipping" />
<preference for="MagentoCatalogControllerAdminhtmlProductEdit" type="VendorModuleControllerAdminhtmlProductEdit" />
<type name="MagentoConfigModelConfig">
<plugin name="admin_system_config_save_fedexltl_plugin" type="VendorModuleModelConfigSaveConfig" sortOrder="1"/>
</type>





Now final step i create a Model file.



<?php
namespace VendorModuleModelConfig;
use MagentoFrameworkAppConfigScopeConfigInterface;
class SaveConfig{
/**
* @var MagentoFrameworkAppConfigStorageWriterInterface
*/
protected $_configWriter;
protected $_scopeConfig;

/**
*
* @param MagentoFrameworkAppRequestHttp $request
* @param ScopeConfigInterface $scopeConfig
* @param MagentoFrameworkAppConfigStorageWriterInterface $configWriter
*/
public function __construct(
MagentoFrameworkAppRequestHttp $request,
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
MagentoFrameworkAppConfigStorageWriterInterface $configWriter
) {
$this->request = $request;
$this->_scopeConfig = $scopeConfig;
$this->_configWriter = $configWriter;
}

/**
*
* @param MagentoConfigModelConfig $subject
* @param Closure $proceed
* @return type
*/
public function aroundSave(
MagentoConfigModelConfig $subject,
Closure $proceed
) {
$postData = $this->request->getPostValue();

if(isset($postData['config_state']['group_section'])){
$isActive = (isset($postData['groups']['section']['fields']['custom'])
&& $postData['groups']['section']['fields']['custom'] == 'yes') ? 'yes' : 'no';
$this->_configWriter->save('Vendor/Module/custom', $isActive, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0);
}

return $proceed();
}
}





share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "479"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f257062%2fhow-to-create-magento-config-yesno-in-custom-phtml-template-in-magento-2%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1















    You can use select class from admin




    check and try this below code in your custom phtml:



    <select name="yesno" class="select admin__control-select">
    <option value="Yes">Yes</option>
    <option value="No">No</option>
    </select>





    share|improve this answer





















    • Save config not saving this option
      – iqbal malik
      yesterday










    • are you implementing it on a system.xml config file?
      – magefms
      yesterday










    • a block is called from system.xml and this block call that phtml file. If another module is enabled than this select option should display.
      – iqbal malik
      yesterday










    • I see, it is a little bit complicated. can you add your system config code in your post.
      – magefms
      yesterday










    • i done this by using save config hook. Thanks for your time
      – iqbal malik
      yesterday
















    1















    You can use select class from admin




    check and try this below code in your custom phtml:



    <select name="yesno" class="select admin__control-select">
    <option value="Yes">Yes</option>
    <option value="No">No</option>
    </select>





    share|improve this answer





















    • Save config not saving this option
      – iqbal malik
      yesterday










    • are you implementing it on a system.xml config file?
      – magefms
      yesterday










    • a block is called from system.xml and this block call that phtml file. If another module is enabled than this select option should display.
      – iqbal malik
      yesterday










    • I see, it is a little bit complicated. can you add your system config code in your post.
      – magefms
      yesterday










    • i done this by using save config hook. Thanks for your time
      – iqbal malik
      yesterday














    1












    1








    1







    You can use select class from admin




    check and try this below code in your custom phtml:



    <select name="yesno" class="select admin__control-select">
    <option value="Yes">Yes</option>
    <option value="No">No</option>
    </select>





    share|improve this answer













    You can use select class from admin




    check and try this below code in your custom phtml:



    <select name="yesno" class="select admin__control-select">
    <option value="Yes">Yes</option>
    <option value="No">No</option>
    </select>






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered yesterday









    magefmsmagefms

    31510




    31510












    • Save config not saving this option
      – iqbal malik
      yesterday










    • are you implementing it on a system.xml config file?
      – magefms
      yesterday










    • a block is called from system.xml and this block call that phtml file. If another module is enabled than this select option should display.
      – iqbal malik
      yesterday










    • I see, it is a little bit complicated. can you add your system config code in your post.
      – magefms
      yesterday










    • i done this by using save config hook. Thanks for your time
      – iqbal malik
      yesterday


















    • Save config not saving this option
      – iqbal malik
      yesterday










    • are you implementing it on a system.xml config file?
      – magefms
      yesterday










    • a block is called from system.xml and this block call that phtml file. If another module is enabled than this select option should display.
      – iqbal malik
      yesterday










    • I see, it is a little bit complicated. can you add your system config code in your post.
      – magefms
      yesterday










    • i done this by using save config hook. Thanks for your time
      – iqbal malik
      yesterday
















    Save config not saving this option
    – iqbal malik
    yesterday




    Save config not saving this option
    – iqbal malik
    yesterday












    are you implementing it on a system.xml config file?
    – magefms
    yesterday




    are you implementing it on a system.xml config file?
    – magefms
    yesterday












    a block is called from system.xml and this block call that phtml file. If another module is enabled than this select option should display.
    – iqbal malik
    yesterday




    a block is called from system.xml and this block call that phtml file. If another module is enabled than this select option should display.
    – iqbal malik
    yesterday












    I see, it is a little bit complicated. can you add your system config code in your post.
    – magefms
    yesterday




    I see, it is a little bit complicated. can you add your system config code in your post.
    – magefms
    yesterday












    i done this by using save config hook. Thanks for your time
    – iqbal malik
    yesterday




    i done this by using save config hook. Thanks for your time
    – iqbal malik
    yesterday













    0














    I have done this maybe this will help others.

    My system.xml



    <field id="testID" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Custom Select Option</label>
    <source_model>VendorModuleBlockSystemConfigCustom</source_model>
    </field><br>


    My Block file



    <?php
    namespace VendorModuleBlockSystemConfig;

    use MagentoFrameworkAppConfigScopeConfigInterface;

    class RADforLiftgate extends MagentoConfigBlockSystemConfigFormField
    {
    const TEMPLATE = 'system/config/custom.phtml';

    protected $_moduleManager;
    public $enable = 'no';

    /**
    *
    * @param MagentoBackendBlockTemplateContext $context
    * @param ScopeConfigInterface $scopeConfig
    * @param MagentoFrameworkModuleManager $moduleManager
    * @param MagentoFrameworkObjectManagerInterface $objectmanager
    * @param array $data
    */
    public function __construct(
    MagentoBackendBlockTemplateContext $context,
    MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
    MagentoFrameworkModuleManager $moduleManager,
    array $data =
    )
    {
    $this->_scopeConfig = $scopeConfig;
    $this->_moduleManager = $moduleManager;
    $this->checkOtherModule();
    parent::__construct($context, $data);
    }

    /**
    *
    * @return $this
    */
    protected function _prepareLayout()
    {
    parent::_prepareLayout();
    if (!$this->getTemplate()) {
    $this->setTemplate(static::TEMPLATE);
    }
    return $this;
    }

    /**
    * checkOtherModule
    */
    protected function checkOtherModule()
    {
    if($this->_moduleManager->isEnabled('OtherVendor_OtherModule')){
    $this->enable = 'yes';
    }
    }
    /**
    * Retrieve element HTML markup.
    *
    * @param MagentoFrameworkDataFormElementAbstractElement $element
    *
    * @return string
    */
    protected function _getElementHtml(MagentoFrameworkDataFormElementAbstractElement $element)
    {
    $this->setNamePrefix($element->getName())
    ->setHtmlId($element->getHtmlId());

    return $this->_toHtml();
    }

    /**
    *
    * @param $name
    * @return boolean
    */
    public function getIsChecked()
    {
    return $this->_scopeConfig->getValue("vendor/module/custom", MagentoStoreModelScopeInterface::SCOPE_STORE);
    }
    }<br>


    My phtml file



    <?php
    if($this->enable == 'yes'){ ?>
    <select name="custom" class=" select admin__control-select"
    data-ui-id="select-groups-third-fields-custom-value">
    <option value="no" <?php echo ($this->getIsChecked() == 'no')? 'selected="selected"':''; ?>>No</option>
    <option value="yes" <?php echo ($this->getIsChecked() == 'yes')? 'selected="selected"':''; ?>>Yes</option>
    </select>

    <?php } else { ?>
    <div class="rad">
    <p>
    Click <a href="#">here</a> to read paragraph.
    </p>
    </div>



    Now to save this select value on Save Config button. Create a file di.xml



    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference for="MagentoCheckoutBlockCartLayoutProcessor" type="VendorModuleModelCheckoutBlockCartShipping" />
    <preference for="MagentoCatalogControllerAdminhtmlProductEdit" type="VendorModuleControllerAdminhtmlProductEdit" />
    <type name="MagentoConfigModelConfig">
    <plugin name="admin_system_config_save_fedexltl_plugin" type="VendorModuleModelConfigSaveConfig" sortOrder="1"/>
    </type>





    Now final step i create a Model file.



    <?php
    namespace VendorModuleModelConfig;
    use MagentoFrameworkAppConfigScopeConfigInterface;
    class SaveConfig{
    /**
    * @var MagentoFrameworkAppConfigStorageWriterInterface
    */
    protected $_configWriter;
    protected $_scopeConfig;

    /**
    *
    * @param MagentoFrameworkAppRequestHttp $request
    * @param ScopeConfigInterface $scopeConfig
    * @param MagentoFrameworkAppConfigStorageWriterInterface $configWriter
    */
    public function __construct(
    MagentoFrameworkAppRequestHttp $request,
    MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
    MagentoFrameworkAppConfigStorageWriterInterface $configWriter
    ) {
    $this->request = $request;
    $this->_scopeConfig = $scopeConfig;
    $this->_configWriter = $configWriter;
    }

    /**
    *
    * @param MagentoConfigModelConfig $subject
    * @param Closure $proceed
    * @return type
    */
    public function aroundSave(
    MagentoConfigModelConfig $subject,
    Closure $proceed
    ) {
    $postData = $this->request->getPostValue();

    if(isset($postData['config_state']['group_section'])){
    $isActive = (isset($postData['groups']['section']['fields']['custom'])
    && $postData['groups']['section']['fields']['custom'] == 'yes') ? 'yes' : 'no';
    $this->_configWriter->save('Vendor/Module/custom', $isActive, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0);
    }

    return $proceed();
    }
    }





    share|improve this answer


























      0














      I have done this maybe this will help others.

      My system.xml



      <field id="testID" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
      <label>Custom Select Option</label>
      <source_model>VendorModuleBlockSystemConfigCustom</source_model>
      </field><br>


      My Block file



      <?php
      namespace VendorModuleBlockSystemConfig;

      use MagentoFrameworkAppConfigScopeConfigInterface;

      class RADforLiftgate extends MagentoConfigBlockSystemConfigFormField
      {
      const TEMPLATE = 'system/config/custom.phtml';

      protected $_moduleManager;
      public $enable = 'no';

      /**
      *
      * @param MagentoBackendBlockTemplateContext $context
      * @param ScopeConfigInterface $scopeConfig
      * @param MagentoFrameworkModuleManager $moduleManager
      * @param MagentoFrameworkObjectManagerInterface $objectmanager
      * @param array $data
      */
      public function __construct(
      MagentoBackendBlockTemplateContext $context,
      MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
      MagentoFrameworkModuleManager $moduleManager,
      array $data =
      )
      {
      $this->_scopeConfig = $scopeConfig;
      $this->_moduleManager = $moduleManager;
      $this->checkOtherModule();
      parent::__construct($context, $data);
      }

      /**
      *
      * @return $this
      */
      protected function _prepareLayout()
      {
      parent::_prepareLayout();
      if (!$this->getTemplate()) {
      $this->setTemplate(static::TEMPLATE);
      }
      return $this;
      }

      /**
      * checkOtherModule
      */
      protected function checkOtherModule()
      {
      if($this->_moduleManager->isEnabled('OtherVendor_OtherModule')){
      $this->enable = 'yes';
      }
      }
      /**
      * Retrieve element HTML markup.
      *
      * @param MagentoFrameworkDataFormElementAbstractElement $element
      *
      * @return string
      */
      protected function _getElementHtml(MagentoFrameworkDataFormElementAbstractElement $element)
      {
      $this->setNamePrefix($element->getName())
      ->setHtmlId($element->getHtmlId());

      return $this->_toHtml();
      }

      /**
      *
      * @param $name
      * @return boolean
      */
      public function getIsChecked()
      {
      return $this->_scopeConfig->getValue("vendor/module/custom", MagentoStoreModelScopeInterface::SCOPE_STORE);
      }
      }<br>


      My phtml file



      <?php
      if($this->enable == 'yes'){ ?>
      <select name="custom" class=" select admin__control-select"
      data-ui-id="select-groups-third-fields-custom-value">
      <option value="no" <?php echo ($this->getIsChecked() == 'no')? 'selected="selected"':''; ?>>No</option>
      <option value="yes" <?php echo ($this->getIsChecked() == 'yes')? 'selected="selected"':''; ?>>Yes</option>
      </select>

      <?php } else { ?>
      <div class="rad">
      <p>
      Click <a href="#">here</a> to read paragraph.
      </p>
      </div>



      Now to save this select value on Save Config button. Create a file di.xml



      <?xml version="1.0"?>
      <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
      <preference for="MagentoCheckoutBlockCartLayoutProcessor" type="VendorModuleModelCheckoutBlockCartShipping" />
      <preference for="MagentoCatalogControllerAdminhtmlProductEdit" type="VendorModuleControllerAdminhtmlProductEdit" />
      <type name="MagentoConfigModelConfig">
      <plugin name="admin_system_config_save_fedexltl_plugin" type="VendorModuleModelConfigSaveConfig" sortOrder="1"/>
      </type>





      Now final step i create a Model file.



      <?php
      namespace VendorModuleModelConfig;
      use MagentoFrameworkAppConfigScopeConfigInterface;
      class SaveConfig{
      /**
      * @var MagentoFrameworkAppConfigStorageWriterInterface
      */
      protected $_configWriter;
      protected $_scopeConfig;

      /**
      *
      * @param MagentoFrameworkAppRequestHttp $request
      * @param ScopeConfigInterface $scopeConfig
      * @param MagentoFrameworkAppConfigStorageWriterInterface $configWriter
      */
      public function __construct(
      MagentoFrameworkAppRequestHttp $request,
      MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
      MagentoFrameworkAppConfigStorageWriterInterface $configWriter
      ) {
      $this->request = $request;
      $this->_scopeConfig = $scopeConfig;
      $this->_configWriter = $configWriter;
      }

      /**
      *
      * @param MagentoConfigModelConfig $subject
      * @param Closure $proceed
      * @return type
      */
      public function aroundSave(
      MagentoConfigModelConfig $subject,
      Closure $proceed
      ) {
      $postData = $this->request->getPostValue();

      if(isset($postData['config_state']['group_section'])){
      $isActive = (isset($postData['groups']['section']['fields']['custom'])
      && $postData['groups']['section']['fields']['custom'] == 'yes') ? 'yes' : 'no';
      $this->_configWriter->save('Vendor/Module/custom', $isActive, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0);
      }

      return $proceed();
      }
      }





      share|improve this answer
























        0












        0








        0






        I have done this maybe this will help others.

        My system.xml



        <field id="testID" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
        <label>Custom Select Option</label>
        <source_model>VendorModuleBlockSystemConfigCustom</source_model>
        </field><br>


        My Block file



        <?php
        namespace VendorModuleBlockSystemConfig;

        use MagentoFrameworkAppConfigScopeConfigInterface;

        class RADforLiftgate extends MagentoConfigBlockSystemConfigFormField
        {
        const TEMPLATE = 'system/config/custom.phtml';

        protected $_moduleManager;
        public $enable = 'no';

        /**
        *
        * @param MagentoBackendBlockTemplateContext $context
        * @param ScopeConfigInterface $scopeConfig
        * @param MagentoFrameworkModuleManager $moduleManager
        * @param MagentoFrameworkObjectManagerInterface $objectmanager
        * @param array $data
        */
        public function __construct(
        MagentoBackendBlockTemplateContext $context,
        MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
        MagentoFrameworkModuleManager $moduleManager,
        array $data =
        )
        {
        $this->_scopeConfig = $scopeConfig;
        $this->_moduleManager = $moduleManager;
        $this->checkOtherModule();
        parent::__construct($context, $data);
        }

        /**
        *
        * @return $this
        */
        protected function _prepareLayout()
        {
        parent::_prepareLayout();
        if (!$this->getTemplate()) {
        $this->setTemplate(static::TEMPLATE);
        }
        return $this;
        }

        /**
        * checkOtherModule
        */
        protected function checkOtherModule()
        {
        if($this->_moduleManager->isEnabled('OtherVendor_OtherModule')){
        $this->enable = 'yes';
        }
        }
        /**
        * Retrieve element HTML markup.
        *
        * @param MagentoFrameworkDataFormElementAbstractElement $element
        *
        * @return string
        */
        protected function _getElementHtml(MagentoFrameworkDataFormElementAbstractElement $element)
        {
        $this->setNamePrefix($element->getName())
        ->setHtmlId($element->getHtmlId());

        return $this->_toHtml();
        }

        /**
        *
        * @param $name
        * @return boolean
        */
        public function getIsChecked()
        {
        return $this->_scopeConfig->getValue("vendor/module/custom", MagentoStoreModelScopeInterface::SCOPE_STORE);
        }
        }<br>


        My phtml file



        <?php
        if($this->enable == 'yes'){ ?>
        <select name="custom" class=" select admin__control-select"
        data-ui-id="select-groups-third-fields-custom-value">
        <option value="no" <?php echo ($this->getIsChecked() == 'no')? 'selected="selected"':''; ?>>No</option>
        <option value="yes" <?php echo ($this->getIsChecked() == 'yes')? 'selected="selected"':''; ?>>Yes</option>
        </select>

        <?php } else { ?>
        <div class="rad">
        <p>
        Click <a href="#">here</a> to read paragraph.
        </p>
        </div>



        Now to save this select value on Save Config button. Create a file di.xml



        <?xml version="1.0"?>
        <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
        <preference for="MagentoCheckoutBlockCartLayoutProcessor" type="VendorModuleModelCheckoutBlockCartShipping" />
        <preference for="MagentoCatalogControllerAdminhtmlProductEdit" type="VendorModuleControllerAdminhtmlProductEdit" />
        <type name="MagentoConfigModelConfig">
        <plugin name="admin_system_config_save_fedexltl_plugin" type="VendorModuleModelConfigSaveConfig" sortOrder="1"/>
        </type>





        Now final step i create a Model file.



        <?php
        namespace VendorModuleModelConfig;
        use MagentoFrameworkAppConfigScopeConfigInterface;
        class SaveConfig{
        /**
        * @var MagentoFrameworkAppConfigStorageWriterInterface
        */
        protected $_configWriter;
        protected $_scopeConfig;

        /**
        *
        * @param MagentoFrameworkAppRequestHttp $request
        * @param ScopeConfigInterface $scopeConfig
        * @param MagentoFrameworkAppConfigStorageWriterInterface $configWriter
        */
        public function __construct(
        MagentoFrameworkAppRequestHttp $request,
        MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
        MagentoFrameworkAppConfigStorageWriterInterface $configWriter
        ) {
        $this->request = $request;
        $this->_scopeConfig = $scopeConfig;
        $this->_configWriter = $configWriter;
        }

        /**
        *
        * @param MagentoConfigModelConfig $subject
        * @param Closure $proceed
        * @return type
        */
        public function aroundSave(
        MagentoConfigModelConfig $subject,
        Closure $proceed
        ) {
        $postData = $this->request->getPostValue();

        if(isset($postData['config_state']['group_section'])){
        $isActive = (isset($postData['groups']['section']['fields']['custom'])
        && $postData['groups']['section']['fields']['custom'] == 'yes') ? 'yes' : 'no';
        $this->_configWriter->save('Vendor/Module/custom', $isActive, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0);
        }

        return $proceed();
        }
        }





        share|improve this answer












        I have done this maybe this will help others.

        My system.xml



        <field id="testID" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
        <label>Custom Select Option</label>
        <source_model>VendorModuleBlockSystemConfigCustom</source_model>
        </field><br>


        My Block file



        <?php
        namespace VendorModuleBlockSystemConfig;

        use MagentoFrameworkAppConfigScopeConfigInterface;

        class RADforLiftgate extends MagentoConfigBlockSystemConfigFormField
        {
        const TEMPLATE = 'system/config/custom.phtml';

        protected $_moduleManager;
        public $enable = 'no';

        /**
        *
        * @param MagentoBackendBlockTemplateContext $context
        * @param ScopeConfigInterface $scopeConfig
        * @param MagentoFrameworkModuleManager $moduleManager
        * @param MagentoFrameworkObjectManagerInterface $objectmanager
        * @param array $data
        */
        public function __construct(
        MagentoBackendBlockTemplateContext $context,
        MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
        MagentoFrameworkModuleManager $moduleManager,
        array $data =
        )
        {
        $this->_scopeConfig = $scopeConfig;
        $this->_moduleManager = $moduleManager;
        $this->checkOtherModule();
        parent::__construct($context, $data);
        }

        /**
        *
        * @return $this
        */
        protected function _prepareLayout()
        {
        parent::_prepareLayout();
        if (!$this->getTemplate()) {
        $this->setTemplate(static::TEMPLATE);
        }
        return $this;
        }

        /**
        * checkOtherModule
        */
        protected function checkOtherModule()
        {
        if($this->_moduleManager->isEnabled('OtherVendor_OtherModule')){
        $this->enable = 'yes';
        }
        }
        /**
        * Retrieve element HTML markup.
        *
        * @param MagentoFrameworkDataFormElementAbstractElement $element
        *
        * @return string
        */
        protected function _getElementHtml(MagentoFrameworkDataFormElementAbstractElement $element)
        {
        $this->setNamePrefix($element->getName())
        ->setHtmlId($element->getHtmlId());

        return $this->_toHtml();
        }

        /**
        *
        * @param $name
        * @return boolean
        */
        public function getIsChecked()
        {
        return $this->_scopeConfig->getValue("vendor/module/custom", MagentoStoreModelScopeInterface::SCOPE_STORE);
        }
        }<br>


        My phtml file



        <?php
        if($this->enable == 'yes'){ ?>
        <select name="custom" class=" select admin__control-select"
        data-ui-id="select-groups-third-fields-custom-value">
        <option value="no" <?php echo ($this->getIsChecked() == 'no')? 'selected="selected"':''; ?>>No</option>
        <option value="yes" <?php echo ($this->getIsChecked() == 'yes')? 'selected="selected"':''; ?>>Yes</option>
        </select>

        <?php } else { ?>
        <div class="rad">
        <p>
        Click <a href="#">here</a> to read paragraph.
        </p>
        </div>



        Now to save this select value on Save Config button. Create a file di.xml



        <?xml version="1.0"?>
        <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
        <preference for="MagentoCheckoutBlockCartLayoutProcessor" type="VendorModuleModelCheckoutBlockCartShipping" />
        <preference for="MagentoCatalogControllerAdminhtmlProductEdit" type="VendorModuleControllerAdminhtmlProductEdit" />
        <type name="MagentoConfigModelConfig">
        <plugin name="admin_system_config_save_fedexltl_plugin" type="VendorModuleModelConfigSaveConfig" sortOrder="1"/>
        </type>





        Now final step i create a Model file.



        <?php
        namespace VendorModuleModelConfig;
        use MagentoFrameworkAppConfigScopeConfigInterface;
        class SaveConfig{
        /**
        * @var MagentoFrameworkAppConfigStorageWriterInterface
        */
        protected $_configWriter;
        protected $_scopeConfig;

        /**
        *
        * @param MagentoFrameworkAppRequestHttp $request
        * @param ScopeConfigInterface $scopeConfig
        * @param MagentoFrameworkAppConfigStorageWriterInterface $configWriter
        */
        public function __construct(
        MagentoFrameworkAppRequestHttp $request,
        MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
        MagentoFrameworkAppConfigStorageWriterInterface $configWriter
        ) {
        $this->request = $request;
        $this->_scopeConfig = $scopeConfig;
        $this->_configWriter = $configWriter;
        }

        /**
        *
        * @param MagentoConfigModelConfig $subject
        * @param Closure $proceed
        * @return type
        */
        public function aroundSave(
        MagentoConfigModelConfig $subject,
        Closure $proceed
        ) {
        $postData = $this->request->getPostValue();

        if(isset($postData['config_state']['group_section'])){
        $isActive = (isset($postData['groups']['section']['fields']['custom'])
        && $postData['groups']['section']['fields']['custom'] == 'yes') ? 'yes' : 'no';
        $this->_configWriter->save('Vendor/Module/custom', $isActive, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0);
        }

        return $proceed();
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        iqbal malikiqbal malik

        7310




        7310






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f257062%2fhow-to-create-magento-config-yesno-in-custom-phtml-template-in-magento-2%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            1300-talet

            1300-talet

            Display a custom attribute below product name in the front-end Magento 1.9.3.8