improved translation links update
This commit is contained in:
parent
488447fbe1
commit
b7459cf014
|
@ -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()
|
|
||||||
if (!$url_object) {
|
|
||||||
// if path does not exists rtuen an error
|
|
||||||
return new JsonResponse(array(
|
|
||||||
"links" => [],
|
|
||||||
"error" => "path {$path} is not valid"
|
|
||||||
));
|
|
||||||
}
|
|
||||||
$route_name = $url_object->getRouteName();
|
|
||||||
$route_parameters = $url_object->getrouteParameters();
|
|
||||||
$languages = $this->languageManager->getNativeLanguages();
|
$languages = $this->languageManager->getNativeLanguages();
|
||||||
|
if (!$url_object) {
|
||||||
|
// 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();
|
||||||
|
}
|
||||||
|
|
||||||
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
|
@ -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) => {
|
|
||||||
console.log("language link",path , link)
|
|
||||||
let hreflang = link.getAttribute('hreflang')
|
|
||||||
|
|
||||||
link.setAttribute('href', data.links[hreflang].url)
|
|
||||||
link.innerHTML = data.links[hreflang].title
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 => {
|
.catch(error => {
|
||||||
console.warn('Path translations links', error)
|
console.warn('Path translations links', error)
|
||||||
|
|
Loading…
Reference in New Issue