4

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.

12

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...

    }
}
1
  • great stuff Khoa, That has helped me out no end, I was just struggling to get the concept, thanks again Feb 10 '17 at 9:23

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.