Overrides DatabaseStorageController::preSave().
Overrides DatabaseStorageController::preSave
protected function preSave(EntityInterface $entity) {
// This is the easiest way to handle the unique internal path '<front>',
// since a path marked as external does not need to match a router path.
$entity->external = url_is_external($entity->link_path) || $entity->link_path == '<front>' ? 1 : 0;
// Try to find a parent link. If found, assign it and derive its menu.
$parent_candidates = !empty($entity->parentCandidates) ? $entity->parentCandidates : array();
$parent = $this
->findParent($entity, $parent_candidates);
if ($parent) {
$entity->plid = $parent
->id();
$entity->menu_name = $parent->menu_name;
}
else {
$entity->plid = 0;
}
// Directly fill parents for top-level links.
if ($entity->plid == 0) {
$entity->p1 = $entity
->id();
for ($i = 2; $i <= MENU_MAX_DEPTH; $i++) {
$parent_property = "p{$i}";
$entity->{$parent_property} = 0;
}
$entity->depth = 1;
}
else {
if ($entity->has_children && $entity->original) {
$limit = MENU_MAX_DEPTH - $this
->findChildrenRelativeDepth($entity->original) - 1;
}
else {
$limit = MENU_MAX_DEPTH - 1;
}
if ($parent->depth > $limit) {
return FALSE;
}
$entity->depth = $parent->depth + 1;
$this
->setParents($entity, $parent);
}
// Need to check both plid and menu_name, since plid can be 0 in any menu.
if (isset($entity->original) && ($entity->plid != $entity->original->plid || $entity->menu_name != $entity->original->menu_name)) {
$this
->moveChildren($entity, $entity->original);
}
// Find the router_path.
if (empty($entity->router_path) || empty($entity->original) || isset($entity->original) && $entity->original->link_path != $entity->link_path) {
if ($entity->external) {
$entity->router_path = '';
}
else {
// Find the router path which will serve this path.
$entity->parts = explode('/', $entity->link_path, MENU_MAX_PARTS);
$entity->router_path = _menu_find_router_path($entity->link_path);
}
}
// Find the route_name.
if (!isset($entity->route_name)) {
$entity->route_name = $this
->findRouteName($entity->link_path);
}
}