0

I want to add some fields in the Magento 2 Admin Panel for my custom module and then I want to check in the phtml file if this field is have value or not.

I need to add this fields:

- enable / disable (yes/no field type) ;
- Add text {input text field type};
- upload image;

the upload image field must have the option to delete the image too.

For enable / disable I add this in the system.xml:

  <section id="mymodule" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
        <class>separator-top</class>
        <label>Module</label>
        <tab>mymodule</tab>
        <resource>Module_First::mymodule_configuration</resource>
        <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
            <label>General Configuration</label>
            <field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>Enable</label>
                <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
            </field>
        </group>
    </section>

I want to know what is the format for adminhtml/system.xml and how I can check this fields value in the phtml file (or what is the best way to check the value for this fields).

Thank you

3 Answers 3

3

Add below code into each of your controller at first line of execute method:

if (!$this->moduleEnabled()) {
    return $this->_forwardNoroute();
}

moduleEnabled():

/**
 * Retrieve true if extension is enabled.
 *
 * @return bool
 */
protected function moduleEnabled()
{
    return (bool) $this->getConfigValue(
        self::XML_PATH_EXTENSION_ENABLED,
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE
    );
}

XML_PATH_EXTENSION_ENABLED contains path of system.xml to enabled/disabled module

/**
 * Extension enabled config path
 */
const XML_PATH_EXTENSION_ENABLED = '<Secton_Name>/general/enabled';
2

To add enable/disable field in system.xml, you need to do this code in syste.xml file.

Vendor\Extension\etc\adminhtml\system.xml

<?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="mymodule" sortOrder="150" type="text" showInDefault="1" showInWebsite="1" showInStore="1">
      <label>Mymodule Label</label>
      <tab>mymoduletab</tab>
      <resource>Module_First::mymodule_configuration</resource>
      <group id="general" translate="label" sortOrder="10" type="text" showInDefault="1" showInWebsite="1" showInStore="1">
        <label>General Configuration</label>
        <field id="enable" translate="label" sortOrder="10" type="select" showInDefault="1" showInWebsite="1" showInStore="1">
          <label>Enable</label>
          <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
        </field>
      </group>
    </section>
  </system>
</config>

And to check that field value in phtml you can create Helper file (Data.php) at this location Vendor/Extension/Helper/Data.php.

<?php
namespace Vendor\Extension\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\ScopeInterface;

class Data extends AbstractHelper
{
    protected $context;

    public function __construct(Context $context)
    {
        $this->context = $context;
        parent::__construct($context);
    }

    public function isEnable()
    {
        return $this->scopeConfig->getValue('mymodule/general/enable', ScopeInterface::SCOPE_STORE);
    }
}

Now Write this code in phtml file :

<?php 
$helper = \Magento\Framework\App\ObjectManager::getInstance()->get('Vendor\Extension\Helper\Data'); 
if($helper->isEnable()) : ?>

//WRITE YOUR CODE HERE

<?php endif; ?>
8
  • thank you for your answer is work but how I can get the the input text value?
    – Robert
    Commented Nov 28, 2017 at 13:24
  • If above answer help you, accept it so it help other in community as well. And for input text value you need to create another function in helper and also change field name into that. Commented Nov 28, 2017 at 13:27
  • and for upload image field?
    – Robert
    Commented Nov 28, 2017 at 13:31
  • Any field you need to do the same things, create one function in Helper and and change field name into that. Commented Nov 28, 2017 at 13:32
  • can you create an example for the image upload field? and how I will get it in the phtml?
    – Robert
    Commented Nov 28, 2017 at 13:35
0

Try this to check if your module is enable in system configuration

In your Block

protected $scopeConfig;

public function __construct(
    ...
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    ...
)
{
    ...
    $this->scopeConfig = $scopeConfig;
    ...
}

public function getConfigValue($section_group_field)
{
    return $this->scopeConfig->getValue(
        $section_group_field,
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE
    );
}

In your phtml file

if($block->getConfigValue("mymodule/general/enable")){
    // your code
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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