How to add a Custom Serializer to alter the Output of REST EXPORT 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-&gt;view-&gt;result as $row_index =&gt; $row) {
      $this-&gt;view-&gt;row_index = $row_index;
      $rows[] = $this-&gt;view-&gt;rowPlugin-&gt;render($row);
    }

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

}


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



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.
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.