5 * Contains \Drupal\draggableviews\DraggableViews class.
8 namespace Drupal\draggableviews;
10 use Drupal\views\ViewExecutable;
11 use Drupal\Component\Utility\Html;
14 * Class DraggableViews.
16 class DraggableViews {
21 * @var \Drupal\views\ViewExecutable $view
26 * Constructs DraggableViewsRows object.
28 * @param \Drupal\views\ViewExecutable $view
31 public function __construct(ViewExecutable $view) {
36 * Get index by name and id.
38 public function getIndex($name, $id) {
39 foreach ($this->view->result as $item) {
40 if ($item->$name == $id) {
50 public function getDepth($index) {
51 if (!isset($this->view->result[$index])) {
54 $row = $this->view->result[$index];
55 // If parent is available, set parent's depth +1.
56 return (!empty($row->draggableviews_structure_parent)) ? $this->getDepth($this->getIndex('nid', $row->draggableviews_structure_parent)) + 1 : 0;
60 * Get parent by index.
62 public function getParent($index) {
63 return isset($this->view->result[$index]->draggableviews_structure_parent) ? $this->view->result[$index]->draggableviews_structure_parent : 0;
67 * Get ancestor by index.
69 public function getAncestor($index) {
70 $row = $this->view->result[$index];
71 return !empty($row->draggableviews_structure_parent) ? $this->getAncestor($this->getIndex('nid', $row->draggableviews_structure_parent)) : $index;
75 * Return value by it's name and index.
77 public function getValue($name, $index) {
78 return $this->view->result[$index]->$name;
82 * Get HTML id for draggableviews table.
84 public function getHtmlId() {
85 return Html::getId('draggableviews-table-' . $this->view->id() . '-' . $this->view->current_display);