Issue #2511968 by Berdir, jhedstrom, zaporylie, catch, dawehner, alexpott, amateescu: Path field should fall back to language neutral aliases (also makes this happen for the form widget!)
parent
bd72409157
commit
7758b75558
|
@ -4,6 +4,7 @@ namespace Drupal\path\Plugin\Field\FieldType;
|
|||
|
||||
use Drupal\Core\Access\AccessResult;
|
||||
use Drupal\Core\Field\FieldItemList;
|
||||
use Drupal\Core\Language\LanguageInterface;
|
||||
use Drupal\Core\Session\AccountInterface;
|
||||
use Drupal\Core\TypedData\ComputedItemListTrait;
|
||||
|
||||
|
@ -26,12 +27,18 @@ class PathFieldItemList extends FieldItemList {
|
|||
|
||||
$entity = $this->getEntity();
|
||||
if (!$entity->isNew()) {
|
||||
// @todo Support loading language neutral aliases in
|
||||
// https://www.drupal.org/node/2511968.
|
||||
$alias = \Drupal::service('path.alias_storage')->load([
|
||||
$conditions = [
|
||||
'source' => '/' . $entity->toUrl()->getInternalPath(),
|
||||
'langcode' => $this->getLangcode(),
|
||||
]);
|
||||
];
|
||||
$alias = \Drupal::service('path.alias_storage')->load($conditions);
|
||||
if ($alias === FALSE) {
|
||||
// Fall back to non-specific language.
|
||||
if ($this->getLangcode() !== LanguageInterface::LANGCODE_NOT_SPECIFIED) {
|
||||
$conditions['langcode'] = LanguageInterface::LANGCODE_NOT_SPECIFIED;
|
||||
$alias = \Drupal::service('path.alias_storage')->load($conditions);
|
||||
}
|
||||
}
|
||||
|
||||
if ($alias) {
|
||||
$value = $alias;
|
||||
|
|
|
@ -63,10 +63,15 @@ class PathItem extends FieldItemBase {
|
|||
* {@inheritdoc}
|
||||
*/
|
||||
public function postSave($update) {
|
||||
// If specified, rely on the langcode property for the language, so that the
|
||||
// existing language of an alias can be kept. That could for example be
|
||||
// unspecified even if the field/entity has a specific langcode.
|
||||
$alias_langcode = ($this->langcode && $this->pid) ? $this->langcode : $this->getLangcode();
|
||||
|
||||
if (!$update) {
|
||||
if ($this->alias) {
|
||||
$entity = $this->getEntity();
|
||||
if ($path = \Drupal::service('path.alias_storage')->save('/' . $entity->urlInfo()->getInternalPath(), $this->alias, $this->getLangcode())) {
|
||||
if ($path = \Drupal::service('path.alias_storage')->save('/' . $entity->urlInfo()->getInternalPath(), $this->alias, $alias_langcode)) {
|
||||
$this->pid = $path['pid'];
|
||||
}
|
||||
}
|
||||
|
@ -79,7 +84,7 @@ class PathItem extends FieldItemBase {
|
|||
// Only save a non-empty alias.
|
||||
elseif ($this->alias) {
|
||||
$entity = $this->getEntity();
|
||||
\Drupal::service('path.alias_storage')->save('/' . $entity->urlInfo()->getInternalPath(), $this->alias, $this->getLangcode(), $this->pid);
|
||||
\Drupal::service('path.alias_storage')->save('/' . $entity->urlInfo()->getInternalPath(), $this->alias, $alias_langcode, $this->pid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace Drupal\Tests\path\Functional;
|
||||
|
||||
use Drupal\Core\Language\LanguageInterface;
|
||||
|
||||
/**
|
||||
* Confirm that the Path module user interface works with languages.
|
||||
*
|
||||
|
@ -78,4 +80,36 @@ class PathLanguageUiTest extends PathTestBase {
|
|||
$this->assertText(t('Filter aliases'), 'Foreign URL alias works');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that language unspecific aliases are shown and saved in the node form.
|
||||
*/
|
||||
public function testNotSpecifiedNode() {
|
||||
// Create test node.
|
||||
$node = $this->drupalCreateNode();
|
||||
|
||||
// Create a language-unspecific alias in the admin UI, ensure that is
|
||||
// displayed and the langcode is not changed when saving.
|
||||
$edit = [
|
||||
'source' => '/node/' . $node->id(),
|
||||
'alias' => '/' . $this->getRandomGenerator()->word(8),
|
||||
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
|
||||
];
|
||||
$this->drupalPostForm('admin/config/search/path/add', $edit, t('Save'));
|
||||
|
||||
$this->drupalGet($node->toUrl('edit-form'));
|
||||
$this->assertSession()->fieldValueEquals('path[0][alias]', $edit['alias']);
|
||||
$this->drupalPostForm(NULL, [], t('Save'));
|
||||
|
||||
$this->drupalGet('admin/config/search/path');
|
||||
$this->assertSession()->pageTextContains('None');
|
||||
$this->assertSession()->pageTextNotContains('English');
|
||||
|
||||
// Create another node, with no alias, to ensure non-language specific
|
||||
// aliases are loaded correctly.
|
||||
$node = $this->drupalCreateNode();
|
||||
$this->drupalget($node->toUrl('edit-form'));
|
||||
$this->drupalPostForm(NULL, [], t('Save'));
|
||||
$this->assertSession()->pageTextNotContains(t('The alias is already in use.'));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue