0

I'm using https://www.drupal.org/project/restful module. I can sort base entity using annotation:

/**
 * Class Things__1_0
 * @package Drupal\restful_thing\Plugin\resource\node\thing
 *
 * @Resource(
 *   name = "things:1.0",
 *   resource = "things",
 *   label = "things",
 *   description = "Export the things with all authentication providers.",
 *   authenticationTypes = TRUE,
 *   authenticationOptional = TRUE,
 *   dataProvider = {
 *     "entityType": "node",
 *     "bundles": {
 *       "course"
 *     },
 *     "sort" = {
 *       "field_weight": "ASC"
 *     },
 *   },
 *   majorVersion = 1,
 *   minorVersion = 0
 * )
 */

But when it comes to relationship, the related data isn't ordered the way I need.

Inside public_fields():

$public_fields['modules'] = array(
  'property' => 'field_modules',
  'resource' => array(
    'name' => 'modules',
    'fullView' => TRUE,
    'majorVersion' => 1,
    'minorVersion' => 0,
    'dataProvider' => array( //here I'm trying to override dataProvider values
      'sort' => array(
        'field_weight' => 'ASC'
      )
    )
  )
);

Is there any way to sort related data?

---edit---

I've also placed "sort" inside the annotation of the modules plugin, but It doesn't have effect too.

1 Answer 1

0

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

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.