Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * The style plugin for serialized output formats.
- *
- * @ingroup views_style_plugins
- *
- * @ViewsStyle(
- * id = "serializer_glossary",
- * title = @Translation("Serializer Glossary"),
- * help = @Translation("Processes results and groups them by first letter, for small result lists only."),
- * display_types = {"data"}
- * )
- */
- class SerializerGlossary extends Serializer {
- /**
- * {@inheritdoc}
- */
- public function render() {
- $rows = [];
- foreach ($this->view->result as $row_index => $row) {
- $entity = $this->view->rowPlugin->render($row);
- $title = $entity->label();
- $first_letter = Unicode::strtoupper($title[0]);
- $rows[$first_letter][] = $this->view->rowPlugin->render($row);
- }
- // 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(['results' => ['items' => $rows, 'totalCount' => count($this->view->result)]], $content_type);
- }
- }
Add Comment
Please, Sign In to add comment