You are here

private function HierarchyManager::hierarchyRecordSave in Entity Reference Hierarchy 8

Prepare an individual item to be added/removed to the database.

If the item already has a hierarchy id (hid) do the following: 1) If the item doesn't have a parent node id (pnid), it is deleted via hierarchyDeleteRecord. Otherwise, load the current item from the database using hierarchyGetRecord 2) Adjust the child weight (cweight) via hierarchyGetParentNextChildWeight. 3) Update the database with the new weight using updateHierarchy.

If the item doesn't already have an hid: 1) Load the next available cweight using hierarchyGetParentNextChildWeight. 2) Insert a new hierarchy record into the database via insertHierarchy.

Parameters

object $item: The hierarchy object to be processed before adding the item to the database, updating an existing item, or deleting the item from the database.

See also

HierarchyManagerInterface::hierarchySaveNode

hierarchyDeleteRecord

hierarchyGetRecord

hierarchyGetParentNextChildWeight

insertHierarchy

updateHierarchy

1 call to HierarchyManager::hierarchyRecordSave()
HierarchyManager::hierarchySaveNode in src/HierarchyManager.php
Process a list of entity_hierarchy parents in preparation for writing to the database. No permission checking is done here. Each parent is written individually using HierarchyManager::hierarchyRecordSave.

File

src/HierarchyManager.php, line 644
Contains \Drupal\entity_hierarchy\HierarchyManager.

Class

HierarchyManager
Defines a hierarchy manager.

Namespace

Drupal\entity_hierarchy

Code

private function hierarchyRecordSave(&$item) {
  if (!empty($item->hid)) {

    // Remove the item if it's no longer needed.
    if (empty($item->pnid)) {
      $this
        ->hierarchyDeleteRecord($item->hid);
    }
    else {
      $existing_item = $this
        ->hierarchyGetRecord($item->hid);

      // If the parent has been changed:
      if ($existing_item->pnid !== $item->pnid) {
        $item->cweight = $this
          ->hierarchyGetParentNextChildWeight($item->pnid);
      }
    }
  }
  else {
    $item->cweight = $this
      ->hierarchyGetParentNextChildWeight($item->pnid);
  }
  if ($item->pnid) {
    if (empty($item->hid)) {
      $this
        ->insertHierarchy($item);
    }
    else {
      $this
        ->updateHierarchy($item);
    }
  }
}