You are here

class DraggableViews in DraggableViews 2.0.x

Same name in this branch
  1. 2.0.x src/DraggableViews.php \Drupal\draggableviews\DraggableViews
  2. 2.0.x src/Plugin/migrate/destination/DraggableViews.php \Drupal\draggableviews\Plugin\migrate\destination\DraggableViews
Same name and namespace in other branches
  1. 8 src/DraggableViews.php \Drupal\draggableviews\DraggableViews

Class DraggableViews.

Hierarchy

Expanded class hierarchy of DraggableViews

2 files declare their use of DraggableViews
draggableviews.module in ./draggableviews.module
Contains draggableviews.module.
DraggableViewsField.php in src/Plugin/views/field/DraggableViewsField.php
1 string reference to 'DraggableViews'
draggableviews.info.yml in ./draggableviews.info.yml
draggableviews.info.yml

File

src/DraggableViews.php, line 12

Namespace

Drupal\draggableviews
View source
class DraggableViews {

  /**
   * The view.
   *
   * @var \Drupal\views\ViewExecutable
   */
  public $view;

  /**
   * Constructs DraggableViewsRows object.
   *
   * @param \Drupal\views\ViewExecutable $view
   *   Views object.
   */
  public function __construct(ViewExecutable $view) {
    $this->view = $view;
  }

  /**
   * Get index by id.
   */
  public function getIndex($id) {
    foreach ($this->view->result as $item) {
      if ($item->_entity instanceof Media) {

        // Media index name.
        $name = 'mid';
      }
      else {

        // Node index name. The default one.
        $name = 'nid';
      }
      if ($item->{$name} == $id) {
        return $item->index;
      }
    }
    return FALSE;
  }

  /**
   * Get depth by index.
   */
  public function getDepth($index) {
    if (!isset($this->view->result[$index])) {
      return FALSE;
    }
    $row = $this->view->result[$index];

    // 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;
  }

  /**
   * Get parent by index.
   */
  public function getParent($index) {
    return isset($this->view->result[$index]->draggableviews_structure_parent) ? $this->view->result[$index]->draggableviews_structure_parent : 0;
  }

  /**
   * Get ancestor by index.
   */
  public function getAncestor($index) {
    $row = $this->view->result[$index];
    return !empty($row->draggableviews_structure_parent) ? $this
      ->getAncestor($this
      ->getIndex($row->draggableviews_structure_parent)) : $index;
  }

  /**
   * Return value by it's name and index.
   */
  public function getValue($name, $index) {
    return $this->view->result[$index]->{$name};
  }

  /**
   * Get HTML id for draggableviews table.
   */
  public function getHtmlId() {
    return Html::getId('draggableviews-table-' . $this->view
      ->id() . '-' . $this->view->current_display);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DraggableViews::$view public property The view.
DraggableViews::getAncestor public function Get ancestor by index.
DraggableViews::getDepth public function Get depth by index.
DraggableViews::getHtmlId public function Get HTML id for draggableviews table.
DraggableViews::getIndex public function Get index by id.
DraggableViews::getParent public function Get parent by index.
DraggableViews::getValue public function Return value by it's name and index.
DraggableViews::__construct public function Constructs DraggableViewsRows object.