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

Better multilingual support.

parent 03facbb9
Branches
Tags
No related merge requests found
...@@ -113,7 +113,7 @@ final class SettingsForm extends ConfigFormBase { ...@@ -113,7 +113,7 @@ final class SettingsForm extends ConfigFormBase {
'#title' => $this->t('Node'), '#title' => $this->t('Node'),
'#type' => 'entity_autocomplete', '#type' => 'entity_autocomplete',
'#target_type' => 'node', '#target_type' => 'node',
'#required' => $required, '#required' => FALSE,
'#disabled' => !$required, '#disabled' => !$required,
'#default_value' => !empty($nodes[$key]) ? $this->entityTypeManager->getStorage('node')->load($nodes[$key]) : NULL, '#default_value' => !empty($nodes[$key]) ? $this->entityTypeManager->getStorage('node')->load($nodes[$key]) : NULL,
]; ];
...@@ -136,7 +136,6 @@ final class SettingsForm extends ConfigFormBase { ...@@ -136,7 +136,6 @@ final class SettingsForm extends ConfigFormBase {
} }
$form['bundles'] = [ $form['bundles'] = [
'#title' => $this->t('Node Types managed by frontend routing'), '#title' => $this->t('Node Types managed by frontend routing'),
'#type' => 'checkboxes', '#type' => 'checkboxes',
...@@ -285,21 +284,20 @@ final class SettingsForm extends ConfigFormBase { ...@@ -285,21 +284,20 @@ final class SettingsForm extends ConfigFormBase {
$nodes = $this->state->get('frontend_routing.settings') ?? []; $nodes = $this->state->get('frontend_routing.settings') ?? [];
// Update the alias for all related nodes. // Update the alias for all related nodes.
foreach ($nodes as $key => $node) { foreach ($nodes as $key => $node_id) {
if ($node) { if ($node_id) {
$node = $this->entityTypeManager->getStorage('node')->load($node); $node = $this->entityTypeManager->getStorage('node')->load($node_id);
$aliases = !empty($config[$key]['aliases']) ? $config[$key]['aliases'] : []; $aliases = !empty($config[$key]['aliases']) ? $config[$key]['aliases'] : [];
// First delete the existing alias.
$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. // Iterate through all languages and check if we have a translation.
foreach ($aliases as $language => $alias) { foreach ($aliases as $language => $alias) {
// First delete the existing alias.
$source = '/' . ltrim($alias, '/' . $language);
$results = $this->entityTypeManager->getStorage('path_alias')->loadByProperties(['alias' => $source]);
foreach ($results as $result) {
$result->delete();
}
if ($node->hasTranslation($language)) { if ($node->hasTranslation($language)) {
$node = $node->getTranslation($language); $node = $node->getTranslation($language);
} }
...@@ -307,14 +305,14 @@ final class SettingsForm extends ConfigFormBase { ...@@ -307,14 +305,14 @@ final class SettingsForm extends ConfigFormBase {
continue; continue;
} }
$paths = $node->get('path')->getValue(); $paths = $node->get('path')->getValue();
foreach ($paths as &$path) { foreach ($paths as &$path) {
if ($path['langcode'] === $language) { if ($path['langcode'] === $language) {
// Save the alias and set pathauto to false. // Save the alias and set pathauto to false.
$alias = preg_replace('/^\/' . $language . '/', '', $alias); $alias = preg_replace('/^\/' . $language . '/', '', $alias);
$path['alias'] = $alias; $path['alias'] = $alias;
$path['pathauto'] = FALSE; $path['pathauto'] = FALSE;
$node->set('path', $paths); $this->updateAlias($path, $node);
$node->save();
} }
} }
} }
...@@ -336,4 +334,30 @@ final class SettingsForm extends ConfigFormBase { ...@@ -336,4 +334,30 @@ final class SettingsForm extends ConfigFormBase {
return $list; 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