0

I'm kind of making my first baby-steps into Magento2. I'm trying to migrate a Magento 1 module to Magento 2. I need to be able to get the configuration values, and read/edit them from my module. I created a plugin, using this on di.xml file:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\App\Config\ScopeConfigInterface">
        <plugin name="MyCompany_MyModule_config_override_plugin" type="\MyCompany\MyModule\Plugin\Config" sortOrder="1" disabled="false"/>
    </type>
</config>

And this is on Plugin/Config.php:

<?php
namespace MyCompany\MyModule\Plugin;

class Config
{
    protected $logger;

    public function __construct(
        \Psr\Log\LoggerInterface $loggerInterface
    ) {
        $this->logger = $loggerInterface;
    }

    public function afterGetValue($path, $scopeType = \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null)
    {
        $this->logger->debug('enter to after event');
        return true;
    }
}

When I use that, I'm getting lots of different issues each time on the frontend, for example this one:

1 exception(s):
Exception #0 (Exception): Notice: unserialize(): Error at offset 0 of 1 bytes in E:\MyCompany\code\magento2\vendor\magento\framework\View\DesignExceptions.php on line 68

I'm seeing on my debug log that the logger is working, here is the info:

[2017-03-29 19:31:56] main.DEBUG: enter to after {"is_exception":false} []
[2017-03-29 19:31:59] main.DEBUG: enter to after {"is_exception":false} []
[2017-03-29 19:32:00] main.DEBUG: enter to after {"is_exception":false} []
[2017-03-29 19:32:00] main.DEBUG: enter to after {"is_exception":false} []
[2017-03-29 19:32:01] main.DEBUG: enter to after {"is_exception":false} []
[2017-03-29 19:32:02] main.DEBUG: enter to after {"is_exception":false} []
[2017-03-29 19:32:02] main.DEBUG: enter to after {"is_exception":false} []
[2017-03-29 19:32:03] main.DEBUG: enter to after {"is_exception":false} []
[2017-03-29 19:32:04] main.DEBUG: enter to after {"is_exception":false} []
[2017-03-29 19:32:04] main.DEBUG: enter to after {"is_exception":false} []

Am I making the right approach to the task? My final goal is to be able to intercept the config options and being able to modify them from my plugin. Thanks in advance!

2
  1. your return true in afterGetValue is wrong. config values are not only boolean.
  2. afterGetValue declaration is wrong. Should be:

    public function afterGetValue(\Magento\Framework\App\Config\ScopeConfigInterface $scope, $result)
    {
      # $result is config value, you can modify it to your likings.
      $this->logger->debug('value= '.$result);
      return $result;
    }
    
  • Thank you very much Konstantin!!! It completely solves my problem! – Matias Delgado Mar 29 '17 at 20:25
  • you are very welcome sir – Konstantin Gerasimov Mar 29 '17 at 20:27

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.