4

I got stuck with our custom libraries/modules and Drupal9.

Installed: Drupal 9.2.7 & Libraries API 3.0-beta1.

We have Drupal multisite, with 5 dev repositories and 30+ production repositories. Dev repositories should have our own custom git pack of libraries, modules and themes (for developers). Prod repositories will get our custom pack of libraries, modules and themes from "all" sites. For contrib stuff (that will use Dev and Prod repositories) we use Drupal root folders libraries, modules and themes managed and autoloaded by Composer.

- web
-- libraries #contrib libraries managed by composer
-- modules #contrib modules managed by composer
-- themes #contrib themes managed by composer
-- sites
--- all
---- libraries # our custom libraries (prod)
---- modules # our custom modules (prod)
---- themes # our custom themes (prod)
--- dev-repository1
---- files
---- libraries # our custom libraries (dev)
---- modules # our custom modules (dev)
---- themes # our custom themes (dev)
--- prod-repoistory1
---- files

So, basically, develop in dev-repositories, push to our git, than pull it to "all" for production. Modules and themes are working great, but libraries are not recognized.

Each dev should have own instance of libraries but apparently composer isn't best solution for multisite libraries? https://www.drupal.org/project/drupal/issues/3004496

That's why we tried with Libraries API

There is basic module "demo" that works:

modules/demo/demo.info.yml

name: Demo
description: My Drupal 9 demo module.
package: Demo
type: module
core_version_requirement: ^9

modules/demo/demo.routing.yml

demo.demo:
  path: '/demo'
  defaults:
    _controller: '\Drupal\demo\Controller\DemoController::showdemo'
    _title: 'Demo title'
  requirements:
    _access: 'TRUE'

modules/demo/src/Controller/DemoController.php

<?php

namespace Drupal\demo\Controller;

use Drupal\Core\Controller\ControllerBase;

class DemoController extends ControllerBase {

  public function showdemo() {
    return ['#markup' => $this->t("test")];
  }

}

and now we need to create custom (maybe independet of Drupal) PHP library with classes that will this module use.

libraries/demolib/DemoLib.php

<?php

    class DemoLib {
        
        public function showHello() {
            
            return "Hello from library!";
            
        }
        
    }

If I dpm Libraries API function libraries_get_libraries(); in controller I get:

Array
(
    [demolib] => sites/dev-repository1/libraries/demolib
)

but if i try to load it with libraries_load('demolib'); I get:

Array
(
    [loaded] => 
)

Notice: Undefined index: installed in libraries_load() (line 708 of modules/contrib/libraries/libraries.module).

I checked https://www.drupal.org/docs/contributed-modules/libraries-api-8x and multiple tutorials but just can't get it to work, to automatically include and use classes in modules from our custom libraries with Libraries API module. Maybe some definitions are missing or file includes (instead of autoload classes)?

Or is there maybe some better approach to this problem?

0 Answers0