Question

I have done this in Magento 1 but I am struggling with Magento 2.

I am building a module to observe failed admin logins and email.

My module.xml located in etc/adminhtml/module.xml

 <section id="modzinc_adminmonitor" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
        <label>Modzinc_Adminmonitor</label>
        <tab>modzinc</tab>
        <resource>Modzinc_Adminmonitor::modzinc_adminmonitor</resource>
        <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
            <label>Admin Monitor Settings</label>
            <field id="enable" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Enable Admin Monitor</label>
                <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                <comment>
                    Must be set to yes for any part of this module to work
                </comment>
            </field>
            <field id="notifyEmail" translate="label comment" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Notification Email</label>
                <comment>
                    Where emails will be sent when a monitored event occurs ( Optional if blank no emails will be sent )
                </comment>
            </field>

I want to get the value that has been saved in the field notifyEmail and use it in my adminFail.php

    <?php
namespace Modzinc\Adminmonitor\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;



class AdminFailed implements ObserverInterface {

public function execute( Observer $observer ) {

    $notifyEmail= ???? How do I get this from my module config 

### IF I simply set the variable like this :

### $notifyEmail = 'me@mysite.com'  email works so I know all my other code is fine 




    mail($notifyEmail,'Failed Login','Failed');

}
}

Any help is greatly appreciated.

Was it helpful?

Solution

First of all, the system config admin in Magento 2 has a separate system.xml config. It is put under etc/adminhtml/system.xml

Second, If you want to get the custom value. You need to use \Magento\Framework\App\Config\ScopeConfigInterface - Magento\Framework\App\Config to get the config value:

<?php
namespace Modzinc\Adminmonitor\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;



class AdminFailed implements ObserverInterface {

    const XML_NOTIFY_EMAIL = 'modzinc_adminmonitor/general/enable';

    const XML_NOTIFY_VALUE = 'modzinc_adminmonitor/general/notifyEmail'
    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $scopeConfig;

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


    public function execute( Observer $observer ) {

        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
        $notifyEmail = $this->scopeConfig->getValue(self::XML_NOTIFY_EMAIL, $storeScope);

        //Your custom code...

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top