0

I'm looking for the proper way to reference a model or helper from another extension that might not be installed.

Normally I could just inject the helper or model into the construct such as the following:

namespace Company\Module\Controller\Process;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;

use AnotherCompany\AnotherModule\Helper\Data;


class Index extends Action
{   

    protected $someHelper;

    public function __construct(Context $context, Data $someHelper) 
    {
        $this->someHelper = $someHelper;
        parent::__construct($context);  
    }

    public function execute()
    {

        $this->someHelper->getSomeValue();

    }


}

The problem is I can't guarantee that AnotherCompany\AnotherModule\Helper\Data exists in the installation when I craft the construct injection. Since using ObjectManager directly is considered to be inappropriate what would be the correct way to achieve something as follows? I'm injecting Magento\Framework\Module\Manager to check for the extension being enabled.

namespace Company\Module\Controller\Process;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;

use Magento\Framework\Module\Manager;

class Index extends Action
{   


    public function __construct(Context $context, Manager $moduleManager) 
    {
        $this->moduleManager = $moduleManager;
        parent::__construct($context);  
    }

    public function execute()
    {

        if($this->isModuleEnabled('AnotherCompany_AnotherModule')):

            //Inject helper or model here from AnotherCompany_AnotherModule without using ObjectManager directly

        endif;

    }

    public function isModuleEnabled($moduleName)
    {
        return $this->moduleManager->isEnabled($moduleName);
    }


}
1

First create a Factory which creates this your model. Move your module manager code inside factory. If Dependency is installed then return object or return null. In your controller just check if the factory returned null or the model object

  • After reviewing what I believe you are referring to by the dependency injection (assuming you mean module.xml) It does not I believe address the issue I'm facing. From what I understand of setting the dependency sequence in the module.xml it requires that the defined module be loaded first. Perhaps I wasn't clear enough but I do not know if the second module is installed, but if it is I want to include some of it's functionality in the return but it is not a requirement for the primary module to function. – Eirik May 16 '17 at 17:11
  • No I do not mean in module.xml by dependency I just meant the class you are going to use IF it is available. You do not have to add anything to module.xml. moving the code in factory is enough – Abhishek Jakhotiya May 16 '17 at 17:26

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.