7

I am using composer for managing dependencies in my drupal module. I specified my dependency in the composer.json file placed in my module's root directory. After running the composer install comand it installs all the dependencies correctly; however, the autoloading does not seem to work.

I have checked several threads here that deal with the problem and found that most of them suggest using the composer manager module that's available even though on the module's page on drupal.org it says that it's deprecated due to Drupal's improved use of composer since version 8.1

Here's my composer.json file in my module's root diretory:

{
"name": "activit/facebook_ratings",
"description": "Display a feed of your Facebook fanpage's ratings on your website.",
"type": "drupal-module",
"require": {
    "facebook/graph-sdk": "^5.4"
},
"license": "MIT",
"authors": [
    {
        "name": "Robert Balasko",
        "email": "rbalasko@activit.sk"
    }
]
}

and a factory class in the root of src directory that tries to load a class from the graph-sdk.

<?php namespace Drupal\facebook_ratings;


use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Facebook\Facebook;

class FacebookConnectorFactory {

  private $logger_factory;

  public function __construct(LoggerChannelFactoryInterface $logger_factory) {
    $this->logger_factory = $logger_factory;
  }

  public function getFacebookService($app_id, $app_secret) {
    if (!$app_id || !$app_secret) {
      $this->logger_factory
        ->get('facebook_ratings')
        ->error('Undefined App ID or App secret');

      return FALSE;
    }

    return new Facebook([
      'app_id' => $app_id,
      'app_secret' => $app_secret,
      'default_graph_version' => 'v2.6',
    ]);
  }

}

The result is a class not found error when I run my code. Shouldn't the dependency be autoloaded or am I doing anything wrong?

Thanks a lot.

7

Composer Manager is just a small wrapper around the Merge Plugin which we have in core nowadays.

Register your module in the root composer.json of your project, look for extra > merge-plugin > include. It should look like this:

"extra": {
    "_readme": [
        "By default Drupal loads the autoloader from ./vendor/autoload.php.",
        "To change the autoloader you can edit ./autoload.php."
    ],
    "merge-plugin": {
        "include": [
            "core/composer.json",
            "modules/facebook_ratings/composer.json"
        ],
        "recurse": false,
        "replace": false,
        "merge-extra": false
    }
}

modules/facebook_ratings/composer.json is the path to your composer.json. It depends on where you have your module.

Run composer install afterwards.

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.