Drupal 通过 HttpClient Service 从外部导入字段内容

在Drupal中,从外部API接口获取字段值并将其用于内容类型或实体,是通过编程实现的。这通常涉及到自定义模块的开发,其中包含了向外部API发起请求、处理响应和更新Drupal实体字段的逻辑。以下是一个基本的实现流程:

1. 创建自定义模块

您需要创建一个自定义模块来包含您的逻辑代码。如果您不熟悉如何创建模块,在Drupal的官方文档中有详细的指南。

2. 使用Drupal的HTTP客户端服务

Drupal 8及以上版本内置了HttpClient服务,使得从PHP代码向外部服务发起HTTP请求变得简单。您可以使用这个服务来调用外部API并获取数据。

示例代码

以下是一个示例代码,展示了如何在自定义模块中使用Drupal的HTTP客户端服务从外部API获取数据,并将其存储在一个节点字段中:

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use GuzzleHttp\ClientInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class MyCustomService {

  protected $httpClient;
  protected $entityTypeManager;
  protected $logger;

  public function __construct(ClientInterface $http_client, EntityTypeManagerInterface $entity_type_manager, LoggerChannelFactoryInterface $logger_factory) {
    $this->httpClient = $http_client;
    $this->entityTypeManager = $entity_type_manager;
    $this->logger = $logger_factory->get('my_custom_service');
  }

  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('http_client'),
      $container->get('entity_type.manager'),
      $container->get('logger.factory')
    );
  }

  public function updateNodeFieldFromApi($node_id, $field_name, $api_url) {
    try {
      // 发起HTTP GET请求
      $response = $this->httpClient->request('GET', $api_url);
      if ($response->getStatusCode() == 200) {
        $data = json_decode($response->getBody());

        // 假设API响应包含我们需要的字段值
        $field_value = $data->some_field;

        // 加载并更新节点
        $node = $this->entityTypeManager->getStorage('node')->load($node_id);
        if ($node) {
          $node->set($field_name, $field_value);
          $node->save();
          $this->logger->info('Node updated from API: @nid', ['@nid' => $node_id]);
        }
      }
    } catch (\Exception $e) {
      $this->logger->error('Failed to update node from API: @message', ['@message' => $e->getMessage()]);
    }
  }
}

3. 调用自定义服务

您可以在模块的其他部分调用这个服务,比如在一个表单的提交处理器、计划任务或其他适合的地方,根据您的需求来触发这个数据更新流程。

注意事项