【问题标题】:Drupal 8 Custom Module - From dynamic data (API) to store at Fields (Content type) Drupal DBDrupal 8 自定义模块 - 从动态数据 (API) 到存储在字段(内容类型) Drupal DB
【发布时间】:2020-07-10 05:09:45
【问题描述】:

我在 Drupal 8 中创建了一个自定义模块,该模块从 API 中获取一些数据,并将它们放入 Drupal DB 创建一个新表。 我想将此数据添加为特定内容类型的内容。

我该怎么做?

这是我的代码:

<?php

/**
 * Implements hook_cron().
 */
function ods_cron() {
  $message = 'Cron run: ' . date('Y-m-d H:i:s');

  $ods  = \Drupal::service('ods.ods');
  $conf = \Drupal::service('ods.ods_configuration_request');

  if ($conf->isDevelopment()) {
    // Development
    $response_bond = beforeSendRequest($conf->devUrlExternalBond(), 'GET');
    $response_mf = beforeSendRequest($conf->devUrlExternalMutualFund(), 'GET');
  } else {
    // Production
    $parameters_bond = [
      'headers' => $conf->headers(),
      'authorization' => $conf->basicAuthorization(),
      'data_post' => $conf->bodyBond(),
    ];
    $parameters_mf = [
      'headers' => $conf->headers(),
      'authorization' => $conf->basicAuthorization(),
      'data_post' => $conf->bodyMutualFund(),
    ];
    $response_bond = beforeSendRequest($conf->urlExternalBond(), 'POST', $parameters_bond);
    $response_mf = beforeSendRequest($conf->urlExternalMutualFund(), 'POST', $parameters_mf);
  }

  $raw_result_bond = json_decode($response_bond);
  $raw_result_mf = json_decode($response_mf);

  // Development
  if ($conf->isDevelopment()) {
    $raw_result_bond = json_decode($raw_result_bond[0]->field_bonds);
    $raw_result_mf = json_decode($raw_result_mf[0]->field_api);
  }

  $BondsProductList = $raw_result_bond->BondsProductInqRs->BondsProductList;
  $MFProductInqList = $raw_result_mf->MFProductInqRs->MFProductInqList;

  // Bond data store to internal
  if ($BondsProductList !==  null) {
    $bond_datas = [];
    foreach ($BondsProductList as $row => $content) {
      $bond_datas[] = [
        'AskPrice' => number_format($content->AskPrice, 1, '.', ','),
        'BidPrice' => number_format($content->BidPrice, 1, '.', ','),
        'BuySettle' => number_format($content->BuySettle, 1, '.', ','),
        'CouponFreqCode' => $content->CouponFreqCode,
        'CouponFreqID' => number_format($content->CouponFreqID),
        'CouponRate' => number_format($content->CouponRate, 2, '.', ','),
        'IDCurrency' => $content->IDCurrency,
        'LastCoupon' => $content->LastCoupon,
        'MaturityDate' => $content->MaturityDate,
        'MinimumBuyUnit' => number_format($content->MinimumBuyUnit),
        'MultipleOfUnit' => number_format($content->MultipleOfUnit),
        'NextCoupon' => $content->NextCoupon,
        'Penerbit' => $content->Penerbit,
        'ProductCode' => $content->ProductCode,
        'ProductName' => $content->ProductName,
        'ProductAlias' => $content->ProductAlias,
        'RiskProfile' => $content->RiskProfile,
        'SellSettle' => $content->SellSettle
      ];
    }

    $insert_data = $ods->setData(
      'bond',
      [
        'AskPrice', 'BidPrice', 'BuySettle', 'CouponFreqCode', 'CouponFreqID', 'CouponRate', 'IDCurrency',
        'LastCoupon', 'MaturityDate', 'MinimumBuyUnit', 'MultipleOfUnit', 'NextCoupon', 'Penerbit',
        'ProductCode', 'ProductName', 'ProductAlias', 'RiskProfile', 'SellSettle'
      ],
      $bond_datas
    );

    if ($insert_data) {
      // make response as JSON File and store the file
      $ods->makeJsonFile($bond_datas, 'feeds/bonds', 'bond.json');
    }

  }

  // Mutual Fund data store to internal
  if ($MFProductInqList !==  null) {
    $mf_datas = [];
    foreach ($MFProductInqList as $row => $content) {
      $mf_datas[] = [
        'ProductCode' => $content->ProductCode,
        'ProductName' => $content->ProductName,
        'ProductCategory' => $content->ProductCategory,
        'ProductType' => $content->ProductType,
        'Currency' => $content->Currency,
        'Performance1' => $content->field_1_tahun_mf,
        'Performance2' => $content->Performance2,
        'Performance3' => $content->Performance3,
        'Performance4' => $content->Performance4,
        'Performance5' => $content->Performance5,
        'UrlProspektus' => $content->UrlProspektus,
        'UrlFactSheet' => $content->UrlFactSheet,
        'UrlProductFeatureDocument' => $content->UrlProductFeatureDocument,
        'RiskProfile' => $content->RiskProfile,
        'FundHouseName' => $content->FundHouseName,
        'NAVDate' => $content->NAVDate,
        'NAVValue' => $content->NAVValue
      ];
    }

    $insert_data_mf = $ods->setData(
      'mutual_fund',
      [
        'ProductCode', 'ProductName', 'ProductCategory', 'ProductType', 'Currency', 'Performance1', 'Performance2', 'Performance3',
        'Performance4', 'Performance5', 'UrlProspektus', 'UrlFactSheet', 'UrlProductFeatureDocument', 'RiskProfile', 'FundHouseName',
        'NAVDate', 'NAVValue'
      ],
      $mf_datas
    );

    if ($insert_data_mf) {
      // make response as JSON File and store the file
      $ods->makeJsonFile($mf_datas, 'feeds/mf', 'mutual_fund.json');
    }

  }

  // console log
  \Drupal::logger('ods')->notice($message);
}

那么我可以将数据存储到原始的 drupal 8 表吗?

【问题讨论】:

  • 用这句话:我想把数据放到 Drupal 字段(在内容类型),然后把数据放到 Drupal 原始表中。您的意思是要将此数据添加为特定内容类型的内容吗?
  • 是的@DavideCasiraghi。我可以这样做吗?
  • 您是否已经从 Drupal 后端创建了内容类型?

标签: php drupal drupal-8


【解决方案1】:

首先,您需要在 Drupal 8 后端转到结构 > 内容类型中创建内容类型。

其次,您可以像这样以编程方式添加节点

use Drupal\node\Entity\Node;

$node = Node::create(array(
    'type' => 'your_content_type',
    'title' => 'your title',
    'langcode' => 'en',
    'uid' => '1',
    'status' => 1,
    'body'=> 'your body',
));

$node->save();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 2020-04-03
    • 2017-04-19
    • 1970-01-01
    • 2016-12-15
    • 2011-05-20
    相关资源
    最近更新 更多