dropmin/drupal-bridge

Provides a service class for directly using the service container of an existing Drupal site

1.0.1 2025-05-20 20:23 UTC

This package is not auto-updated.

Last update: 2025-06-18 17:06:51 UTC


README

This package provides a service class for directly using the service container of an existing Drupal site inside a PHP / Symfony application.

Installation

Add this package as dependency to your application codebase:

composer require dropmin/drupal-bridge

You need to specify the app root folder to your existing Drupal installation. The app root is the directory that contains Drupal's index.php file.

In case you have a Symfony-based app, it can be specified by adding following entry into your services.yaml file:

services:
    Dropmin\DrupalBridge:
       class: \Dropmin\DrupalBridge
       arguments:
          $drupalAppRoot: "/absolute/path/to/your-drupal-project/web"

For better DX you may add your separate Drupal directory to your IDE for discovery of Drupal code. Here is an example entry to add into settings.json for Intelephense in VS Code:

"intelephense.environment.includePaths": [
  "/absolute/path/to/your-drupal-project"
]

Usage

Using autowire, you can just directly get the service with dependency injection:

class MyService {

  public function __construct(protected \Dropmin\DrupalBridge $bridge) {}

  public function doSomething(): void {
    $storage = $this->bridge->getEntityTypeManager()->getStorage('node');
    $nids = $storage->getQuery()->accessCheck(FALSE)->execute();
    $nodes = $storage->loadMultiple($nids);
  }

}

Service configuration

You have some options available how the bridge should be initialized. Following constructor arguments are available:

$drupalAppRoot: This is the mandatory string that must resolve to the absolute path to your Drupal web application (usually the /web sub-folder of the Drupal project)

$isolatedMode: A boolean indicating how the request should be initialized. The isolated mode will create a "fake" request without any request header or session data that is passed to your Symfony app. By default this is set to false, which means that the request object of your Symfony app is shared directly with the Drupal kernel. Sharing the same request object allows the Drupal kernel to initialize a user session and other flexibility, whereas the isolated mode may ensure that the request of the your app has less ways to influence the Drupal instance.

$exceptionOnMaintenance: A boolean indicating whether a \Dropmin\DrupalMaintenanceModeException should be automatically thrown when trying to boot the Drupal kernel while the Drupal instance is in maintenance mode. You can also directly check, whether the site is in maintenance mode by calling ::maintenanceMode().

$requestStack: The "@request_stack" service. It is passed by default if available, but you could also set it to null if you don't want the bridge to be initialized using the same request object that is used by your app. The bridge would then initialize a new request object by given global variables.

$request: An optional request object to be used by the Drupal kernel, instead of any options provided above. Alternatively - before the bridge has been used in any way, you may also call ::setRequest with the request object of your choice to be handled by the Drupal kernel.

Support this package

If you benefit from my work, please consider a donation. It helps me continue maintaining and contributing to projects like this one in my free time.

Donate via Paypal