Skip to content

Instantly share code, notes, and snippets.

@init90
Created November 14, 2022 15:46
Show Gist options
  • Save init90/e1a764fa4e118d2251af2382eba6d9b2 to your computer and use it in GitHub Desktop.
Save init90/e1a764fa4e118d2251af2382eba6d9b2 to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\gojara_activity_results\Plugin\views\field;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
/**
* Provides Result Summary field handler.
*
* @ViewsField("activity_result_summary")
*/
class ResultSummary extends FieldPluginBase {
/**
* {@inheritdoc}
*/
public function query() {
}
/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
/** @var \Drupal\gojara_activity_results\ActivityResultInterface $result */
$result = $values->_entity;
$data = json_decode($result->get('field_data')->value);
$result_summary = 'Verb: ' . $data->verb->display->{"en-US"} . '<br />';
if (isset($data->result)) {
if (isset($data->result->completion)) {
$result_summary .= 'Result completion: ' . ($data->result->completion ? 'true' : 'false') . '<br />';
}
if (isset($data->result->success)) {
$result_summary .= 'Result success: ' . ($data->result->success ? 'true' : 'false') . '<br />';
}
}
else {
$result_summary .= 'Result: - <br />';
}
$output['summary'] = [
'#markup' => $result_summary,
];
$link_url = Url::fromRoute('gojara_activity_results.result_json', [
'activity_result' => $result->id(),
]);
$link_url->setOptions([
'attributes' => [
'class' => [
'use-ajax',
],
'data-dialog-type' => 'modal',
]
]);
$output['json_link'] = Link::fromTextAndUrl(t('Raw JSON'), $link_url)->toRenderable();
$output['#attached'] = ['library' => ['core/drupal.dialog.ajax']];
return $output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment