Magento – Add new field in configuration Magento2

adminmagento2magento2-dev-betamodule

I try create custom module config in magento2. This code add field productzoom

Source file: Xyaddons\Productzoom\etc\adminhtml\system.xml.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Config/etc/system_file.xsd">
    <system>
         <tab id="catalog" translate="label" sortOrder="200">
            <label>Catalog</label>
        </tab>
        <section id="catalog" translate="label" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>Catalog</label>
            <tab>catalog</tab>
            <resource>Magento_Catalog::config_catalog</resource>
            <group id="productzoom" translate="label" type="text" sortOrder="200" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Xyaddons Productzoom(Product views page)</label>
                <field id="flat_xyaddons_productzoom" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Active</label>
                   <!-- <backend_model>Magento\Catalog\Model\Indexer\Category\Flat\System\Config\Mode</backend_model> -->
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="list_zoom_style" translate="label" type="select" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Set style zoom</label>
                    <source_model>Xyaddons\Productzoom\Model\Config\Source\Productzoom</source_model>
                </field>                
            </group>
        </section>
    </system>
</config> 

source File Productzoom.php : Xyaddons\Productzoom\Model\Config\Source\Productzoom

<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Xyaddons\Productzoom\Model\Config\Source;

class Productzoom implements \Magento\Framework\Option\ArrayInterface
{
    /**
     * {@inheritdoc}
     *
     * @codeCoverageIgnore
     */
    public function toOptionArray()
    {
        return [
            ['value' => 'tints', 'label' => __('Tints')],
            ['value' => 'easing', 'label' => __('Easing')]
        ];
    }
}

how do get value in frontend?

Best Answer

/** @var \Magento\Framework\App\Config\ScopeConfigInterface */
protected $scopeConfig;

/**
 * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig    
 */
public function __construct(
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
) {
    $this->scopeConfig = $scopeConfig;
}

/**
 * Fetch config value for above case
 * @return string
 */
public function getCustomConfigValue()
{
    return $this->scopeConfig->getValue('catalog/productzoom/flat_xyaddons_productzoom');
}

In your .phtml file, call:

echo $block->getCustomConfigValue();
Related Topic