diff --git a/src/DraggableViews.php b/src/DraggableViews.php index 256fecb95..bd1f5d5c9 100644 --- a/src/DraggableViews.php +++ b/src/DraggableViews.php @@ -3,6 +3,7 @@ namespace Drupal\draggableviews; use Drupal\media\Entity\Media; +use Drupal\node\Entity\Node; use Drupal\views\ViewExecutable; use Drupal\Component\Utility\Html; @@ -11,6 +12,13 @@ */ class DraggableViews { + /** + * Already indexed ids. + * + * @var array + */ + protected $ids; + /** * The view. * @@ -25,6 +33,7 @@ class DraggableViews { * Views object. */ public function __construct(ViewExecutable $view) { + $this->ids = []; $this->view = $view; } @@ -37,11 +46,19 @@ public function getIndex($id) { // Media index name. $name = 'mid'; } - else { - // Node index name. The default one. + elseif ($item->_entity instanceof Node) { + // Node index name. $name = 'nid'; } - if ($item->$name == $id) { + else { + // The default one. + $name = 'id'; + } + + if (property_exists($item, $name) && $item->$name == $id) { + if (!in_array($id, $this->ids)) { + $this->ids[] = $id; + } return $item->index; } } @@ -51,13 +68,25 @@ public function getIndex($id) { /** * Get depth by index. */ - public function getDepth($index) { - if (!isset($this->view->result[$index])) { + public function getDepth($index, $current_depth = 0) { + if (!isset($this->view->result[$index]) || $index === FALSE) { return FALSE; } + $row = $this->view->result[$index]; + + if (!property_exists($row, 'draggableviews_structure_parent')) { + return 0; + } + + if ((string)$row->draggableviews_structure_parent === '0') { + return $current_depth; + } + + $current_depth++; + // If parent is available, set parent's depth +1. - return (!empty($row->draggableviews_structure_parent)) ? $this->getDepth($this->getIndex($row->draggableviews_structure_parent)) + 1 : 0; + return $this->getDepth($this->getIndex($row->draggableviews_structure_parent), $current_depth); } /** diff --git a/src/Plugin/views/field/DraggableViewsField.php b/src/Plugin/views/field/DraggableViewsField.php index 14d0dab50..1e9493eb5 100755 --- a/src/Plugin/views/field/DraggableViewsField.php +++ b/src/Plugin/views/field/DraggableViewsField.php @@ -144,7 +144,7 @@ public function viewsForm(&$form, FormStateInterface $form_state) { $options = [ 'table_id' => $draggableviews->getHtmlId(), 'action' => 'match', - 'relationship' => 'group', + 'relationship' => 'parent', 'group' => 'draggableviews-parent', 'subgroup' => 'draggableviews-parent', 'source' => 'draggableviews-id',