You are here

public function SortableViewsStyleTrait::render in Sortableviews 8

File

src/SortableViewsStyleTrait.php, line 108

Class

SortableViewsStyleTrait
Provides sortable functionality for View styles.

Namespace

Drupal\sortableviews

Code

public function render() {
  $build = parent::render();
  $original_order = [];
  foreach ($this->view->result as $result) {
    $original_order[] = $result->_entity
      ->id();
  }
  $build['#attached']['drupalSettings']['sortableviews'][$this->view->dom_id] = [
    'original_order' => $original_order,
    'view_name' => $this->view->storage
      ->id(),
    'display_name' => $this->view->current_display,
    'ajax_url' => $this->urlGenerator
      ->generateFromRoute('sortableviews.ajax'),
    'dom_id' => $this->view->dom_id,
    'selector' => $this
      ->javascriptSelector(),
    'sort_order' => $this
      ->retrieveSortOrder(),
    // The following items allows to skip rebuilding the view.
    'page_number' => isset($this->view->pager->current_page) ? (int) $this->view->pager->current_page : 0,
    'total_rows' => isset($this->view->total_rows) ? $this->view->total_rows : count($this->view->result),
    'items_per_page' => isset($this->view->pager->options['items_per_page']) ? $this->view->pager->options['items_per_page'] : NULL,
  ];
  $build['#attached']['library'][] = 'sortableviews/sortableviews.sortable';
  return $build;
}