1

I’ve read a lot of topic on this but i can’t figure why it is not working for me.

I’ve built a custom module. It is working fine since return $output; in my controller class is working : I can see in front-end a page with my custom html.

I’ve written a script outside Drupal in order to test. Basically, i connect to a webervice and retrieve all the datas. It’s working fine.

Now, i want to execute that script inside the controller class.

I need to load an external php library to instantiate an object.

In my test php file (outside Drupal) a simple require_once is working.

require_once('nusoap.php');

But it is not working in my controller class.

So, i’ve activated the library module and do :

$path = libraries_get_path('nusoap') . DRUPAL_ROOT . '/modules/contrib/webservices_tr/webservices_dc/src/Controller/nusoap.php';

require($path);

It’s still not working.

Error message indiquactes :

Error: Class 'Drupal\webservices_dc\nusoap_client' not found

I’m sur the file path is correct because if i do this, i can see the content of the file.

echo file_get_contents(DRUPAL_ROOT . '/modules/contrib/webservices_tr/webservices_dc/src/Controller/nusoap.php');
die();

I’ve also tried this :

in nusoap.php (where there is the class i need to include),

namespace Drupal\webservices_dc;

and in module controller class :

use Drupal\webservices_dc\nusoap_client;

But it is still not working.

Finally, i’ve copied the content of the external php library inside the module controller and it is working.

So i’m nearly sure the issue is related to the loading of that external library.

What is the correct way to load external php library (and wait it is fully loaded) in order to use it in a custom module ?

  • Are you using Drupal 7 or Drupal 8? You have tagged Drupal 8, but libraries_get_path() does not exist in Drupal 8. Maybe this is your issue. – Jaypan Jul 20 '17 at 8:28
  • @Jaypan I’m on Drupal 8. Library module is installed and libraries_get_path() doesn’t’ throw error (it is not undefined) and seems to load the file. – Sébastien Gicquel Jul 20 '17 at 8:33
  • Why not use SoapClient instead of nusoap? – Kevin Jul 20 '17 at 11:24
  • @Kevin You’re absolutely right. It was my first intention to use SoapClient. I was able to connect but i wasn’t able to use methods. Since the client was using nusoap on his intranet, i decided to try it and i was able to use methods available. But it would be a better approach to use 'SoapClient'. – Sébastien Gicquel Jul 20 '17 at 12:14
  • 1
    You can still pass calls through to __soapClient instead of call them dynamically, as that doesn't always work depending on the WSDL method involved. – Kevin Jul 20 '17 at 12:15
4

You cannot require classes with require or library hooks. Nor should you be editing external packages.

You are supposed to require external php packages with Composer. Composer will update the autoloader. Then in your class, you can say:

use ExternalPackage\namespace\Foo;

In the header and Foo class is then usable. Furthermore, you can inject it to the constructor with a little more work.

I would advise using SoapClient instead of nusoap as that is very old.

Regarding SoapClient, here is something I solved recently injecting it proper to my class, because that's the same road you're heading down.

Dependency inject a class with values from a config object?

  • Thanx ! I followed your advice, asked more informations from client and i was able to successfully connect and call method with SoapClient inside module controller. – Sébastien Gicquel Jul 20 '17 at 21:53
  • I’ve made a lot of search before asking and it seems that there is a lot of answer on SO indicate to use library module with libraries_get_path or module_load_include('php', 'modulename','filename') Was it the way to include php file with Drupal 7 ? – Sébastien Gicquel Jul 20 '17 at 21:55
  • 1
    It should also be pointed out that, for a ClassName class not using namespaces, the class should be referenced as \ClassName or PHP would look for the class in the currently used namespace, which is why PHP throws the error the OP (a.k.a. @SébastienGicquel) is seeing. – kiamlaluno Jul 21 '17 at 17:50
0

You can use module_load_include like we use it in Drupal7. Put your Php file in you module directory and use

  module_load_include('php', 'modulename','filename');
  • I’ve already tried this and it didn’t work (the class is not found). Maybe i’m doing something wrong. The library should be in the root of the module folder ? – Sébastien Gicquel Jul 20 '17 at 9:48
  • No, this is wrong. – Kevin Jul 20 '17 at 11:32
  • Expanding on that - this is D8 and involves an external php package which will be in the vendor folder, and thus not discoverable by that function. – Kevin Jul 20 '17 at 13:12
-1

You can use this inside controller to load the library

$example = array(
  '#attached' => array(
    'library' => array(
      'module/file',
    ),
  ),
);
  • 5
    #attached is for JS and CSS, not for PHP. – marcvangend Jul 21 '17 at 13:04

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.