Checks and updates the 'has_children' property for the parent of a link.
\Drupal\Core\Entity\EntityInterface $entity: A menu link entity.
protected function updateParentalStatus(EntityInterface $entity, $exclude = FALSE) {
// If plid == 0, there is nothing to update.
if ($entity->plid) {
// Check if at least one visible child exists in the table.
$query = \Drupal::entityQuery($this->entityType);
$query
->condition('menu_name', $entity->menu_name)
->condition('hidden', 0)
->condition('plid', $entity->plid)
->count();
if ($exclude) {
$query
->condition('mlid', $entity
->id(), '<>');
}
$parent_has_children = (bool) $query
->execute() ? 1 : 0;
$this->database
->update('menu_links')
->fields(array(
'has_children' => $parent_has_children,
))
->condition('mlid', $entity->plid)
->execute();
}
}