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?

  • 3
    I think no D8 release yet for this type of modules. You can do programmatically see drupal.org/node/1853148. Feb 3 '15 at 12:29
  • 1
    Awesome that's what I was looking for. Feb 3 '15 at 12:42
  • 1
    BTW you profile pic is too cute man.. :) Feb 3 '15 at 12:43

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->label()))),0,60);
    if ($entity->toArray()['path'][0]['alias'] == "") {
        $path = \Drupal::service('path.alias_storage')->save("/node/" . $entity->id(), "/mynewpath/" . $pathTitle, "en");

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


As of Drupal 8.8, path aliases are now entities, and the method of creating a new alias has changed.

To programmatically save an alias, you can do the following:

$path_alias = \Drupal\path_alias\Entity\PathAlias::create([
  'path' => '/unaliased/path',
  'alias' => '/aliased/path',


Using drupal 8, this is how I did (after a quick search in the pathauto.module file) :

Step 1 : Create the node

$node = Node::create([
    'type' => 'article',
    'title' => 'Test article',
    'langcode' => 'en', // Get it however you want or hardcode it.
    'uid' => [USER_ID] // Get it however you want.

Step 2 : Save the node


Step 3 : Insert the path (make sure the pathauto module is installed and add a pattern for the give node type)


If you need to update the node programmatically, don't forget to update its path : pathauto_entity_update($node) and don't forget to delete it if you delete the node (delete it BEFORE you delete the node of course since the reference will be lost if you don't do that) : pathauto_entity_delete($node)


To get a clean string, you can use this function from the Pathauto module.

$clean_string = \Drupal::service('pathauto.alias_cleaner')->cleanString('Your very dirty string, with many URL un-friendly parts!');

Source: Drupal: clean a string for use in URLs


We're maintaining an inofficial port of the pathauto module. You also need token.module, which you can get from here. Use at your own risk, the configuration structure might change, or core updates might break the module temporarily.

Work just started to get those two modules back to drupal.org.


Also if just going a custom content entity and want to add the url path alias to the form ...

just do

 * Implements hook_entity_base_field_info().
function YOURMODULE_entity_base_field_info(EntityTypeInterface $entity_type) {
 if (in_array($entity_type->id(), ['ENTITYID'], TRUE)) {
$fields['path'] = BaseFieldDefinition::create('path')
  ->setLabel(t('URL alias'))
  ->setDisplayOptions('form', [
    'type' => 'path',
    'weight' => 30,
  ->setDisplayConfigurable('form', TRUE)

  return $fields;



You can just set the value of the path field:

$node->set('path', 'some-alias');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.