WSExampleBlockDecoder.php in Web Service Data 2.0.x
File
modules/wsdata_example/src/Plugin/WSDecoder/WSExampleBlockDecoder.php
View source
<?php
namespace Drupal\wsdata_example\Plugin\WSDecoder;
use Drupal\wsdata\Plugin\WSDecoderBase;
use Drupal\Component\Serialization\Json;
class WSExampleBlockDecoder extends WSDecoderBase {
public function decode($data) {
$items = [];
if (!isset($data) || empty($data)) {
return;
}
if (substr($data, 0, 3) == pack("CCC", 0xef, 0xbb, 0xbf)) {
$data = substr($data, 3);
}
$data = trim($data);
$json_data = Json::decode($data);
foreach ($json_data as $element) {
$items[] = [
'#markup' => '<h2>' . $element['title'] . '</h2><p>' . $element['body'] . '</p>',
];
}
$content = [
'#theme' => 'item_list',
'#list_type' => 'ul',
'#title' => 'My List',
'#items' => $items,
'#attributes' => [
'class' => 'mylist',
],
'#wrapper_attributes' => [
'class' => 'container',
],
];
return \Drupal::service('renderer')
->render($content);
}
public function accepts() {
return [
'text/json',
];
}
}