I am trying to extend the menu_block
module to allow it to work with domains (via the domain
module). In order to do this I am extending the MenuBlock
class, and trying to access the domain.negotiator
service from within getDerivativeID()
. I am using the create()
method to inject the service and store it to a $domainNegotiator
property as follows:
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->domainNegotiator = $container->get('domain.negotiator');
return $instance;
}
When I attempt to call $this->domainNegotiator
from within getDerivativeID()
I get an error stating the property doesn't exist and I therefore cannot access methods on it. In debugging I was able to trace the issue to MenuBlock::defaultConfiguration()
calling $this->getDerivativeID()
.
Default configuration is set in __construct()
inside BlockPluginTrait
(called in BlockBase
), so ultimately it boils up to being called in my class's create()
method.
I am able to work around this issue by using a non-injected version of the service in my getDerivativeID()
method, however this isn't a great practice:
$active_domain = \Drupal::service('domain.negotiator')->getActiveDomain();
Is there a way to inject dependencies so they are available before calling parent::create()
in my class's create()
method?