language siwtchers links are working now (reloading the whole site but with the right url)

This commit is contained in:
2018-04-14 16:15:11 +02:00
parent cb2ae7ee35
commit 252149bfdc
9 changed files with 315 additions and 25 deletions
@@ -5,8 +5,10 @@ namespace Drupal\edlp_ajax\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Url;
use Drupal\Core\Language\LanguageInterface;
use Drupal\menu_link_content\Entity\MenuLinkContent;
// use Symfony\Component\HttpFoundation\JsonResponse;
use \Drupal\block\Entity\Block;
use Drupal\Core\Cache\CacheableJsonResponse;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\core\render\RenderContext;
@@ -94,6 +96,20 @@ class EdlpAjaxController extends ControllerBase {
$data['menu_parents'] = $menu_parents;
}
// translations links
$links = \Drupal::languageManager()->getLanguageSwitchLinks(LanguageInterface::TYPE_URL, $this->entity->toUrl());
if (isset($links->links)) {
$translations_build = [
'#theme' => 'links__language_block',
'#links' => $links->links,
'#attributes' => ['class' => ["language-switcher-{$links->method_id}"]],
'#set_active_class' => TRUE,
];
$translations_rendered = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($translations_build) {return render($translations_build);});
// dpm($links);
$data['translations_links'] = $translations_rendered;
}
$data['#cache'] = [
'max-age' => \Drupal\Core\Cache\Cache::PERMANENT,
'tags' => [
@@ -110,8 +126,85 @@ class EdlpAjaxController extends ControllerBase {
return $response;
dpm($response);
// dpm($response);
// return array(
// '#markup'=>'hello'
// );
}
/**
* Get all visisble blocks from edlptheme as json through ajax.
*
* @return json
*/
// NOT USED (YET)
public function blocksjson($blockid) {
$block_viewbuilder = \Drupal::entityTypeManager()->getViewBuilder('block');
//
$blocksids = [];
if(!$blockid){
// TODO: get all blocks definition from edlptheme
$block_storage = \Drupal::entityTypeManager()->getStorage('block');
// dpm($block_storage, '$block_storage');
$allblocks = $block_storage->loadMultiple();
// dpm($blocks, 'blocks');
foreach ($allblocks as $block) {
// dpm($block->id(), 'block');
// dpm($block->getTheme(), 'theme');
// $visibility = $block->getVisibility();
if($block->getTheme() == "edlptheme"){
// dpm($block, $block->id());
$blocksids[] = $block->id();
}
}
// $block_listbuilder = \Drupal::entityTypeManager()->getListBuilder('block');
// dpm($block_listbuilder, '$block_listbuilder');
// // foreach ($blocks as $key => $value) {
// // $block = \Drupal\block\Entity\Block::load('productions');
// // }
}
else{
$blocksids[] = $blockid;
}
// dpm($blocksids, 'blockids');
//
$blocks = [];
foreach ($blocksids as $id) {
$block = Block::load($id);
$block_render = $block_viewbuilder->view($block);
$rendered = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($block_render) {
return render($block_render);
});
$blocks[$id] = array(
'rendered'=> $rendered,
'region'=> str_replace('_', '-', $block->getRegion()),
'id'=> preg_replace('/^[^:]+:(.+)$/', 'block-$1', $block->getPluginId())
);
}
// dpm($blocks, 'blocks');
//
$data = [
'blockid'=>$blockid,
'blocks' => $blocks,
'#cache' => [
'max-age' => \Drupal\Core\Cache\Cache::PERMANENT,
'tags' => [
'edlp-ajax-blocks-cache',
// $this->entity_type.':'.$this->id // not necessary as we add $renderable as CacheableMetadata to the response
]
]
];
// dpm($data);
//
$response = new CacheableJsonResponse($data);
// $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($renderable));
$response->addCacheableDependency(CacheableMetadata::createFromRenderArray($data));
//
return $response;
// dpm($response);
//
return array(
'#markup'=>'hello'
);