2

I am writing a custom drupal module (my first one in my life), and I am trying to load an external library (https://github.com/jwilsson/spotify-web-api-php), and until now I used these methods:

  1. Loaded file array in info file

    files[] = spotify-web-api-php/vendor/autoload.php,spotify-web-api-php/src/Request.php,spotify-web-api-php/src/Session.php,spotify-web-api-php/src/SpotifyWebAPI.php,spotify-web-api-php/src/SpotifyWebAPIException.php

  2. I made this function based on another answers here (Copied the library in libraries folder)

    function blve_spotify_registry_files_alter (&$files, $modules) { if (!class_exists('SpotifyWebAPI\Session')) { $library_path = function_exists('libraries_get_path') ? libraries_get_path('spotify-web-api-php') : 'sites/all/libraries/spotify-web-api-php';

    $files[$library_path . '/vendor/autoload.php'] = array(
      'module' => 'blve_spotify',
      'weight' => 0,
    );
    

    } }

  3. I used require_once but will only work on one function

None of these methods worked, when I made a function to call all the loaded files any of the methods or files I need are loaded, please help me to made it work with the required php files. Thanks in advance

3 Answers 3

1

Use libraries module to load 3rd party dependencies (easier way), or composer manager to load composer package (better way).

Here you can find some details and examples how to start with libraries module.

0

Instead of

files[] = spotify-web-api-php/vendor/autoload.php,spotify-web-api-php/src/Request.php,spotify-web-api-php/src/Session.php,spotify-web-api-php/src/SpotifyWebAPI.php,spotify-web-api-php/src/SpotifyWebAPIException.php

Try:

files[] = spotify-web-api-php/vendor/autoload.php
files[] = spotify-web-api-php/src/Request.php
files[] = spotify-web-api-php/src/Session.php
files[] = spotify-web-api-php/src/SpotifyWebAPI.php,
files[] = spotify-web-api-php/src/SpotifyWebAPIException.php
2
0

in Drupal 7 i used to do a quick & dirty way, surely it's not recommended, but perhaps can help you or give you inspiration:

In the following example i load a class called Carbon

  1. create a function, i.e. : _get_carbon()

function _get_carbon() {

$carbon_path = function_exists('libraries_get_path') ? libraries_get_path('Carbon') : 'sites/all/libraries/Carbon';
$c = $carbon_path . '/src/Carbon/Carbon.php';
require($c);
$GLOBALS['carbon'] = new Carbon\Carbon();

}

  1. everytime you need this class, call that function _get_carbon(), you'll find the class at $GLOBALS['carbon']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.