在Drupal 8块中显示HTML表

时间:2019-06-10 13:33:12

标签: drupal drupal-8 drupal-modules drupal-views

我在Drupal 8中创建了一个模块,用于在块中显示第三方API数据

这是该API返回的一些数据

{"ObjectId":43,"ObjectName":"MEGA MELA","ObjectTitle":"Event Created by API","ObjectDescription":"NEW EVENT BY API","ObjectLabel":"","ObjectTypeId":33,"MaxFieldsExpected":5,"ObjectValueType":null,"ObjectControlType":"","IsDeleted":true,"CreatedDate":"2019-05-22T07:56:03.767","CreatedBy":null,"EditedDate":null,"EditedBy":null,"DeletedDate":null},{"ObjectId":44,"ObjectName":"Event x11","ObjectTitle":"Event Created by API","ObjectDescription":"NEW EVENT BY API","ObjectLabel":"","ObjectTypeId":33,"MaxFieldsExpected":5,"ObjectValueType":null,"ObjectControlType":"","IsDeleted":true,"CreatedDate":"2019-05-23T00:33:50.7","CreatedBy":null,"EditedDate":null,"EditedBy":null,"DeletedDate":null}]} 

我创建了一个自定义模块,以在一个块中显示某些数据

这是我的模块目录层次结构,模块目录名称为apihtml

 -apihtml 
    -src
      -Plugin
        -Block
          -rest.php   

    -apihtml.info.yml

如您所见,只有两个文件   apihtml.info.ymlrest.php

这是apihtml.info.yml的内容

 name: Third Party Api Data with html
    type: module
    description: 'This is for showing rest data in ui with html'
    package: Custom
    version: 8.x
    core: 8.x
    dependencies:
      - node
      - block

这是rest.php的内容

   <?php

/**
 * @file
 */
namespace Drupal\apihtml\Plugin\Block;
use Drupal\Core\Access\AccessResult;

use Drupal\Core\Block\BlockBase;
use Drupal\Component\Serialization\Json;

/**
 * Creates a 'Foobar' Block
 * @Block(
 * id = "AntShow with html formatting",
 * admin_label = @Translation("Ant Show & HTML"),
 * )
 */
class rest extends BlockBase {
 public function build() {
    /** @var \GuzzleHttp\Client $client */
    $client = \Drupal::service('http_client_factory')->fromOptions([
      'base_uri' => 'http://myApiPath',
    ]);



       $response = $client->get('objects/events');


    $dec = Json::decode($response->getBody());



    $items = [];


foreach ($dec as $d) {
    foreach ($d as $ins) {  
  $items[] = $ins['ObjectName'] ;

    }
}


         return [
      '#theme' => 'item_list',
      '#items' => $items,

    ];

  }
}

在这里,通过使用array Key ObjecName,我可以在块中显示对象名称

这是块OutPut

MEGA MELA
事件x11

但是我想要更多
我想以表格形式显示此API返回的数据,这意味着array key应该是header值的data,数据将在rows

喜欢

   ObjectId  | ObjectName | ObjectTitle           | ObjectDescription | ObjectLabel | ObjectTypeId | MaxFieldsExpected | ObjectValueType | ObjectControlType | IsDeleted | CreatedDate | CreatedBy | EditedDate |  EditedBy
    43        |  MEGA MELA |  Event Created by API | NEW EVENT BY API | ..............................................................................

在该Drupal块中,所有显示的数据都必须在build文件的rest.php函数中返回

这是以表格格式显示api返回的数据的代码

     foreach ($dec as $d) {
  ?>
    <table>
<?php foreach ($d as $ins) { ?>
<tr>
<?php
 foreach ($ins as $key=>$value) {  
   echo "<td><h3>".$key."</h3></td>";
 } ?>
</tr>
<tr>
<?php
 foreach ($ins as $key=>$value) {  
   echo "<td><h3>".$value."</h3></td>";
 } ?>
</tr><?php
} ?>
</table> <?php
}

但是我不知道如何将此代码放在build的{​​{1}} function中以在rest.php中显示表格数据

2 个答案:

答案 0 :(得分:1)

您可以创建自定义Twig模板并根据需要显示所有数据。您可以详细了解here

答案 1 :(得分:1)

使用自定义模板: 1.更改退货:

return [
      '#theme' => 'item_list',
      '#items' => $items,

    ];

return [
        '#theme' => 'my_custom_template',
        '#content' => $items,
      ];

在您的 apihtml.module 中,我们是hook_theme

function  apihtml_theme() {
    return array(
      'my_custom_template' =>[
        'variables' => [
          'content' => []
        ]
      ]);
}

并创建自定义树枝: templates / my-custom-template.html.twig

{% for data in content %}
...
{% endfor %}