You are here

public function EntityReferenceHierarchyFieldItemList::postSave in Entity Reference Hierarchy 3.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldType/EntityReferenceHierarchyFieldItemList.php \Drupal\entity_hierarchy\Plugin\Field\FieldType\EntityReferenceHierarchyFieldItemList::postSave()

Defines custom post-save behavior for field values.

This method is called during the process of saving an entity, just after item values are written into storage.

Parameters

bool $update: Specifies whether the entity is being updated or created.

Return value

bool Whether field items should be rewritten to the storage as a consequence of the logic implemented by the custom behavior.

Overrides FieldItemList::postSave

See also

\Drupal\Core\Field\FieldItemInterface::postSave()

File

src/Plugin/Field/FieldType/EntityReferenceHierarchyFieldItemList.php, line 18

Class

EntityReferenceHierarchyFieldItemList
Defines a field item list for entity reference with hierarchy.

Namespace

Drupal\entity_hierarchy\Plugin\Field\FieldType

Code

public function postSave($update) {

  // The values are now empty.
  if (empty($this->list)) {

    // If this node was in the tree, it needs to be moved to a root node.
    $stubNode = $this
      ->getNestedSetNodeFactory()
      ->fromEntity($this
      ->getEntity());
    $storage = $this
      ->getTreeStorage();
    if (($existingNode = $storage
      ->getNode($stubNode)) && $existingNode
      ->getDepth() > 0) {
      $fieldDefinition = $this
        ->getFieldDefinition();
      $fieldName = $fieldDefinition
        ->getName();
      $entityTypeId = $fieldDefinition
        ->getTargetEntityTypeId();
      $this
        ->lockTree($fieldName, $entityTypeId);
      $storage
        ->moveSubTreeToRoot($existingNode);
      $this
        ->releaseLock($fieldName, $entityTypeId);
    }
  }
  return parent::postSave($update);
}