history nav is now ok

This commit is contained in:
Bachir Soussi Chiadmi
2018-04-03 15:00:13 +02:00
parent f3fc776b71
commit 4e01b68f5b
20 changed files with 810 additions and 554 deletions
@@ -5,6 +5,53 @@
# @Filename: edlp_ajax.module
# @License: GPL-V3
use Drupal\Core\Url;
/**
* Implements hook_page_attachments().
* @param array $attachments
*/
function edlp_ajax_page_attachments(array &$attachments) {
$url = Url::fromRoute('edlp_ajax.entityjson');
$attachments['#attached']['drupalSettings']['edlp_ajax']['entityjson_path'] = $url->getInternalPath();
// $url = Url::fromRoute('edlp_ajax.urljson');
// $attachments['#attached']['drupalSettings']['edlp_ajax']['urljson_path'] = $url->getInternalPath();
$redirect = false;
$current_path = \Drupal::service('path.current')->getPath();
$is_front = \Drupal::service('path.matcher')->isFrontPage();
// do not redirect if note node, term, or custom routing
if(preg_match('/^\/?node\/\d+/', $current_path)
|| preg_match('/^\/?taxonomy\/term\/\d+/', $current_path)
|| preg_match('/^\/?productions/', $current_path)
|| preg_match('/^\/?agenda/', $current_path)
|| preg_match('/^\/?studio/', $current_path)){
// TODO: search url
$redirect = true;
}
$js_str = "var edlp = {\n
sys_path:'".$current_path."',\n
is_front:".($is_front ? 'true':'false').",\n
redirect:".($redirect ? 'true':'false').",\n
};";
$attachments['#attached']['html_head'][] = [
[
'#type' => 'html_tag',
'#tag' => 'script',
'#value' => $js_str,
'#weight' => -999,
'#group' => 'edlp'
],
// A key, to make it possible to recognize this HTML element when altering.
'edlp',
];
}
/**
* Implements hook_theme().
*/
@@ -6,20 +6,14 @@
# @Last modified time: 20-12-2017
# @License: GPL-V3
edlp_ajax.node:
path: '/edlp/ajax/{entity_type}/{id}/{viewmode}'
defaults:
_controller: '\Drupal\edlp_ajax\Controller\EdlpAjaxController::entity'
_title: 'EdlpAjax'
viewmode: 'default'
requirements:
_permission: 'access content'
edlp_ajax.ajaxnode:
path: '/edlp/ajax/json/{entity_type}/{id}/{viewmode}'
edlp_ajax.entityjson:
path: '/edlp/ajax/entity/json/{entity_type}/{id}/{viewmode}'
defaults:
_controller: '\Drupal\edlp_ajax\Controller\EdlpAjaxController::entityjson'
_title: 'EdlpAjaxJson'
entity_type : null
id: null
viewmode: 'default'
requirements:
_permission: 'access content'
# id: \d+
@@ -3,6 +3,9 @@
namespace Drupal\edlp_ajax\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Url;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -29,18 +32,6 @@ class EdlpAjaxController extends ControllerBase {
}
}
/**
* Display entity as a page.
*
* @return renderable array
*/
public function entity($entity_type, $id, $viewmode) {
$this->entity_type = $entity_type;
$this->id = $id;
$this->viewmode = $viewmode;
return $this->toRenderable();
}
/**
* Get entity as json through ajax.
*
@@ -50,6 +41,7 @@ class EdlpAjaxController extends ControllerBase {
$this->entity_type = $entity_type;
$this->id = $id;
$this->viewmode = $viewmode;
$renderable = $this->toRenderable();
$rendered = render($renderable);
@@ -64,17 +56,43 @@ class EdlpAjaxController extends ControllerBase {
$bundle = NULL;
}
$response = new JsonResponse();
$response->setData([
$data = [
'test' => 'hello edlp ajax',
'id' => $this->id,
'view_mode' => $this->viewmode,
'bundle' => $bundle,
'entity_type' => $this->entity_type,
'rendered'=> $rendered,
]);
];
// if content type page (productions) get the menu items
if($bundle == "page"){
$menuLinkManager = \Drupal::service('plugin.manager.menu.link');
$links = $menuLinkManager->loadLinksByRoute('entity.node.canonical', array('node' => $this->id), 'productions');
// dpm($links, 'links');
$menu_parents = array();
foreach ($links as $link) {
$parents = $menuLinkManager->getParentIds($link->getPluginId());
// dpm($parents, 'parents');
foreach ($parents as $parent_id) {
$parent = $menuLinkManager->getInstance(array('id'=>$parent_id));
// dpm($parent, 'parent');
$url = $parent->getUrlObject();
$sys_path = $url->getInternalPath();
// dpm($sys_path);
$menu_parents[] = $sys_path;
}
}
$data['menu_parents'] = $menu_parents;
}
$response = new JsonResponse();
$response->setData($data);
return $response;
// return array(
// '#markup'=>'hello'
// );
}
}