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.

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

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.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.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.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.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.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.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Issue tags: +Bug Smash Initiative

@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

>>> $node->addTranslation('it', $node->toArray());
InvalidArgumentException with message 'Invalid translation language (it) specified.'

I didn't investigate any further.

samuel.mortenson’s picture

Status: Active » Closed (outdated)

@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:

>>> \Drupal::database()->query("select * from path_alias")->fetchAll()
=> [
     {#7184
       +"id": "1",
       +"revision_id": "1",
       +"uuid": "caa92ab1-5142-4440-8b59-9184eec7ff9b",
       +"langcode": "en",
       +"path": "/node/1",
       +"alias": "/test-1",
       +"status": "1",
     },
     {#5639
       +"id": "2",
       +"revision_id": "2",
       +"uuid": "861fb04e-27a6-42f0-b179-72492d64adae",
       +"langcode": "fr",
       +"path": "/node/1",
       +"alias": "/test-1",
       +"status": "1",
     },
   ]
>>> 

So closing this out. Thanks!