Skip to content
Snippets Groups Projects
Commit 7afe44be authored by Jon Minder's avatar Jon Minder
Browse files

Better multilingual support.

parent 03facbb9
No related branches found
No related tags found
No related merge requests found
......@@ -113,7 +113,7 @@ final class SettingsForm extends ConfigFormBase {
'#title' => $this->t('Node'),
'#type' => 'entity_autocomplete',
'#target_type' => 'node',
'#required' => $required,
'#required' => FALSE,
'#disabled' => !$required,
'#default_value' => !empty($nodes[$key]) ? $this->entityTypeManager->getStorage('node')->load($nodes[$key]) : NULL,
];
......@@ -136,7 +136,6 @@ final class SettingsForm extends ConfigFormBase {
}
$form['bundles'] = [
'#title' => $this->t('Node Types managed by frontend routing'),
'#type' => 'checkboxes',
......@@ -285,21 +284,20 @@ final class SettingsForm extends ConfigFormBase {
$nodes = $this->state->get('frontend_routing.settings') ?? [];
// Update the alias for all related nodes.
foreach ($nodes as $key => $node) {
if ($node) {
$node = $this->entityTypeManager->getStorage('node')->load($node);
foreach ($nodes as $key => $node_id) {
if ($node_id) {
$node = $this->entityTypeManager->getStorage('node')->load($node_id);
$aliases = !empty($config[$key]['aliases']) ? $config[$key]['aliases'] : [];
// Iterate through all languages and check if we have a translation.
foreach ($aliases as $language => $alias) {
// First delete the existing alias.
$source = '/' . ltrim($alias, '/' . $language);
$results = $this->entityTypeManager->getStorage('path_alias')->loadByProperties(['alias' => $source]);
$path = '/' . $node->toUrl()->getInternalPath();
$results = $this->entityTypeManager->getStorage('path_alias')->loadByProperties(['path' => $path]);
foreach ($results as $result) {
$result->delete();
}
// Iterate through all languages and check if we have a translation.
foreach ($aliases as $language => $alias) {
if ($node->hasTranslation($language)) {
$node = $node->getTranslation($language);
}
......@@ -307,14 +305,14 @@ final class SettingsForm extends ConfigFormBase {
continue;
}
$paths = $node->get('path')->getValue();
foreach ($paths as &$path) {
if ($path['langcode'] === $language) {
// Save the alias and set pathauto to false.
$alias = preg_replace('/^\/' . $language . '/', '', $alias);
$path['alias'] = $alias;
$path['pathauto'] = FALSE;
$node->set('path', $paths);
$node->save();
$this->updateAlias($path, $node);
}
}
}
......@@ -336,4 +334,30 @@ final class SettingsForm extends ConfigFormBase {
return $list;
}
private function updateAlias($path, $entity) {
$path_alias_storage = $this->entityTypeManager->getStorage('path_alias');
$alias_langcode = $path['langcode'];
// If we have an alias, we need to create or update a path alias entity.
if ($path['alias']) {
if (empty($path['pid'])) {
$path_alias = $path_alias_storage->create([
'path' => '/' . $entity->toUrl()->getInternalPath(),
'alias' => $path['alias'],
'langcode' => $alias_langcode,
]);
$path_alias->save();
$path['pid'] = $path_alias->id();
}
elseif ($path['pid']) {
$path_alias = $path_alias_storage->load($path['pid']);
if ($path['alias'] != $path_alias->getAlias()) {
$path_alias->setAlias($path['alias']);
$path_alias->save();
}
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment