1

To the Response/Output of REST EXPORT of a view, I want to add one extra row.

So I created a Custom Serializer in my module. The path of the file is:

My_Module/src/Plugin/views/style/HindiCustomSerializer.php

Here I extended the Serializer Class from core/modules/rest/src/Plugin/views/style/Serializer.php

And I am trying to override the render() method in my custom serializer like this. I am copying the render() method from original Serializer and adding one extra line from my side.

use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\rest\Plugin\views\style\Serializer;

/**
 * The style plugin for serialized output formats.
 *
 * Add and extra row to the output.
 *
 * @ingroup views_style_plugins
 *
 * @ViewsStyle(
 *   id = "hindi_custom_serializer",
 *   title = @Translation("Hindi Custom Serializer"),
 *   help = @Translation("Serializes views row data using the Serializer component."),
 *   display_types = {"data"}
 * )
 */
class HindiCustomSerializer extends Serializer implements CacheableDependencyInterface{

  public function render() {
    $rows = [];
    // 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_index => $row) {
      $this->view->row_index = $row_index;
      $rows[] = $this->view->rowPlugin->render($row);
    }

    $rows[] = "JUST ADDING AN EXTRA ROW!!";
    unset($this->view->row_index);
    // Get the content type configured in the display or fallback to the
    // default.
    if ((empty($this->view->live_preview))) {
      $content_type = $this->displayHandler->getContentType();
    }
    else {
      $content_type = !empty($this->options['formats']) ? reset($this->options['formats']) : 'json';
    }
    return $this->serializer->serialize($rows, $content_type, ['views_style_plugin' => $this]);
  }

}

The option to choose the custom serializer MyCustomSerializer should be visible in Format section of view settings.

enter image description here

But its not displaying. Is there any step I am missing? Do I need to add some extra information regarding the serializer in .info file or any such steps needed ?

PS: If I add this line directly to the original Serializer code, I see the extra row coming in the response.

$rows[] = "JUST ADDING AN EXTRA ROW!!";

But of course that's not the correct way.

  • For what I'm seeing in your question droves me that you forgot to add the proper @ViewStyle annotation check the \Drupal\rest\Plugin\views\style\Serializer annotation – d70rr3s Jan 24 at 7:01
  • What are the possible issues ? – Sandesh Yadav Jan 24 at 7:03
  • Sorry I wrote to fast and stroke the Enter key without adding my actual comment :). Also don't forget to clear caches – d70rr3s Jan 24 at 7:04
  • thanks for comment. I added the annotations and updated the question. Still not getting it displayed. – Sandesh Yadav Jan 24 at 7:19
  • You put it in the wrong place. I've updated your question with the annotation placed properly on the class not the method. Please test it that way and more information if the problem persist since this is a common solution and should work in almost all cases. – d70rr3s Jan 24 at 7:43
2

The plugin was missing the proper annotation (added now in the question). Views plugins use Annotation Discovery.

Adding something like the following solved the problem:

/**
 * My custom serializer
 *
 * @ViewsStyle(
 *   id = "hindi_custom_serializer",
 *   title = @Translation("Hindi Custom Serializer"),
 *   help = @Translation("Serializes views row data using the Serializer component."),
 *   display_types = {"data"}
 * )
 */
class HindiCustomSerializer extends Serializer {
  ....
}

Also worth noting that you don't need to include the implements CacheableDependencyInterface since is already declared by the \Drupal\rest\Plugin\views\style\Serializer plugin which you are extending from.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.