You are here

function entity_hierarchy_node_insert in Entity Reference Hierarchy 8

Implements This function will be called whenever a new node is created. We will write the hierarchy information to the database if a parent is set on the node add form.

See also

hook_ENTITY_TYPE_insert() for node entities.

HierarchyManagerInterface::hierarchySaveNode

File

./entity_hierarchy.module, line 166
A module to make nodes hierarchical.

Code

function entity_hierarchy_node_insert(NodeInterface $node) {

  /** @var \Drupal\entity_hierarchy\HierarchyManager $hierarchy_manager */
  $hierarchy_manager = \Drupal::service('entity_hierarchy.manager');

  // TODO: check if we really need a permission check here. (Don't even show the form if no permission.)
  $user = \Drupal::currentUser();
  $uid = $user
    ->id();
  $node_uid = $node
    ->getOwnerId();
  if ($user
    ->hasPermission('edit all node parents') || $node_uid == $uid && $user
    ->hasPermission('edit own node parents')) {
    $hierarchy_manager
      ->hierarchySaveNode($node);
  }
}