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) { public function getPathTranslationLinks(Request $request) {
$post_data = json_decode( $request->getContent(),TRUE); $post_data = json_decode( $request->getContent(),TRUE);
$path = $post_data['path']; $path = $post_data['path'];
$data = [];
// build the links // build the links
$url_object = \Drupal::service('path.validator')->getUrlIfValid($path); $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 (!$url_object) {
// if path does not exists rtuen an error // if path does not exists
return new JsonResponse(array( $data["error"] = "path {$path} is not valid";
"links" => [], $route_name = "<front>";
"error" => "path {$path} is not valid" $route_parameters = null;
)); }else{
}
$route_name = $url_object->getRouteName(); $route_name = $url_object->getRouteName();
$route_parameters = $url_object->getrouteParameters(); $route_parameters = $url_object->getrouteParameters();
$languages = $this->languageManager->getNativeLanguages(); }
foreach ($languages as $key => $language) { foreach ($languages as $key => $language) {
$url = Url::fromRoute($route_name, $route_parameters, ["language"=>$language]); $url = Url::fromRoute($route_name, $route_parameters, ["language"=>$language]);
$links[$key] = [ $links[$key] = [
"title" => $language->get('label'), "title" => $language->get('label'),
"url" => $url->toString() "url" => $url->toString(),
"sys_path" => $url->getInternalPath()
]; ];
} }
$data = array( $data["links"] = $links;
"links" => $links
);
return new JsonResponse($data); 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 }) => { .then(({ data }) => {
console.log('Path translations links', data) console.log('Path translations links', data)
if (data.error) { if (data.error) {
console.warn("error get translation paths", data.error); console.warn("error getting translation paths", data.error);
} else { }
links.forEach((link, i) => { links.forEach((link, i) => {
console.log("language link",path , link) console.log("language link",path , link)
let hreflang = link.getAttribute('hreflang') let hreflang = link.getAttribute('hreflang')
link.setAttribute('href', data.links[hreflang].url) 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 link.innerHTML = data.links[hreflang].title
}); });
}
}) })
.catch(error => { .catch(error => {
console.warn('Path translations links', error) console.warn('Path translations links', error)