定制 dropmin/drupal-bridge 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dropmin/drupal-bridge

最新稳定版本:1.0.3

Composer 安装命令:

composer require dropmin/drupal-bridge

包简介

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

README 文档

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 use the bridge as a service:

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);
  }

}

You can also define a Drupal service in your services.yaml file like this:

services:
    Drupal\Core\Entity\EntityTypeManagerInterface:
        class: Drupal\Core\Entity\EntityTypeManagerInterface
        factory: ['@Dropmin\DrupalBridge', 'getService']
        arguments: ['entity_type.manager']

Once added, you can then directly use it instead of loading from the bridge:

class MyService {

  public function __construct(
    protected \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
  ) {}

  public function doSomething(): void {
    $storage = $this->entityTypeManager->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

统计信息

  • 总下载量: 6
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-20