You are here

class InsertPosition in Entity Reference Hierarchy 3.x

Same name and namespace in other branches
  1. 8.2 src/Storage/InsertPosition.php \Drupal\entity_hierarchy\Storage\InsertPosition

Defines a value object for an insert position.

Hierarchy

Expanded class hierarchy of InsertPosition

1 file declares its use of InsertPosition
EntityReferenceHierarchy.php in src/Plugin/Field/FieldType/EntityReferenceHierarchy.php

File

src/Storage/InsertPosition.php, line 10

Namespace

Drupal\entity_hierarchy\Storage
View source
class InsertPosition {
  const ACTION_ADD_NODE = 'addNode';
  const DIRECTION_BEFORE = 'Before';
  const DIRECTION_AFTER = 'After';
  const DIRECTION_BELOW = 'Below';
  const ACTION_MOVE_SUB_TREE = 'moveSubTree';

  /**
   * Direction to insert, one of the DIRECTION_ constants.
   *
   * @var string
   */
  protected $direction;

  /**
   * Node to insert before or after.
   *
   * @var \PNX\NestedSet\Node
   */
  protected $reference;

  /**
   * TRUE to insert, FALSE to move.
   *
   * @var bool
   */
  protected $insert;

  /**
   * Constructs a new InsertPosition object.
   *
   * @param \PNX\NestedSet\Node $reference
   *   Node to insert before or after.
   * @param bool $insert
   *   TRUE if inserting rather than moving.
   * @param string $direction
   *   Direction constants - one of Before/After/Below.
   */
  public function __construct(Node $reference, $insert = TRUE, $direction = self::DIRECTION_BEFORE) {
    $this->reference = $reference;
    $this->direction = $direction;
    $this->insert = $insert;
  }

  /**
   * Perform the insert.
   *
   * @param \Drupal\entity_hierarchy\Storage\NestedSetStorage $storage
   *   Storage.
   * @param \PNX\NestedSet\Node|\PNX\NestedSet\NodeKey $node
   *   Node to create or insert.
   *
   * @return \PNX\NestedSet\Node
   *   Inserted node.
   */
  public function performInsert(NestedSetStorage $storage, $node) {
    $command = [
      'action' => self::ACTION_ADD_NODE,
      'direction' => $this->direction,
    ];
    if (!$this->insert) {
      $command['action'] = self::ACTION_MOVE_SUB_TREE;
    }
    $method = implode('', $command);
    return call_user_func_array([
      $storage,
      $method,
    ], [
      $this->reference,
      $node,
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
InsertPosition::$direction protected property Direction to insert, one of the DIRECTION_ constants.
InsertPosition::$insert protected property TRUE to insert, FALSE to move.
InsertPosition::$reference protected property Node to insert before or after.
InsertPosition::ACTION_ADD_NODE constant
InsertPosition::ACTION_MOVE_SUB_TREE constant
InsertPosition::DIRECTION_AFTER constant
InsertPosition::DIRECTION_BEFORE constant
InsertPosition::DIRECTION_BELOW constant
InsertPosition::performInsert public function Perform the insert.
InsertPosition::__construct public function Constructs a new InsertPosition object.