diff --git a/core/lib/Drupal/Core/CoreBundle.php b/core/lib/Drupal/Core/CoreBundle.php index f1d0046..cbb0c1d 100644 --- a/core/lib/Drupal/Core/CoreBundle.php +++ b/core/lib/Drupal/Core/CoreBundle.php @@ -126,6 +126,10 @@ public function build(ContainerBuilder $container) { ->setFactoryClass('Drupal\Core\ExceptionController') ->setFactoryMethod('getExceptionListener'); + // Register a very simple normalizer and the JSON encoder. + $container->register('serializer.normalizer.default', 'Drupal\Core\Serialization\DefaultNormalizer')->addTag('normalizer', array('priority' => 0)); + $container->register('serializer.encoder.default', 'Symfony\Component\Serializer\Encoder\JsonEncoder')->addTag('encoder', array('priority' => 0)); + $container->addCompilerPass(new RegisterMatchersPass()); $container->addCompilerPass(new RegisterNestedMatchersPass()); // Add a compiler pass for registering event subscribers. diff --git a/core/lib/Drupal/Core/Serialization/DefaultNormalizer.php b/core/lib/Drupal/Core/Serialization/DefaultNormalizer.php new file mode 100644 index 0000000..1e4767a --- /dev/null +++ b/core/lib/Drupal/Core/Serialization/DefaultNormalizer.php @@ -0,0 +1,50 @@ +get('request'); + parent::execute(); $output = $this->view->render(); - $response = $this->view->getResponse(); - $response->setContent($output); + $response = new Response($output, 200, array('Content-type' => $request->getMimeType('json'))); return $response; } @@ -123,7 +125,20 @@ public function execute() { * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::render(). */ public function render() { - $output = $this->view->style_plugin->render(); + // Get the Serializer service. + $container = drupal_container(); + $serializer = $container->get('serializer'); + + // If the Data Entity row plugin is used, this will be an array of entities + // which will pass through Serializer to one of the registered Normalizers, + // which will transform it to arrays/scalars. If the Data Field row plugin + // is used, $rows will not contain objects and will pass directly to the + // Encoder. + foreach ($this->view->result as $row) { + $rows[] = $this->view->style_plugin->row_plugin->render($row); + } + $output = $serializer->serialize($rows, 'json'); + if (!empty($this->view->live_preview)) { return '
' . $output . '
'; }