function farm_sensor_asset_view_alter in farmOS 2.x
Implements hook_ENTITY_TYPE_view_alter().
File
- modules/
asset/ sensor/ farm_sensor.module, line 16 - The farm_sensor module.
Code
function farm_sensor_asset_view_alter(array &$build, AssetInterface $asset, EntityViewDisplayInterface $display) {
// Bail if there is no sensor asset.
if (empty($asset) || $asset
->bundle() != 'sensor') {
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') {
// Bail if the sensor asset does not have any basic data streams.
$basic_data_streams = array_filter($asset
->get('data_stream')
->referencedEntities(), function (DataStreamInterface $data_stream) {
return $data_stream
->bundle() === 'basic';
});
if (count($basic_data_streams) === 0) {
return;
}
// Add a Developer information details element with brief description.
$build['api'] = [
'#type' => 'details',
'#title' => t('Developer information'),
'#description' => t('This sensor asset 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 sensor. For more information, refer to the farmOS sensor guide.'),
'#open' => FALSE,
];
// Build URL to the sensor API endpoint.
$url = new Url('farm_sensor.data_stream_data', [
'uuid' => $asset
->uuid(),
]);
// If the sensor is not public, include the private key.
if (!$asset
->get('public')->value) {
$url
->setOption('query', [
'private_key' => $asset
->get('private_key')->value,
]);
}
// 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>',
'#weight' => -10,
];
// 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>',
];
}
}