0

I created a custom module and installed the endroid/qrcode library in my module using Composer. When I try to use that library, I get the following error.

Error: Class 'Endroid\QrCode\QrCode' not found in Drupal\shopexp\Plugin\Block\ShopExp->build() (line 25 of modules\custom\shopexp\src\Plugin\Block\ShopExp.php).

The code using the library is the following one.

namespace Drupal\shopexp\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Endroid\QrCode\QrCode;

class ShopExp extends BlockBase {    
  public function createpr() {
      $qr = QrCode::create("https://site.com");
  }  
  public function build() {
    $qr = new QrCode();
    return [
      '#markup' => 'test msg',
    ];
  }
}

The composer.json file used by the module is the following one.

{
    "require": {
        "endroid/qr-code": "^2.5"
    }
}

The folder structure is the following one.

screenshot

4
  • Why not use the existing module for that rather than write your own code? And if it doesn't do exactly what you want then maybe help out with that module. drupal.org/project/endroid_qr_code
    – anonymous
    Sep 26 at 2:39
  • @anonymous Actually i tried to reduce the module count and tried to include other functionalities in one module
    – ArK
    Sep 26 at 2:40
  • That's faulty logic. What's wrong with many modules? It's still the same number of lines of code, no matter where you put them. Regardless, IMO you have the wrong approach. Collaborate and make the existing code better for everyone, don't just solve your own problem.
    – anonymous
    Sep 26 at 2:45
  • @anonymous i understand your point. But problem is where i did wrong using a php library to use inside a block after its included via composer
    – ArK
    Sep 26 at 2:48
1

You have to composer require endroid/qrcode from the Drupal root directory, not from your custom module directory. The package needs to be installed in the main vendor directory along with all the other packages used by Drupal, otherwise the class loader can't find it.

2
  • if include in root composer json means what happens if i use same module to other system. Will installing this module in a other drupal instance will include the dependencies ?
    – ArK
    Sep 26 at 2:51
  • No - if you want Composer to install the dependencies for your module automatically, your module itself needs to be installed by Composer. Unless you use something like the composer merge plugin which might be an option, but that will also require changes to each site's composer.json
    – Clive
    Sep 26 at 9:20

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.