I am creating one custom form and in this form I need to use one service which are there in another module. I need to use that dependency injection based on condition which are there in configuration.
Conditions in Config Form:
- Use Custom1 Form
- Use Custom2 Form
If “Use Custom1 Form” enabled, then I need to use dependency injection.
<?php namespace DrupaltestForm; use DrupalCoreFormConfigFormBase; use DrupalCoreFormFormStateInterface; use SymphonyComponentDependencyInjectionContainerInterface; use Drupalcustom_form1ServicesTestService; /** * Test forms. */ class TestForm extends ConfigFormBase { /** * PinCodeRegistration Object. * * @var Drupalcustom_form1ServicesTestService; */ protected $TestService; /** * Constructs a new pin code multistep form. * * @param Drupalcustom_form1ServicesTestService $test_service * The service handler. */ public function __construct(TestService $test_service) { $this->TestService = $test_service; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static( $container->get('custom_form1.form1_services') ); } }
How we will add "use Drupalcustom_form1ServicesTestService;" based on conditions.
If "custom_form1" module is uninstalled, then we are getting error. Any Idea how we need to avoid error and added dependency based on conditions?