2

I have a problem taking values from a system.xml field to show them in another field.

This is the 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>
        <tab id="tryagain_system" translate="label" sortOrder="0">
            <label>Tryagain module</label>
        </tab>
        <section id="tryagain_system" translate="label" sortOrder="1000" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>Settings</label>
            <tab>tryagain_system</tab>
            <resource>Tryagain_System::config</resource>
            <group id="tryagain_system_group1" translate="label" type="text" sortOrder="1000" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Views</label>
                <field id="active_views" translate="label" type="multiselect" sortOrder="91" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Select store views</label>
                    <source_model>Tryagain\System\Model\Config\Source\ListView\Values</source_model>
                </field>
            </group>
            <group id="fixiamo_dictionary" translate="label" type="text" sortOrder="7000" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Dictionary</label>
                <field id="terms" translate="label" sortOrder="92" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Dictionary</label>

                    <frontend_model>Tryagain\System\Block\Adminhtml\Menu\Field\AdditionalItem</frontend_model>
                    <backend_model>Tryagain\System\Block\Adminhtml\Menu\Config\Backend\AdditionalItem</backend_model>
                </field>
            </group>
        </section>
    </system>
</config>

I would like to take the data selected in the active_views and show them as header of the field terms.

These are the frontend_model and the backend_model

<?php 
 namespace Tryagain\System\Block\Adminhtml\Menu\Field;

  use 

Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;

class AdditionalItem extends AbstractFieldArray
{
protected $_typeblockOptions;
protected $_cmsblockOptions;
/**
 * {@inheritdoc}
 */

protected function _prepareToRender()
{

    $this->addColumn(
        'fromprice',
        [
            'label' => __('From Price'),
            'size' => '200px',
            'class' => 'required-entry' //remove this class if the field is not required
        ]
    );

    $this->addColumn(
        'toprice',
        [
            'label' => __('To Price'),
            'size' => '200px',
            'class' => 'required-entry'
        ]
    );

    $this->addColumn(
        'productcost',
        [
            'label' => __('Product Cost'),
            'size' => '200px',
            'class' => 'required-entry'
        ]
    );

    $this->_addAfter = false;
    $this->_addButtonLabel = __('Add');
} }

create block for backend

<?php

namespace Tryagain\System\Block\Adminhtml\Menu\Config\Backend;

use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Config\Value as ConfigValue;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Model\Context;
use Magento\Framework\Model\ResourceModel\AbstractResource;
use Magento\Framework\Registry;
use Magento\Framework\Serialize\SerializerInterface;

class AdditionalItem extends ConfigValue
{

protected $serializer;

public function __construct(
    SerializerInterface $serializer,
    Context $context,
    Registry $registry,
    ScopeConfigInterface $config,
    TypeListInterface $cacheTypeList,
    AbstractResource $resource = null,
    AbstractDb $resourceCollection = null,
    array $data = []
) {
    $this->serializer = $serializer;
    parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
}

public function beforeSave()
{
    $value = $this->getValue();
    unset($value['__empty']);
    $encodedValue = $this->serializer->serialize($value);

    $this->setValue($encodedValue);
}

protected function _afterLoad()
{
    /** @var string $value */
    $value = $this->getValue();
    if($value) {
        $decodedValue = $this->serializer->unserialize($value);

        $this->setValue($decodedValue);
    }

}    }

So I know that the way to take values from the system is this in a controller

<?php
namespace Vendor\Module\Controller\Index;
use Magento\Store\Model\ScopeInterface;

class Index extends \Magento\Framework\App\Action\Action
{
protected $scopeConfig;

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig)
{
    $this->scopeConfig = $scopeConfig;
    parent::__construct($context);
}

public function execute()
{
    $storeScope = ScopeInterface::SCOPE_STORE;
    $value = $this->scopeConfig->getValue('pathinsystem', $storeScope);

    echo $value;
} }

I also tried a helper

How can I do the same thing in the block and pass the values to terms field

0

Your Answer

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