improved translation links update

This commit is contained in:
Bachir Soussi Chiadmi 2021-03-08 18:45:19 +01:00
parent 488447fbe1
commit b7459cf014
3 changed files with 26 additions and 24 deletions

View File

@ -48,32 +48,32 @@ class MaterioDecoupledLanguageLinks extends ControllerBase {
public function getPathTranslationLinks(Request $request) {
$post_data = json_decode( $request->getContent(),TRUE);
$path = $post_data['path'];
$data = [];
// build the links
$url_object = \Drupal::service('path.validator')->getUrlIfValid($path);
// Call to a member function getRouteName() on bool in Drupal\materio_decoupled\Controller\MaterioDecoupledLanguageLinks->getPathTranslationLinks()
$languages = $this->languageManager->getNativeLanguages();
if (!$url_object) {
// if path does not exists rtuen an error
return new JsonResponse(array(
"links" => [],
"error" => "path {$path} is not valid"
));
}
// if path does not exists
$data["error"] = "path {$path} is not valid";
$route_name = "<front>";
$route_parameters = null;
}else{
$route_name = $url_object->getRouteName();
$route_parameters = $url_object->getrouteParameters();
$languages = $this->languageManager->getNativeLanguages();
}
foreach ($languages as $key => $language) {
$url = Url::fromRoute($route_name, $route_parameters, ["language"=>$language]);
$links[$key] = [
"title" => $language->get('label'),
"url" => $url->toString()
"url" => $url->toString(),
"sys_path" => $url->getInternalPath()
];
}
$data = array(
"links" => $links
);
$data["links"] = $links;
return new JsonResponse($data);
}

File diff suppressed because one or more lines are too long

View File

@ -185,16 +185,18 @@ import { MA } from 'vuejs/api/ma-axios'
.then(({ data }) => {
console.log('Path translations links', data)
if (data.error) {
console.warn("error get translation paths", data.error);
} else {
console.warn("error getting translation paths", data.error);
}
links.forEach((link, i) => {
console.log("language link",path , link)
let hreflang = link.getAttribute('hreflang')
link.setAttribute('href', data.links[hreflang].url)
link.setAttribute('data-drupal-link-system-path', data.links[hreflang].sys_path)
link.innerHTML = data.links[hreflang].title
});
}
})
.catch(error => {
console.warn('Path translations links', error)