在drupal 8块中显示html表
我在Drupal8中创建了一个模块,用于在块中显示第三方API数据 下面是从该API返回的一些数据在drupal 8块中显示html表,drupal,drupal-8,drupal-modules,drupal-views,Drupal,Drupal 8,Drupal Modules,Drupal Views,我在Drupal8中创建了一个模块,用于在块中显示第三方API数据 下面是从该API返回的一些数据 {"ObjectId":43,"ObjectName":"MEGA MELA","ObjectTitle":"Event Created by API","ObjectDescription":"NEW EVENT BY API","ObjectLabel":"","ObjectTypeId":33,"MaxFieldsExpected":5,"ObjectValueType":null,"Obj
{"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.yml
和rest.php
这里是apihtml.info.yml
content
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
content
<?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,
];
}
}
在这个Drupal块中,所有显示的数据都必须在rest.php
文件的build
函数中返回
这是以表格格式显示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
}
foreach($dec as$d){
?>
您可以创建自定义细枝模板并根据需要显示所有数据。您可以阅读更多信息。您可以创建自定义细枝模板并根据需要显示所有数据。您可以阅读更多信息。使用自定义模板:
1.更改您返回的内容:
return [
'#theme' => 'item_list',
'#items' => $items,
];
return [
'#theme' => 'my_custom_template',
'#content' => $items,
];
在您的apihtml.moduleus hook\u主题中
function apihtml_theme() {
return array(
'my_custom_template' =>[
'variables' => [
'content' => []
]
]);
}
并创建一个自定义小树枝:templates/my custom template.html.twig
{% for data in content %}
...
{% endfor %}
使用自定义模板:
1.更改您返回的内容:
return [
'#theme' => 'item_list',
'#items' => $items,
];
return [
'#theme' => 'my_custom_template',
'#content' => $items,
];
在您的apihtml.moduleus hook\u主题中
function apihtml_theme() {
return array(
'my_custom_template' =>[
'variables' => [
'content' => []
]
]);
}
并创建一个自定义小树枝:templates/my custom template.html.twig
{% for data in content %}
...
{% endfor %}
apihtml.module文件中应该有什么?当我使用您编写的文件时,它不起作用。请在apihtml.module中输入此函数,并在apihtml模块中创建一个新文件夹“templates”,然后将my-custom-template.html.twig放在那里,并清除缓存。我在模块根目录中为twig文件创建了一个目录模板,然后创建了模块根目录中的apithtml.module文件仍然存在apithtml.module文件中应该有什么?当我使用您在其中编写的文件时,该文件不起作用在apithtml.module中输入此函数,在apithtml模块中创建一个新文件夹“templates”,并将my-custom-template.html.twig放在那里,然后清除缓存我创建了一个目录模板然后我在模块根目录中创建了apithtml.module文件