class WSExampleBlockDecoder in Web Service Data 8
Same name and namespace in other branches
- 2.0.x modules/wsdata_example/src/Plugin/WSDecoder/WSExampleBlockDecoder.php \Drupal\wsdata_example\Plugin\WSDecoder\WSExampleBlockDecoder
JSON Decoder.
Plugin annotation
@WSDecoder(
id = "ExampleBlockDecoder",
label = @Translation("Example block decoder", context = "WSDecoder"),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\wsdata\Plugin\WSDecoderBase implements WSDecoderInterface
- class \Drupal\wsdata_example\Plugin\WSDecoder\WSExampleBlockDecoder
- class \Drupal\wsdata\Plugin\WSDecoderBase implements WSDecoderInterface
Expanded class hierarchy of WSExampleBlockDecoder
File
- modules/
wsdata_example/ src/ Plugin/ WSDecoder/ WSExampleBlockDecoder.php, line 16
Namespace
Drupal\wsdata_example\Plugin\WSDecoderView source
class WSExampleBlockDecoder extends WSDecoderBase {
/**
* {@inheritdoc}
*/
public function decode($data) {
$items = [];
if (!isset($data) || empty($data)) {
return;
}
// Remove UTF-8 BOM if present, json_decode() does not like it.
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);
}
/**
* {@inheritdoc}
*/
public function accepts() {
return [
'text/json',
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
WSDecoderBase:: |
public | property | Storage for decoded data. | |
WSDecoderBase:: |
protected | property | Storage for error information. | |
WSDecoderBase:: |
protected | property | Languages which we have data for. | |
WSDecoderBase:: |
public | function |
Add data to an empty object or replace all existing data. Overrides WSDecoderInterface:: |
|
WSDecoderBase:: |
public | function |
Retrieve the value for the given data key. Overrides WSDecoderInterface:: |
|
WSDecoderBase:: |
public | function |
Retrieve error message, if any. Overrides WSDecoderInterface:: |
|
WSDecoderBase:: |
public | function | Returns whether or not the result on the decoder are cacheable. | |
WSDecoderBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
|
WSExampleBlockDecoder:: |
public | function |
Returns an array of the content type of the data this processor accepts. Overrides WSDecoderBase:: |
|
WSExampleBlockDecoder:: |
public | function |
Decode the web service response string into an array. Overrides WSDecoderBase:: |