I'm having a lot of problems figuring out how to properly create a new entity translation if the default translation had a URL Alias. It's probably easier for me to show you the behavior I'm seeing than explaining my entire setup, so here are some interesting commands you should be able to run on any Drupal site with the following setup:
- The node module and the Article content type exists
- Content Translation is enabled for Articles, and in particular the URL Alias field
- The french language is enabled
- You can run arbitrary PHP on your site (I use the "drush php" command/shell)
With that, here's what I'm seeing:
>>> // Create a node
>>> $node = \Drupal\node\Entity\Node::create(['type' => 'article', 'path' => '/test-1', 'title' => 'Hello Drupal']);
>>> // Save the node
>>> $node->save()
>>> // Create a translation
>>> $node->addTranslation('fr', $node->toArray());
>>> // Save the node again
>>> $node->save()
>>> // Check out the values for the default and french languages. Notice that there's no langcode?
>>> $node->get('path')->getValue()
=> [
[
"alias" => "/test-1",
"pid" => "7",
],
]
>>> $node->getTranslation('fr')->get('path')->getValue()
=> [
[
"alias" => "/test-1",
"pid" => "7",
],
]
>>> // Now look at what's in the database. The langcode is french!
>>> \Drupal::database()->query("select * from url_alias where source = '/node/6'")->fetchAll()
=> [
{#7747
+"pid": "7",
+"source": "/node/6",
+"alias": "/test-1",
+"langcode": "fr",
},
]
At this point I'm not sure if this is a DX problem where, when setting a URL Alias in code, you always must include the langcode, or if I'm missing the mark entirely. Thoughts?
Comments
Comment #2
samuel.mortensonComment #14
quietone commented@samuel.mortenson, is this still a problem?
I tested on Drupal 9.5.x, standard install, by following the steps in the IS but I get this failure
I didn't investigate any further.
Comment #15
samuel.mortenson@quietone I'm guessing content translation wasn't enabled for articles, or italian wasn't enabled as a language for you.
But regardless, I tried to replicate my original steps and have now found that URL aliases (now called path aliases) are created for each langcode:
So closing this out. Thanks!