Is it possible to programmatically add a path alias to a programmatically created node? I have created pre save hook in module which is populating my node by fetching information from various API's. However I can't get my mind around how to add node alias to newly created node. Is there any function or module or something (Drupal 7 had path_auto)? Or should I go with raw insert into url_alias? Might I shoot myself in the foot with such a drastic move?
To create the alias, I first check if the alias was set in the user interface, and if it is empty then I set it manually using the label().

use Drupal\Core\Entity\EntityInterface;

/**
 * hook_entity_insert
 */
function mymodule_entity_insert(EntityInterface $entity) {
    // Convert to lowercase, trim spaces, replace spaces with underscore and then truncate to 60 chars.
    $pathTitle = substr(str_replace(" ","_",trim(strtolower($entity-&gt;label()))),0,60);
    if ($entity-&gt;toArray()['path'][0]['alias'] == "") {
        $path = \Drupal::service('path.alias_storage')-&gt;save("/node/" . $entity-&gt;id(), "/mynewpath/" . $pathTitle, "en");
    }
}


https://www.drupal.org/node/1853148 for more information