9

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?

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

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

  • Nice answer, saved my day. Keep posting such useful answers. – Pupil Nov 12 at 10:02
4

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

$node->save();

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

pathauto_entity_insert($node)

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)

2

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

1

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.

0

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'))
  ->setTranslatable(TRUE)
  ->setDisplayOptions('form', [
    'type' => 'path',
    'weight' => 30,
  ])
  ->setDisplayConfigurable('form', TRUE)
  ->setComputed(TRUE);

  return $fields;
}

}

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.