Guest User

Untitled

a guest
May 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. /**
  2. * The style plugin for serialized output formats.
  3. *
  4. * @ingroup views_style_plugins
  5. *
  6. * @ViewsStyle(
  7. * id = "serializer_glossary",
  8. * title = @Translation("Serializer Glossary"),
  9. * help = @Translation("Processes results and groups them by first letter, for small result lists only."),
  10. * display_types = {"data"}
  11. * )
  12. */
  13. class SerializerGlossary extends Serializer {
  14.  
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public function render() {
  19. $rows = [];
  20.  
  21. foreach ($this->view->result as $row_index => $row) {
  22. $entity = $this->view->rowPlugin->render($row);
  23. $title = $entity->label();
  24. $first_letter = Unicode::strtoupper($title[0]);
  25. $rows[$first_letter][] = $this->view->rowPlugin->render($row);
  26. }
  27.  
  28. // Get the content type configured in the display or fallback to the
  29. // default.
  30. if ((empty($this->view->live_preview))) {
  31. $content_type = $this->displayHandler->getContentType();
  32. }
  33. else {
  34. $content_type = !empty($this->options['formats']) ? reset($this->options['formats']) : 'json';
  35. }
  36.  
  37. return $this->serializer->serialize(['results' => ['items' => $rows, 'totalCount' => count($this->view->result)]], $content_type);
  38. }
  39. }
Add Comment
Please, Sign In to add comment