I made 2 custom modules: boutique and paypal. paypal is a dependance of boutique.

I check if paypal is enabled, with the following code.

namespace Drupal\boutique\Form\Multistep;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\paypal\Controller\PaypalController;

abstract class MultistepFormBase extends FormBase {
  protected function saveData() {
    if (\Drupal::moduleHandler()->moduleExists('paypal')) {
      $test = new PaypalController();

    drupal_set_message($this->t('The form has been saved.'));


All is working good, but use Drupal\paypal\Controller\PaypalController; is always be here; if the paypal module is not present, does it create problems? How can I conditionally use use Drupal\paypal\Controller\PaypalController;` only when the module is installed?

  • As side note, on Drupal 8 you cannot have disabled modules as with Drupal 7; they are installed or not. – kiamlaluno Apr 30 '16 at 17:01

You don't have to prevent anything.

The use is just alias information. It doesn't care if the class that you're aliasing doesn't exist. As long as you don't actually use it, it won't fail.


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.