DraggableViews.php in DraggableViews 8
File
src/DraggableViews.php
View source
<?php
namespace Drupal\draggableviews;
use Drupal\media\Entity\Media;
use Drupal\views\ViewExecutable;
use Drupal\Component\Utility\Html;
class DraggableViews {
public $view;
public function __construct(ViewExecutable $view) {
$this->view = $view;
}
public function getIndex($id) {
foreach ($this->view->result as $item) {
if ($item->_entity instanceof Media) {
$name = 'mid';
}
else {
$name = 'nid';
}
if ($item->{$name} == $id) {
return $item->index;
}
}
return FALSE;
}
public function getDepth($index) {
if (!isset($this->view->result[$index])) {
return FALSE;
}
$row = $this->view->result[$index];
return !empty($row->draggableviews_structure_parent) ? $this
->getDepth($this
->getIndex($row->draggableviews_structure_parent)) + 1 : 0;
}
public function getParent($index) {
return isset($this->view->result[$index]->draggableviews_structure_parent) ? $this->view->result[$index]->draggableviews_structure_parent : 0;
}
public function getAncestor($index) {
$row = $this->view->result[$index];
return !empty($row->draggableviews_structure_parent) ? $this
->getAncestor($this
->getIndex($row->draggableviews_structure_parent)) : $index;
}
public function getValue($name, $index) {
return $this->view->result[$index]->{$name};
}
public function getHtmlId() {
return Html::getId('draggableviews-table-' . $this->view
->id() . '-' . $this->view->current_display);
}
}