1

I have an extension "redirection after customer login/reg/logout", it works on Magento v2.2.1 but when I set up on 2.3.2 so not work :(. issue not saving form value system.xml to core_config_data. help me. thank!

source: http://upfile.vn/6qXmF5BmN~FQ/magehit-redirectionformagento2v1-0-1-zip.html

system.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Config/etc/system_file.xsd">
    <system>
        <section id="customredirectionsection" translate="label" type="text" sortOrder="140" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>Custom Redirection</label>
            <tab>v2agency</tab>
            <resource>V2Agency_Redirection::customredirectionsection</resource>

            <group id="general" translate="label" type="text" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>General</label>
                <field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enabled</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="login_redirection_grp_enable" translate="label" type="select" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enabled Login Redirection</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                    <depends>
                        <field id="active">1</field>
                    </depends>
                </field>
                <field id="register_redirection_grp_active" translate="label" type="select" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enabled Registration Redirection</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                    <depends>
                        <field id="active">1</field>
                    </depends>
                </field>
                <field id="logout_redirection_active" translate="label" type="select" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enabled Logout Redirection</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                    <depends>
                        <field id="active">1</field>
                    </depends>
                </field>
                <field id="logout_redirection_url" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Path Redirect After Logout</label>
                    <comment><![CDATA[<em>Leave empty if you want to default redirect</em>]]></comment>
                    <depends>
                        <field id="active">1</field>
                        <field id="logout_redirection_active">1</field>
                    </depends>
                </field>
            </group>

            <group id="v2_login_redirection_grp" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Customer Login</label>
                <field id="login_redirection" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Login Redirection</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <comment><![CDATA[<em>Leave empty if you want to default redirect</em>]]></comment>
            </group>

            <group id="v2_register_redirection_grp" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Customer Registration</label>
                <field id="register_redirection" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Registration Redirection</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <comment><![CDATA[<em>Leave empty if you want to default redirect</em>]]></comment>
            </group>
        </section>
    </system>
</config>

Module\Block\System\Config\Form.php

<?php
namespace V2Agency\Redirection\Block\System\Config;
class Form extends \Magento\Config\Block\System\Config\Form
{


    const SCOPE_DEFAULT = 'default';

    const SCOPE_WEBSITES = 'websites';

    const SCOPE_STORES = 'stores';

    /**
     * Config data array
     *
     * @var array
     */
    protected $_configData;

    /**
     * Backend config data instance
     *
     * @var \Magento\Config\Model\Config
     */
    protected $_configDataObject;

    /**
     * Default fieldset rendering block
     *
     * @var \Magento\Config\Block\System\Config\Form\Fieldset
     */
    protected $_fieldsetRenderer;

    /**
     * Default field rendering block
     *
     * @var \Magento\Config\Block\System\Config\Form\Field
     */
    protected $_fieldRenderer;

    /**
     * List of fieldset
     *
     * @var array
     */
    protected $_fieldsets = [];

    /**
     * Translated scope labels
     *
     * @var array
     */
    protected $_scopeLabels = [];

    /**
     * Backend Config model factory
     *
     * @var \Magento\Config\Model\Config\Factory
     */
    protected $_configFactory;

    /**
     * Magento\Framework\Data\FormFactory
     *
     * @var \Magento\Framework\Data\FormFactory
     */
    protected $_formFactory;

    /**
     * System config structure
     *
     * @var \Magento\Config\Model\Config\Structure
     */
    protected $_configStructure;

    /**
     *Form fieldset factory
     *
     * @var \Magento\Config\Block\System\Config\Form\Fieldset\Factory
     */
    protected $_fieldsetFactory;

    /**
     * Form field factory
     *
     * @var \Magento\Config\Block\System\Config\Form\Field\Factory
     */
    protected $_fieldFactory;

    /**
     * @var SettingChecker
     */
    private $settingChecker;

    /**
     * @var DeploymentConfig
     */
    private $appConfig;

    public function initFields(
        \Magento\Framework\Data\Form\Element\Fieldset $fieldset,
        \Magento\Config\Model\Config\Structure\Element\Group $group,
        \Magento\Config\Model\Config\Structure\Element\Section $section,
        $fieldPrefix = '',
        $labelPrefix = ''
    ) {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/tester.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);

        if (!$this->_configDataObject) {
            $this->_initObjects();
        }
        // Extends for config data
        $extraConfigGroups = [];
        foreach ($group->getChildren() as $element) {
            if ($element instanceof \Magento\Config\Model\Config\Structure\Element\Group) {
                $this->_initGroup($element, $section, $fieldset);
            } else {
                // $logger->info('case 2'); --!
                $path = $element->getConfigPath() ?: $element->getPath($fieldPrefix);
                //$logger->info($path);
                if ($element->getSectionId() != $section->getId()) {
                    $groupPath = $element->getGroupPath();
                    if (!isset($extraConfigGroups[$groupPath])) {
                        $this->_configData = $this->_configDataObject->extendConfig(
                            $groupPath,
                            false,
                            $this->_configData
                        );
                        $extraConfigGroups[$groupPath] = true;
                    }
                }
                if($group->getId() == 'v2_login_redirection_grp' || $group->getId() =='v2_register_redirection_grp'){
                    $this->_initCustomElement($element, $fieldset, $path, $fieldPrefix, $labelPrefix);
                }else{
                    $this->_initElement($element, $fieldset, $path, $fieldPrefix, $labelPrefix);
                }
            }
        }
        return $this;
    }

    protected function _initCustomElement(
        \Magento\Config\Model\Config\Structure\Element\Field $field,
        \Magento\Framework\Data\Form\Element\Fieldset $fieldset,
        $path,
        $fieldPrefix = '',
        $labelPrefix = ''
    ) {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/tester.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);

        $inherit = true;
        $data = null;
        $fieldRendererClass = $field->getFrontendModel();
        if ($fieldRendererClass) {
            // $logger->info('case 1');
            $fieldRenderer = $this->_layout->getBlockSingleton($fieldRendererClass);
        } else {
            //  $logger->info('case 2'); !--
            $fieldRenderer = $this->_fieldRenderer;
        }

        $fieldRenderer->setConfigData($this->_configData);

        $elementId = $this->_generateElementId($field->getPath($fieldPrefix));
        $dependencies = $field->getDependencies($fieldPrefix, $this->getStoreCode());

        $sharedClass = $this->_getSharedCssClass($field);
        $requiresClass = $this->_getRequiresCssClass($field, $fieldPrefix);

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerGroups = $objectManager->get('\Magento\Customer\Model\ResourceModel\Group\Collection')->toOptionArray();

        $configLoader = $objectManager->get(\Magento\Framework\App\Config\Storage\WriterInterface::class);

        foreach($customerGroups as  $customerGroup){
            $customPath = '_' . $customerGroup['value'];
            $elementId = $elementId . $customPath;
            $elementName = $this->_generateElementName($field->getPath() . $customPath , $fieldPrefix);
            //$logger->info('ele '. $elementName);
            $this->_populateDependenciesBlock($dependencies, $elementId, $elementName);
            $customPath = $path . $customPath;
            //$logger->info('custompath '. $customPath);
            if (array_key_exists($customPath, $this->_configData)) {
                $logger->info('case1');
                $data = $this->_configData[$customPath];
                $inherit = false;
                if ($field->hasBackendModel()) {
                    //$logger->info('case2');
                    $backendModel = $field->getBackendModel();
                    $backendModel->setPath($customPath)
                        ->setValue($data)
                        ->setWebsite($this->getWebsiteCode())
                        ->setStore($this->getStoreCode())
                        ->afterLoad();
                    $data = $backendModel->getValue();
                }
            }elseif ($field->getConfigPath() !== null) {
                //$logger->info('case3');
                $data = $this->getConfigValue($field->getConfigPath());
            } else {
                $logger->info('case4');
                $data = $this->getConfigValue($customPath);
            } 

            //$configLoader->save($customPath,  $data, $scope = $this->getScope(), $scopeId = $this->getScopeId());

            $formField = $fieldset->addField(
                $elementId ,
                'text',
                [
                    'name' => $elementName,
                    'label' => $customerGroup['label'],
                    'hint' => $field->getHint(),
                    'value' => $data,
                    'inherit' => $inherit,
                    'class' => $field->getFrontendClass() . $sharedClass . $requiresClass,
                    'field_config' => $field->getData(),
                    'scope' => $this->getScope(),
                    'scope_id' => $this->getScopeId(),
                    'scope_label' => $this->getScopeLabel($field),
                    'can_use_default_value' => $this->canUseDefaultValue($field->showInDefault()),
                    'can_use_website_value' => $this->canUseWebsiteValue($field->showInWebsite()),
                    'can_restore_to_default' => $this->isCanRestoreToDefault($field->canRestore())
                ]
            );


            $field->populateInput($formField);

            if ($field->hasValidation()) {
                $formField->addClass($field->getValidation());
            }
            if ($field->getType() == 'multiselect') {
                $formField->setCanBeEmpty($field->canBeEmpty());
            }
            if ($field->hasOptions()) {
                $formField->setValues($field->getOptions());
            }
            $formField->setRenderer($fieldRenderer);
        }


    }
}

form extend core: 2.2.1 https://notepad.vn/share/nkflfno15

form extend core: 2.3.2 https://notepad.vn/share/jelmupt00

di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference for="Magento\Config\Block\System\Config\Form" type="V2Agency\Redirection\Block\System\Config\Form" />
</config>

backend

enter image description here

General is Ok, but Customer Login, Customer Registration not save to core_config_data table, !

  • if you can post your system.xml file, it will be easier for people to help – fmsthird Aug 8 '19 at 3:15
  • i was edited. please help me..thank!! – duysexy Aug 8 '19 at 3:36
  • you have not defined tab id in system.xml like this <tab id="v2agency" translate="label" sortOrder="10"> <label>v2agency</label> </tab> – Mohit Rane Aug 8 '19 at 4:43
  • @Mohit Rane i defined it in menu.xml . this is not my problem – duysexy Aug 8 '19 at 6:02
  • everything seems fine, but when installing it on version 2.3.1, in the backend image, Customer Login, Customer Registration is not possible to save the values ​​to core_config_data – duysexy Aug 8 '19 at 6:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.