JSONDataField.php in JSON Field 8
File
src/Plugin/views/field/JSONDataField.php
View source
<?php
namespace Drupal\json_field\Plugin\views\field;
use Drupal\rest\Plugin\views\display\RestExport;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Serializer\SerializerInterface;
class JSONDataField extends FieldPluginBase {
protected $serializer;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('serializer'));
}
public function __construct(array $configuration, $plugin_id, $plugin_definition, SerializerInterface $serializer) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->serializer = $serializer;
}
public function usesGroupBy() {
return FALSE;
}
protected function allowAdvancedRender() {
return FALSE;
}
public function render(ResultRow $row) {
$value = $this
->getValue($row);
$build = parent::render($row);
if (!empty($value) && $this->view->display_handler instanceof RestExport) {
return $this->serializer
->decode($value, 'json');
}
return $build;
}
public function advancedRender(ResultRow $values) {
return $this
->render($values);
}
}
Classes
Name |
Description |
JSONDataField |
Field handler to present JSON data to an entity "data" display. |