Closed arysom closed 5 years ago
Did you figure this out? Maybe the answer will help someone else in the future.
Hi @e0ipso , I thought that nobody will respond to it, so I've tried to delete the issue, but I could only close it. I've also posted the question on drupal stackexchange, but also no sign of solution yet. After winter holidays, I'll try once more and if I find a solution, I'll post it here.
I've resolved the sorting problem with sorting data inside the formatter. Here's my code in case someone had similar problems.
<?php
/**
* @file
* Contains \Drupal\restful\Plugin\formatter\OrderFormatter.
*/
namespace Drupal\my_module\Plugin\formatter;
use Drupal\restful\Plugin\formatter\FormatterJson;
use Drupal\restful\Plugin\formatter\FormatterInterface;
/**
* Class OrderFormatter
* @package Drupal\restful\Plugin\formatter
*
* @Formatter(
* id = "order_formatter",
* label = "ORDER FORMATTER",
* description = "Output filtered data",
* curie = {
* "name": "dis",
* "path": "doc/rels",
* "template": "/{rel}",
* },
* )
*/
class OrderFormatter extends FormatterJson implements FormatterInterface {
/**
* Content Type
*
* @var string
*/
protected $contentType = 'application/json+sort; charset=utf-8';
/**
* {@inheritdoc}
*/
public function render(array $structured_data) {
$i=0;
foreach ($structured_data['data'] as $v) {
$structured_data['data'][$i]['unsortedThings'] = $this->sortBy($structured_data['data'][$i]['unsortedThings']);
$i++;
}
return drupal_json_encode($structured_data);
}
public function sortBy($arr) {
$results = [];
foreach ($arr as $k=>$v) {
$results[$k] = $v['weight']; //this is my sorting field
}
asort($results, SORT_REGULAR);
foreach (array_keys($results) as $key) {
$results[$key] = $arr[$key];
}
$results = array_values($results);
return $results;
}
}
You run this formatter by adding header to your request: Accept: application/json+sort; charset=utf-8
Hi,
my plugin extends ResourceNode and implements ResourceInterface. Overwritting defaultSortInfo() doesn't work. Did the api changed or I'm extending/implementing wrong class? Could you please point me what am I doing wrong?