Score:0

Why isn't the Endroid\QrCode\QrCode found?

cn flag
ArK

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

fr flag
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. https://www.drupal.org/project/endroid_qr_code
ArK avatar
cn flag
ArK
@anonymous Actually i tried to reduce the module count and tried to include other functionalities in one module
fr flag
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.
ArK avatar
cn flag
ArK
@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
Score:1
fr flag

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.

ArK avatar
cn flag
ArK
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 ?
cn flag
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
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.