3 namespace Drupal\draggableviews;
5 use Drupal\views\ViewExecutable;
6 use Drupal\Component\Utility\Html;
9 * Class DraggableViews.
11 class DraggableViews {
16 * @var \Drupal\views\ViewExecutable
21 * Constructs DraggableViewsRows object.
23 * @param \Drupal\views\ViewExecutable $view
26 public function __construct(ViewExecutable $view) {
31 * Get index by name and id.
33 public function getIndex($name, $id) {
34 foreach ($this->view->result as $item) {
35 if ($item->$name == $id) {
45 public function getDepth($index) {
46 if (!isset($this->view->result[$index])) {
49 $row = $this->view->result[$index];
50 // If parent is available, set parent's depth +1.
51 return (!empty($row->draggableviews_structure_parent)) ? $this->getDepth($this->getIndex('nid', $row->draggableviews_structure_parent)) + 1 : 0;
55 * Get parent by index.
57 public function getParent($index) {
58 return isset($this->view->result[$index]->draggableviews_structure_parent) ? $this->view->result[$index]->draggableviews_structure_parent : 0;
62 * Get ancestor by index.
64 public function getAncestor($index) {
65 $row = $this->view->result[$index];
66 return !empty($row->draggableviews_structure_parent) ? $this->getAncestor($this->getIndex('nid', $row->draggableviews_structure_parent)) : $index;
70 * Return value by it's name and index.
72 public function getValue($name, $index) {
73 return $this->view->result[$index]->$name;
77 * Get HTML id for draggableviews table.
79 public function getHtmlId() {
80 return Html::getId('draggableviews-table-' . $this->view->id() . '-' . $this->view->current_display);