refactored edlp_ajax_node module to edlp_ajax to be able to get taxonomy terms

This commit is contained in:
Bachir Soussi Chiadmi
2018-03-08 16:44:54 +01:00
parent fb4701f043
commit 3980479031
21 changed files with 248 additions and 206 deletions
@@ -0,0 +1,11 @@
# @Author: Bachir Soussi Chiadmi <bach>
# @Email: bachir@figureslibres.io
# @Filename: edlp_ajax.info.yml
# @License: GPL-V3
name: Edlp Ajax
type: module
description: Manage ajax entity loading for edlp d8.
core: 8.x
package: Edlp
@@ -0,0 +1,41 @@
<?php
# @Author: Bachir Soussi Chiadmi <bach>
# @Email: bachir@figureslibres.io
# @Filename: edlp_ajax.module
# @License: GPL-V3
/**
* Implements hook_theme().
*/
function edlp_ajax_theme($existing, $type, $theme, $path) {
// @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module
return array(
'edlp_ajax' => array(
'file' => 'includes/edlp_ajax.inc',
'variables' => array(
'entity_type' => 'node',
'entity' => NULL,
'view_mode' => 'default'
),
),
);
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function edlp_ajax_theme_suggestions_edlp_ajax(array $vars) {
// dpm($vars);
$suggestions = [];
// $node = $variables['elements']['#node'];
$sanitized_view_mode = strtr($vars['view_mode'], '.', '_');
//
$suggestions[] = 'edlp_ajax__' . $vars['entity_type'];
$suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $sanitized_view_mode;
// $suggestions[] = 'node__' . $node->bundle() . '__' . $sanitized_view_mode;
$suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['entity']->id();
$suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['entity']->id() . '__' . $sanitized_view_mode;
return $suggestions;
}
@@ -0,0 +1,25 @@
# @Author: Bachir Soussi Chiadmi <bach>
# @Date: 13-12-2017
# @Email: bachir@figureslibres.io
# @Filename: edlp_corpus.routing.yml
# @Last modified by: bach
# @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}'
defaults:
_controller: '\Drupal\edlp_ajax\Controller\EdlpAjaxController::entityjson'
_title: 'EdlpAjaxJson'
viewmode: 'default'
requirements:
_permission: 'access content'
@@ -0,0 +1,12 @@
<?php
// use Drupal\Core\Url;
function template_preprocess_edlp_ajax(&$vars){
// dpm($vars);
/*
@see https://www.drupal8.ovh/index.php/en/tutoriels/339/render-a-node-or-an-entity
*/
$view_builder = \Drupal::entityTypeManager()->getViewBuilder($vars['entity_type']);
$vars['content'] = $view_builder->view($vars['entity'], $vars['view_mode']);
}
@@ -0,0 +1,69 @@
<?php
namespace Drupal\edlp_ajax\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;
class EdlpAjaxController extends ControllerBase {
private function query() {
$this->entity = entity_load($this->entity_type, $this->id);
}
private function toRenderable(){
$this->query();
if($this->entity){
return array(
"#theme"=>'edlp_ajax',
"#entity_type" => $this->entity_type,
'#entity' => $this->entity,
'#view_mode' => $this->viewmode,
);
}else{
return array(
'#markup'=>'404! '.$this->entity_type.' '.$this->id." not found!"
);
}
}
/**
* 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.
*
* @return json
*/
public function entityjson($entity_type, $id, $viewmode) {
$this->entity_type = $entity_type;
$this->id = $id;
$this->viewmode = $viewmode;
$renderable = $this->toRenderable();
$rendered = render($renderable);
$response = new JsonResponse();
$response->setData([
'test' => 'hello edlp ajax',
'id' => $this->id,
'view_mode' => $this->viewmode,
'bundle' => $this->entity->getType(),
'entity_type' => $this->entity_type,
'rendered'=> $rendered,
]);
return $response;
}
}
@@ -0,0 +1 @@
{{ content }}