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:

  1. The node module and the Article content type exists
  2. Content Translation is enabled for Articles, and in particular the URL Alias field
  3. The french language is enabled
  4. 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

samuel.mortenson created an issue. See original summary.

samuel.mortenson’s picture

Title: Problems with programmatically creating Content Translations with translatable URL Aliases » Problems programmatically creating Content Translations with translatable URL Aliases
Issue summary: View changes

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.