74 lines
1.9 KiB
PHP
74 lines
1.9 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace Drupal\materio_decoupled\Controller;
|
||
|
|
||
|
use Drupal\Core\Controller\ControllerBase;
|
||
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||
|
use Drupal\Core\Language\LanguageManagerInterface;
|
||
|
use Drupal\Core\Url;
|
||
|
use Symfony\Component\HttpFoundation\Request;
|
||
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||
|
/**
|
||
|
* Class AjaxHomeController.
|
||
|
*/
|
||
|
class MaterioDecoupledLanguageLinks extends ControllerBase {
|
||
|
|
||
|
|
||
|
/*
|
||
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
|
||
|
*/
|
||
|
protected $languageManager;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* {@inheritdoc}
|
||
|
*/
|
||
|
public static function create(ContainerInterface $container) {
|
||
|
return new static(
|
||
|
$container->get('language_manager')
|
||
|
);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Constructs a new MaterioDecoupledLanguageLinks object.
|
||
|
*
|
||
|
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
|
||
|
* The language manager.
|
||
|
*/
|
||
|
public function __construct(LanguageManagerInterface $language_manager) {
|
||
|
$this->languageManager = $language_manager;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* getPathTranslationLinks
|
||
|
*
|
||
|
* @return string
|
||
|
* Return translations links given a path.
|
||
|
*/
|
||
|
public function getPathTranslationLinks(Request $request) {
|
||
|
$post_data = json_decode( $request->getContent(),TRUE);
|
||
|
$path = $post_data['path'];
|
||
|
|
||
|
// build the links
|
||
|
$url_object = \Drupal::service('path.validator')->getUrlIfValid($path);
|
||
|
$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()
|
||
|
];
|
||
|
}
|
||
|
|
||
|
$data = array(
|
||
|
"links" => $links
|
||
|
);
|
||
|
|
||
|
return new JsonResponse($data);
|
||
|
}
|
||
|
|
||
|
}
|