data_stream.module in farmOS 2.x
The data stream module.
File
modules/core/data_stream/data_stream.moduleView source
<?php
/**
* @file
* The data stream module.
*/
use Drupal\Core\Url;
use Drupal\data_stream\Entity\DataStreamInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
/**
* Implements hook_ENTITY_TYPE_view_alter().
*/
function data_stream_data_stream_view_alter(array &$build, DataStreamInterface $data_stream, EntityViewDisplayInterface $display) {
// Bail if there is no entity.
if (empty($data_stream)) {
return;
}
// Bail if not the basic type.
if ($data_stream
->bundle() != 'basic') {
return;
}
// Only render developer information in the full view mode.
// Use getOriginalMode() because getMode() is not reliable. The default
// display mode will return "full" unless a "default" display is actually
// saved in config. In either case, the original mode is always "full".
if ($display
->getOriginalMode() === 'full') {
// Add a Developer information details element with brief description.
// @todo Include link to updated user guide.
$build['api'] = [
'#type' => 'details',
'#title' => t('Developer information'),
'#description' => t('This data stream type will listen for data posted to it from other web-connected devices. Use the information below to configure your device to begin posting data to this data stream. For more information, refer to the farmOS sensor guide.'),
'#open' => FALSE,
];
// Build URL to the data stream API endpoint.
$url = new Url('data_stream.data', [
'uuid' => $data_stream
->uuid(),
]);
// If the data stream is not public, include the private key.
if (!$data_stream
->isPublic()) {
$url
->setOption('query', [
'private_key' => $data_stream
->getPrivateKey(),
]);
}
// Render the API url.
$url_string = $url
->setAbsolute()
->toString();
$url_string_label = t('URL');
$build['api']['url'] = [
'#type' => 'link',
'#title' => $url_string,
'#url' => $url,
'#prefix' => '<p><strong>' . $url_string_label . ':</strong> ',
'#suffix' => '</p>',
];
// Render JSON examples.
$request_time = \Drupal::time()
->getRequestTime();
$json_example = '{ "timestamp": ' . $request_time . ', "value": 76.5 }';
$json_example_label = t('JSON Example');
$build['api']['json_example'] = [
'#markup' => '<p><strong>' . $json_example_label . ':</strong> ' . $json_example . '</p>',
];
$json_example_multiple = '{ "timestamp": ' . $request_time . ', "sensor1": 76.5, "sensor2": 60 }';
$json_example_multiple_label = t('JSON Example (multiple values)');
$build['api']['json_example_multiple'] = [
'#markup' => '<p><strong>' . $json_example_multiple_label . ':</strong> ' . $json_example_multiple . '</p>',
];
// Render example CURL command.
$curl_example = 'curl -H "Content-Type: application/json" -X POST -d \'' . $json_example . '\' ' . $url_string;
$curl_example_label = t('Example CURL command');
$build['api']['json_example_multiple'] = [
'#markup' => '<p><strong>' . $curl_example_label . ':</strong> ' . $curl_example . '</p>',
];
}
// Add the basic data block view.
$build['views']['data'] = views_embed_view('data_stream_basic_data', 'block', $data_stream
->id());
}
/**
* Implements hook_ENTITY_TYPE_delete().
*/
function data_stream_data_stream_delete(DataStreamInterface $data_stream) {
// @todo Considerations for improved entity reference integrity?
// Remove any references to this data stream from the asset.data_stream field.
\Drupal::database()
->delete('asset__data_stream')
->condition('data_stream_target_id', $data_stream
->id())
->execute();
}
Functions
Name | Description |
---|---|
data_stream_data_stream_delete | Implements hook_ENTITY_TYPE_delete(). |
data_stream_data_stream_view_alter | Implements hook_ENTITY_TYPE_view_alter(). |