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,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;
}
}