1

How could I create a simple module to Magento 2 with admin configuration, like Porto theme or Amasty modules?

Store > Configuration

  • Are you asking how to make your module's configuration in store->configuration? – Vivek Kumar Apr 8 '17 at 4:34
  • @Neo yes, I added on the description. – Rafael Corrêa Gomes Apr 8 '17 at 4:35
5

In Your Custome Module system.xml looks like This

<?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>
        <tab id="yourid" translate="label" sortOrder="10">
            <label>YourModuleName</label>
        </tab>
        <section id="helloworld" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>Your Tab Label</label>
            <tab>tabname</tab>
            <resource>your_modulename::yourmodule_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>Module Enable</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="display_text" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Display Text</label>
                    <comment>This text will display on the frontend.</comment>
                </field>
            </group>
        </section>
    </system>
</config>

Flush Magento Cache

bin/magento cache:flush

Getting Values Of Configuration

Create Helper Class

<?php

namespace Your\namespace\Helper;

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

class Data extends AbstractHelper
{
    protected $storeManager;
    protected $objectManager;

    const XML_PATH_HELLOWORLD = 'helloworld/';



    public function __construct(Context $context,
        ObjectManagerInterface $objectManager,
        StoreManagerInterface $storeManager
    ) {
        $this->objectManager = $objectManager;
        $this->storeManager  = $storeManager;
        parent::__construct($context);
    }

    public function getConfigValue($field, $storeId = null)
    {
        return $this->scopeConfig->getValue(
            $field, ScopeInterface::SCOPE_STORE, $storeId
        );
    }


    public function getGeneralConfig($code, $storeId = null)
    {
        return $this->getConfigValue(self::XML_PATH_HELLOWORLD . $code, $storeId);
    }


}

Getting Values Of Configuration Using Helper Class

$helper = $this->objectManager->create('Your\namespace\Helper\Data');
echo $helper->getGeneralConfig('enable');
echo $helper->getGeneralConfig('display_text');

Magento2 system.xml provides below fields type

checkbox,
checkboxes,
column,
date,
editablemultiselect,
editor,
fieldset,
file,
gallery,
hidden,
image,
imagefile,
label,
link,
multiline,
multiselect,
note,
obscure,
password,
radio,
radios,
reset,
select,
submit,
text,
textarea,
time

You Can Find More About system.xml Here

| improve this answer | |
0

When you have to make configuration in admin you have to make a system.xml in your module's etc/adminhtml and a config.xml to define their default values.

You can read more on this in this tutorial..

| improve this answer | |
0

I was having hard time using

return $this->scopeConfig->getValue($field, ScopeInterface::SCOPE_STORE,$storeId);

in my helper that extended AbstractHelper.

Was getting error in

/var/log/httpd/error_log.log

PHP Fatal error:  Uncaught Error: Call to a member function getValue() on null in /var/www/html/myproject/app/code/My/Customer/Helper/Data.php:170

Until I used the following

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $val = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue($config_path);

where $config_path was something like dev/debug/template_hints

Hope this helps someone like me.

| improve this answer | |

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.